Recently I have been thinking about how to improve development efficiency. From web crawlers to data processing, and then to artificial intelligence, Python’s strength lies in having a large set of out-of-the-box toolkits, so you do not have to reinvent the wheel, which greatly improves development efficiency. So why not modularize the functional pieces of a project and package them as reusable Python packages? This article is mainly about how to package a piece of functionality as a Python package and upload it to PyPI.
1. First You Need a Python Package
In the course of Python development, you can separate out modules with very clear responsibilities as a standalone Python package. This not only helps with maintaining and upgrading the modules, but more importantly lets you reuse these Python packages during project development.
In one of my recent projects, I needed to control access permissions for Django Views. Django’s built-in
Permission provides table-level permission control, while django-guardian provides object-level permission control. But this project had no Model at all; the data came entirely from third-party APIs. After searching GitHub without success, I decided to develop a Django App for permission control at View granularity.
| |
2. Packaging the Source
You can package the source code with the following command:
| |
This creates an additional package ending in tar.gz in the dist folder of the current directory.
3. Uploading the Package
3.1 Registering an Account
Visit: https://pypi.python.org/pypi and register your own account yourname:yourpassword.
3.2 Creating the .pypirc Configuration File
If you do not configure the .pypirc file, uploading a package to PyPI will report a 403 error. The .pypirc file is used to authenticate permissions when uploading a package to PyPI. Create the file $HOME/.pypirc in your home directory with the following content:
| |
3.3 Uploading the Source Package
| |
3.5 Install and Test
| |
