Packaging and Publishing Python code

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!

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:

This article is contributed by Nikhil Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Article Tags :