How to put your Python lib into PyPi?

Amirhossein Mohamadi
5 min readAug 27, 2021

If you are a Python developer, you know about libraries and modules. Libraries are stored at PyPi and allow you to download and view previous versions. You may have a library that people may like it. So, you need to put it on PyPi. In this post I am going to teach you how to do that.

First of all, it good to put your library in Github that in PyPi will point to main library resources. Create a docs directory to write about it and host it with Github Pages. For this case we will talk about in next post. Anyway let’s start it.

Now create a directory or repository named your library and create required files. For example I will name this simple project like checkme. See the repository tree blew.

Ok, but now the question is, what are those files?

setup.py is the files that create files for uploading to PyPi storage and have repository data. Like author, site and other data. setup.cfg is a cheekily named Python package which supports providing all of a Python distribution’s metadata and build configuration via the setup.cfg file at the base of the distribution’s source tree, rather than in the setup.py script. The standard setup.py script is reduced to a stub which uses the setup. We create a directory with the name as same as the library. In the directory put 2 files. One is __init__.py and other is also again same name as the library. What are they? in checkme.py we put all of the classes, functions. I mean the codes are all in this file. And __init__.py is the file that you import checkme.py in.

What after? Did I make a library? Well, now. You just create files that a simple library needs. However, you did the first step! Let’s go for next level!

As I said, checkme is a simple library and we are just going to say some simple stuff for it. in checkme we will use platform library to create something very small just to check operating system. Open checkme.py and first import platform. As you know platform library has many functions for checking platform or other part of your platform. Like Python version or even Python compiler version too. We will create a function that get an OS name to check your platform and at last, give you True or False.

Before doing that, Lets use iPython to get the point better.

Now we know about it. And also we code it in iPython. Well, Let’s create it. So the checkme.py content will be something like the code below:

Well done! Now setup __init__.py and be ready for last configs in setup.py. __init__.py is so simple. Just write this code in it.

from checkme.checkme import *

In setup.py you just need to import setuptools library and write some codes and run it.

See, this is setup.py and in this post we are not going to use setup.cfg file. But in next posts, why not? Right now, your lib is ready to upload. But first run setup.py to create files for PyPi. Do not run it like python3 setup.py. Run it like this:

python3 setup.py sdist

When you run the file, it will create dist directory and checkme.egg-info. We don’t care about egg files. The main directory that we need for PyPi is dist directory. If you see the files in dist, you will see there is a package named your lib with its version.

At last library is done. Time to upload. But before that, do you have a PyPi account? If not, create it right now. For uploading you can use Twine. Installation of twine is so simple.

pip3 install twine

Ok, now upload your lib. with this command. name-version.tar.gz. Example:

twine upload dist/checkme-0.0.1.tar.gz

Now your project is on PyPi. Just install it via pip!

All done! Nothing more. We put our library in PyPi. Lets check it.

As you can see, checkme works well.

Hope you like this post. Tell us the mistakes or goodies in comments. If you did something like that comment it.

One of the simplest python library is pyis. pyis is a library that uses for checking variables, urls, sites and even url errors. You can contribute on pyis in Github.

Pyis is Github and PyPi.

Until the next time. Have a good time!

Amirhossein.

--

--