This article mainly introduces the method of packaging and publishing Python code. Friends in need can refer to it
In the python program, a .py file is regarded as a module and is defined in each module different functions. When we want to use a function in a module, we must first import the module, otherwise the function will be undefined.
Recorded below is the method of packaging and installing the package.
The example in this article is to create a simulated login program:
The logIn.py file code is as follows:
pwd=int(raw_input('please input your passward: ')) if pwd==123: print 'success' else: print 'error'
1. Packaging
1. First create a folder. This folder 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 in the distribution folder with the following code:
from distutils.core import setup setup( name='logIn', #这个是最终打包的文件名 version='1.0.0', py_modules=['logInr'], #要打包哪些,.py文件, )
3. In the final step, cd to the distrbution folder, and then run the following command:
python setup.py sdist
This way There are a few more files in the folder. In the dist folder, logIn-1.0.0.tar.gz is our release package;
2. Install the package to the local copy Medium:
sudo python setup.py install
The path is:/usr/local/lib/python2.7/dist-packages
The above is the detailed content of Packaging and publishing of Python code. For more information, please follow other related articles on the PHP Chinese website!