People who have written python know that Python is the most convenient and the most good thing is that it has countless third-party lib can be used directly, you can make it easier to write code.
Long-use installation of the third-party Lib method has Easy_install and Pip, the use of these two methods is about the same. This is not the focus of this article to describe, I will simply write two:
Easy_install
Install new package: Easy_install install xxxx
To uninstall an installed package: Easy_install uninstall xxxx
Update installed packages: easy_install-u xxxx
Pip
Install new package: Pip install xxxx
To uninstall an installed package: Pip uninstall xxxx
Update installed packages: Easy_install Install Xxxx-u
To view installed packages: Pip list
Make your own installation package with Distutils
How to make your own package can be installed as a third-party library? Take a look at the following example:
mylib.py file
defAdd (x, y):returnx+ydefDivision (x, y):returnx/ydefmutiply (x, y):returnx*ydefsubtract (x, y):returnX-ydefMain ():Passif __name__=='__main__': Main ()
A simple way to get into the directory that mylib.py stores, and then import Mylib to use the method it provides. But if the project is large and requires a lot of people to collaborate, it is very inconvenient. You can use the Distutils standard library at this time. We can then create a setup.py in the mylib.py sibling directory with the following content:
from Import setupsetup (Name='mylib', version='1.0' ) , py_modules=['mylib'),)
Name is the specified package name, version is the revision number, and Py_modules contains the Python file that needs to be installed, in this case the mylib.py file.
Once you have written your setup.py, you can use
Install
Install it into the system. After the installation I can use the PIP list to view the installed package information, and in any place in the Python file can be import mylib.
Create a project automatically using Pastescript
So how do you upload your own installation package to PyPI, so you can try Easy_install or PIP installation like any other installation package?
This needs to follow the PEP241, give enough metadata to do, such as the description of the package, the author's information, Authorization, home page and so on. Need a very complex setup.py file, this time you can manually write the following files:
fromDistutils.coreImportSetup fromSetuptoolsImportSetup, Find_packagessetup (name=' MyLib', Version='0.0.1', Keywords= (' Simple','Test'), Description='just a simple test', License='MIT License', author='Jim', Author_email='[email protected]', Packages=find_packages (), Platforms=' any',)
Then run the following command to generate a Mylib package. Finally, the level directory including setup.py is uploaded to PyPI.
# All of the following makefile will be in the current path under the Dist directory in Python setup.py Bdist_egg # generate Easy_install supported formats Python setup.py sdist # Generate PIP supported formats, as an example
There is a third-party library Pastescript can help us to automatically create such a complex file, you just need to answer its questions on it.
First we pass
Pip Install Pastescript
Install the Pastescript package, and then you can use the following command to automatically create a complex setup.py package:
Paster create-o mylib-2 -T basic_packge mylib
If you do not want to answer questions, you can also edit the configuration file in advance config.cfg
[pastescript] = = = I am mylib, = = [email protected] = = 1.0.0
And then use the command line to automatically load
Paster create-t basic_package--config="config.cfg" mylib
Share the installation package on PyPI
The above fix the installation package in the local installation, then the following start to share Mylib to PyPI, then certainly need a pypi account, this yourself to register it.
Then go to the directory that just generated the mylib, run
Python setup.py Register
Fill in the system with the information we registered on the PyPI, including the user name, password, and register the package name on the PyPI. (Make sure there are no packages with the same name on PyPI)
Execute after completion
Python setup.py sdist upload
Upload the code to the PyPI.
Finally note the points:
1. The package uploaded to PyPI is likely to fail with a PIP or easy_install download, prompting can not find a version satisfy request xxxxx. The reason is that when the package is generated by default as Dev, so can not release, so you have to manually modify its auto-generated configuration file setup.cfg, the Tag_build = dev to tag_build =
2. After the trial found that the package generated by the Pastescript can be uploaded according to PyPI, but it passes on only an empty package, there is no mylib.py this file, it may be necessary to use other parameters to generate the package, rather than basic_packge , and the package that you built with Python setup.py sdist has no problem.
How to create your own Python package