Packaging and Publishing Python code
Last Updated :
29 Sep, 2022
If you have been coding in python, even for a while, you must be familiar with the concept of ‘pip’ by now. It is a package management system used to install and manage software packages/libraries written in Python.
Then one may ask that where are all these packages/libraries stored? It is obvious that there must be a large “on-line repository” which stores all this code. And the answer to this question is Python Package Index (PyPI).
PyPI is the official third-party software repository for Python. At the time of writing this article, PyPI was already hosting 95971 packages!
pip uses PyPI as the default source for packages and their dependencies. So whenever you type:
pip install package_name
pip will look for that package on PyPI and if found, it will download and install the package on your local system.
In this article, I will demonstrate how you can publish your own Python package on PyPI so that it is one-line installable and easily available to all other python users online! I will take the example of a sample package and show you the complete process. The example package is hosted on Github.
Step 1: Get the python scripts ready
First step is, of course, getting your python program (which you want to publish on PyPI) ready!
It could be any python script. Here I am using my own python script which I have named ‘locator.py‘ (I am using this name just for reference purpose. Feel free to save your python scripts by any name.) This file is available here.
Step 2: Getting the package-directory structure ready
This is the most important step. Now, we have to follow some pre-defined structure for our package’s directory.
As a reference, feel free to check out the Github repository of the sample project which is used in this tutorial. You can clone this repository and make some modifications to create your own package.
The directory structure has to be like this :
Ok let’s discuss what all these files will contain.
Step 3: Create your accounts
Now, its time to create an account on PyPI and Test PyPI. Test PyPI is just a testing site where we will upload our code first to see if everything works correctly or not.
Once accounts have been made, create this .pypirc file in home directory of your system and enter the account details.
[distutils]
index-servers =
pypi
pypitest
[pypi]
repository=https://pypi.python.org/pypi
username= your_username
password= your_password
[pypitest]
repository=https://testpypi.python.org/pypi
username= your_username
password= your_password
Note: If you are on a windows system, just type echo %USERPROFILE% in command prompt to know the home directory of your PC. Put the .pypirc file there.
Step 4: Upload the package
Finally, we are ready to upload our package on PyPI!
- First of all, we will check if our package installs correctly on Test PyPI.
Open command prompt/terminal in the root directory of your package.
Run this in terminal:
python setup.py register -r pypitest
This will attempt to register your package against PyPI’s test server, just to make sure you’ve set up everything correctly.
Now, run this:
python setup.py sdist upload -r pypitest
You should get no errors, and should also now be able to see your library in the test PyPI repository.
- Once you’ve successfully uploaded to PyPI Test, perform the same steps but point to the live PyPI server instead.
To register on PyPI, run:
python setup.py register -r pypi
Then, run:
python setup.py sdist upload -r pypi
And you’re all done! Your package is now publicly available on PyPI and could be easily installed by a simple pip command!
The package we created using this tutorial is available here.
Just type on terminal,
pip install your_package_name
to check if installation process is getting completed successfully.
References:
Share your thoughts in the comments
Please Login to comment...