This tutorial will teach you how to upload a Python Package to PyPi and show all the requirements needed to get this done.
1. PyPI and its Uses
PIP is the Python Package Manager (Python Installer Package); it is a way to install and manage additional packages and dependencies that are not yet distributed as part of the standard version of the package. PIP Package is integrated with the Python installation from Python3 versions 3.4 and versions 2.7.9 for Python2 and is used in many Python projects.
You will need to install it via the get-pip script for all other versions. Once the last download, you need to start the order: Pypi, for Python Package Index, is somehow a centralized python packet server. It centralizes the different modules/packages made available by the community and manages the other versions. If you do not always have an internet connection, you also can create a local copy. Better yet, if, for various reasons, you do not want to use the official PyPI, you can install a local PyPI totally or partially blank and use it conventionally.
PyPI does not come alone since a dedicated tool exists: PIP. This tool will make it possible to
- establish a connection between a computer and the PyPI server to install / update.
- uninstall a module/package with more or fewer options than we will see together.
The PIP tool will allow you to install modules/packets from the official PyPI server, online from a local PyPI server, or from a specific folder you will stipulate.
Python includes a robust packaging system to handle the module dependencies of your programs. You have probably used it to install third-party packages with the PIP package manager command.
A confusing appearance of the package installation with PIP is that it tries to settle in your default global python environment. Of course, this makes the brand new package you install available around the world for yourself, which is very convenient. But this is also rapidly transformed into a nightmare if you work on several projects requiring versions of the same package.
For example, what to do if one of your projects needs version 1.3 of a library while another project needs version 1.4 of the same library?
You can also have different programs that need different python versions themselves. For example, some programs can still operate in Python 2, while most of your new developments are for Python 3.
- Install packages with PIP:
Python is a language with an integrated battery. The standard Python library includes an extended set of packages and modules to help developers in their scripts and applications.
At the same time, the Python community is very active; it still provides larger sets of packages that can help you in your development. These packages are in the Python Package Index, Pypi.
The most common use of PIP is the installation from PyPI using a specifier on the requirements.
Using additional files:
The PIP install command is always based on the latest package version (module); sometimes, you want to install a precise version compatible with your code.
2. Requirements to Upload Python Package to PyPi
There are a few things you need to get set up before uploading a python package to PyPI; see below:
- Create an account on PyPi and PyPI test server. These are two different account, but please keep the same username with different passwords
- Install twine – pip install twine
- Set up the token access (optional and explained on the next section)
3. Setting Up a PyPi Token
Setting up a PyPi is not required but highly recommended. The PyPi token should be created at ~/.pypirc, following the format below.
[distutils]
index-servers =
pypi
testpypi
[pypi]
username = __token__
password = TOKEN_1
[testpypi]
username = __token__
password = TOKEN_2
You can create the PyPi account and the PyPi test server token.
4. Upload Python Package to PyPi
Next, everything is set up; you can generate a source distribution and a wheel for your package
$ python setup.py sdist bdist_wheel
Next, you can test if your package was developed with no problem by running the command below:
$ twine check dist/*
Now. upload files to PyPI test server
$ twine upload --repository testpypi dist/*
Last but not least, upload it to the PyPi account here, and hopefully, you will get no errors:
$ twine upload dist/*
If you did not set up your tokens, you must enter your username and password. Alternatively, you can pass the flags in the two commands above the flags -u USERNAME -p PASSWORD.
Conclusion
This blog post taught how to upload a Python package to PyPi in a step-by-step tutorial, including setting up the PyPi token in case it is needed. Please leave your comments below – Did you set up the Pypi token, or did you decide to use the username and password flags instead?