0%

How to Make Your Project pip-available

pip is a well-known management system people who have fun with Python use to install and manage packages. You might want to install a third-party package with following command:

1
pip install PACKAGE

It is convenient to use a package published by others. If you want to publish your code, here is the easy way to do:

Sign Up an Account on PyPi

It not difficult to register on PyPi. Remember your username and password. You will need to type it in the console when you upload your project.

Clean Your Project and Code

To make sure you have remove all the irrelevant code and redundant lines of code, e.g. print("...") . Use logging to give any necessary info if you want.

Move large files(e.g. data files) out and only give a link to your audience in README.md.

Create Necessary Files for PyPi

These files should be in the same directory where the package (the one you want to upload) is:

1
2
3
4
setup.py
setup.cfg
LICENSE.txt
README.md

setup.py

You need to give meta-data information about your project in setup.py. The easiest way to do this is to copy a template and only replace several strings. I really recommend this template, the author has left some comments about the strings you need to change.

setup.cfg

Create a new file called “setup.cfg”. You can specify your description file if you have:

1
2
[metadata]
description-file = README.md

LICENSE.txt

Use this file to define all license details.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
MIT License
Copyright (c) 2018 YOUR NAME
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

README.md

Give all the info your audience should know, including: 1. How to install 2. What are the features 3. Examples for using 4. Anything else You can easily find a template in Github.

Upload Your Project to PyPi

Upload it to Github first. Install setuptools and twine:

1
2
pip install --upgrade setuptools
pip install twine
Then you can use this lines to upload it to PyPi:
1
2
python setup.py sdist bdist_wheel
twine upload dist/*
An easy way to upload:
1
python setup.py upload 

Then we are all set, you can check it:

1
pip install PACKAGE

If you have a new version, do not forget to change the VERSION and REQUIRED in setup.py before re-upload it.

People can upgrade it in this way:

1
pip install PACKAGE --upgrade

Reference

How to upload your python package to PyPi (Chinese)