PyPI is the official third-party repository of Python software. At the time of this writing, PyPI already contains 95971 packages!
pip uses PyPI as the default source for packages and their dependencies. So when you type:
pip install package_name
pip will look for that package in PyPI and, if found, will download and install the package on your local system.
In this article, I will show you how you can publish your own Python package to PyPI so that it can be installed on one line and easily accessible to all other Python users on the web! I'll take an example of a sample package and show you the whole process. An example package is hosted at Github .
Step 1: Prepare Python Scripts p>
First step — this is of course preparing your Python program (which you want to publish to PyPI)!
This can be any python script. Here I am using my own Python script, which I named " locator.py " (I use this name for reference only. Feel free to save your Python scripts under any name.) This file is available here .
Step 2: Prepare the package directory structure
This is the most important step. Now we have to follow some predefined structure for our package directory.
As a reference, feel free to check out the Github repository of the sample project used in this tutorial. You can clone this repository and make some changes to create your own package.
The directory structure should be like this:
Ok, let's discuss what all these files will contain.
pip install setuptools
This is what my setup.py looks like:
| tr> |
Let's see what the various arguments to the setup function do:
Step 3: Create your accounts
Now it's time to create an account at PyPI and test PyPI . Test PyPI — this is just a test site that we will first upload our code to to see if everything is working correctly or not.
After creating the accounts, create this .pypirc file in your home directory system and enter your account credentials.
[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, simply enter echo% USERPROFILE% at the command line to find out your PC's home directory. Place there .pypirc file.
Step 4: Download package
Finally, we are ready to upload our package to PyPI!
python setup.py register -r pypitest
This will try to register your package with the PyPI test server, just to make sure you set everything up correctly. p >
Now run this:
python setup.py sdist upload -r pypitest
You shouldn't get any errors and should now be able to see your library in PyPI test repository .
python setup.py register -r pypi
python setup.py sdist upload -r pypi
And you're done! Your package is now publicly available on PyPI and can be easily installed with a simple pip command!
The package we created with this tutorial is available here .
Just type in the terminal,
pip install your_package_name
check if the installation process completed successfully.
This article contributed by Nikhil Kumar . If you like Python.Engineering and would like to contribute, you can also write an article using contrib.python.engineering, or email your article to [email protected] See my article appearing on the Python.Engineering homepage and help other geeks.
Please post comments if you find anything wrong or if you would like to share more information on the topic discussed above.