How to publish your own Python Package in pypi

Prudhvi Gnv
5 min readSep 8, 2020

--

Python is famous for coming with batteries included. Sophisticated capabilities are available in the standard library. You can find modules for working with sockets, parsing CSV, JSON, and XML files, and working with files and file paths.

The PyPi package index is one of the properties that makes python so powerfull: With just a simple command, you get access to thousands of cool libraries, ready for you to use.

However great the packages instead. included with Python are, there are many fantastic projects available outside the standard library. These are most often hosted at the Python Packaging Index (PyPI), historically known as the Cheese Shop. At PyPI, you can find everything from Hello World to advanced deep learning libraries.

But what is PyPi?

Well, according to their Website:

The Python Package Index (PyPI) is a repository of software for the Python programming language. PyPI helps you find and install software developed and shared by the Python community.

Overview:

  • Pre-requisites
  • Make your code publish-ready
  • Create a python package
  • Create the files PyPi needs
  • Create a PyPi account
  • Upload your package to github.com
  • Upload your package to PyPi
  • Install your own package using pip
  • Change your package

SOME PRE-REQUISITES BEFORE WE START IT…

  1. You should have Python3 installed on your machine.
  2. Make sure you have the latest version of pip. Be sure by typing it on your terminal or command prompt
python -m pip install -upgrade pip

3. You should have installed wheel and twine library. If not then they can be easily installed by the following commands,

pip install wheelpip install twine

4. You should have a working GitHub account.

Make your code publish-ready

Let’s prepare your code for the uploading. First, you should remove all “print” statements from your code. It’s annoying when you work with a library and your command promt is flooded with print messages that are not yours — therefore remove all of them. If you want to inform the user about certain activities, use logging.

wrap it into the “__main__” function.

if __name__ == "__main__":    
your example code goes here

Create a python package

To create a package, create a folder that is named exactly how you want your package to be named. Place all the files and classes that you want to ship into this folder.

Now, create a file called __init__.py (again two underscores).

from packagename.Filename import Classname

Create a PyPi account

You can register yourself for a PyPi account here.

Upload your package to github.com

Create a github repo including all the files we are going to create in a second, as well as your package folder. Name the repo exactly as the package. You find an example of such a repo here.

Create the files PyPi needs

PyPi needs three files in order to work:

  • setup.py
  • setup.cfg
  • LICENSE.txt
  • README.md (optinal but highly recommended)

setup.py

The setup.py file contains information about your package that PyPi needs, like its name, a description, the current version etc. Copy and Paste the following Code and replace the Strings with your matching content:

Let me explain the download_url and the install_requires a bit further.

download_url

You have previously uploaded your project to your github repository. Now, we create a new release version of your project on github. This release will then be downloaded by anyone that runs the “pip install YourPackage” command.

First, go to github.com and navigate to your repository. Next, click on the tab “releases” and then on “Create a new release”. Now, define a Tag verion (it is best to use the same number as you used in your setup.py version-field: v_01. Add a release title and a description (not that important), then click on “publish release”. Now you see a new release and under Assets, there is a link to Source Code (tar.gz). Right-click on this link and chose Copy Link Address. Paste this link-address into the download_url field in the setup.py file. Every time you want to update your package later on, upload a new version to github, create a new release as we just discussed, specify a new release tag and copy-paste the link to Source into the setup.py file (do not forget to also increment the version number).

Install_requires

Here, you define all the dependencies your package has — all the pip packages that you are importing.

import keras as k
import numpy as np

The resulting install_requires field would look like this:

install_requires=[       
'numpy',
'keras',
],

setup.cfg

This is a short one. Create a new file called “setup.cfg”. If you have a description file (and you definitely should!), you can specify it here:

# Inside of setup.cfg
[metadata]
description-file = README.md

LICENSE.txt

Use this file to define all license details. Most propably, just copy-paste the license text from this website into the LICENSE.txt file. I always use the MIT license for my projects, but feel free to use your own.

README.md (Optional)

Create a markdown file with this beautiful website, then download it and place it into your directory.

Upload your package to PyPi

Now, the final step has come: uploading your project to PyPi. First, open the command prompt and navigate into your the folder where you have all your files and your package located:

cd "C://PATH//TO//YOUR//FOLDER"

Now, we create a source distribution with the following command

python setup.py sdist

You might get a warning stating “Unknown distribution option: ‘install_requires’. Just ignore it.

We will need twine for the upload process, so first install twine via pip:

pip install twine

Then, run the following command:

twine upload dist/*

You will be asked to provide your username and password. Provide the credentials you used to register to PyPi earlier.

notes:

Twine → used to uploading project builds to pypi

sdist → is used to make a source code artifact that can be uploaded to pypi. it will place this artifact in ./build/ directory as project-name-version.tar.gz

Congratulations, your package is now uploaded! Visit https://pypi.org/project/YOURPACKAGENAME/ to see your package online!

Install your own package using pip

Okay, now let’s test this out. Open your console and type the following command:

pip install YOURPACKAGENAME

It works! Now open the python SHELL/IDLE and import your package.

Update your package

If you maintain your package well, you will need to change the source code form time to time.

python setup.py sdist
twine upload dist/*

Finally, update your package via pip

pip install YOURPACKAGE --upgrade

I published a python package called Py-AutoML,

checkout :

source code here: www.github.com/PrudhviGNV/py-automl

and pypi: https://pypi.org/project/py-automl/

--

--

Prudhvi Gnv

Machine learning || Full stack development || Data Science geek || Techie