This article mainly describes the Python code packaging and publishing methods, the need for friends can refer to the following
In a Python program, a. py file is treated as a module that defines different functions in each module. When we want to use a certain function in a module, we must first import the module, otherwise there will be a case where the function is undefined.
The following records are the methods for packaging and installing packages.
The example in this article is to create a program that simulates login:
The login.py file code is as follows:
Pwd=int (raw_input (' Please input your passward: ')) if pwd==123: print ' success ' else: print ' ERROR '
First, packaging
1. First create a folder that is used to store the. py file we will use for publishing, (now we create a folder named distribution, put login.py in this folder)
2. Create a new setup.py file code in the distribution folder as follows:
From Distutils.core import setupsetup (name= ' logIn ', #这个是最终打包的文件名 version= ' 1.0.0 ', py_modules=[' Loginr '], #要打包哪些, . py file,)
3. In the end, CD into this distrbution folder, then move the line as follows:
Python setup.py sdist
So in the folder there are a few more files, in the Dist folder, Login-1.0.0.tar.gz is our release package;
Second, install the package to the local copy:
sudo python setup.py install
Path is:/usr/local/lib/python2.7/dist-packages