Upload Your Python Package to PYPI

Rahul Gupta
4 min readNov 17, 2020

Do you remember how exciting it was when you created your first “Hello World” program in any language? I had the same excitement before uploading my first package to PyPi so that any Python programmer could simply install and use my package like any other regular library (pandas, seaborn etc.).

PyPi (Python Package Index) is a is a software repository for Python programming language. It is used by Python developers to publicize and share their software. The command to install the package that I uploaded to PyPi is shown below.

pip install nearest-square

The full project code is available on my GitHub account

SOME PRE-REQUISITES BEFORE WE START

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

3. You should have installed twine library by the following command.

pip install twine

STEP 1: CREATE AN EMPTY FOLDER TO CONTAIN YOUR PACKAGE

Create an empty folder on any location on your pc, you can use any name because it’s just a container and name of this folder will not be used in the process. Let us make it with the name “Upload-Package-To-Pypi”.

STEP 2: JUMP INSIDE YOUR FOLDER AND WRITE YOUR PYTHON FILES

Jump inside the folder “Upload-Package-To-Pypi folder” and create another folder, this will be your package name. So choose it wisely as I chose “nearest_square” . Make some files inside this folder which you want to upload as your library. After writing my files, the file structure looks like this:

Container Folder
Package Folder
Package Folder

Now I will show all the files one-by-one and what is inside them,

nearest_square.py
__init__.py
setup.cfg

STEP 3: WRITE SOME OFFICIAL FILES

Now we have created enough content for publishing. For that purpose, first of all, come out of “nearest_square” folder. Here I have created a file named “license.txt” which contains a sample text I copied from MIT license.

license.txt
setup.py
Readme.md

STEP 4: COMPILE THE setup.py FILE

Use this command in Upload-Package-To-Pypi folder.

python setup.py sdist

After compilation, you will see some new folders named like shown below

  • dist: Contains your package in a .tar.gz file.
  • nearest_square.egg-info: An egg package contains compiled bytecode, package information, dependency links, and captures the info used by the setup.py test command when running tests.

Now you are ready to upload your package into PyPI.

STEP 5: CREATE AN ACCOUNT IN pypi.org AND test.pypi.org

Why two accounts in two websites?

Because if you are only curious to know how to do it. Then you should work with test.pypi.org because it is the server which is getting used to upload the libraries which are made for educational purpose, curiosity, testing and so on. But for right now use pypi.org

Now finally you have to just fire another command and you are done uploading your package. This command is different for pypi.org and test.pypi.org

For test.pypi.org

twine upload --repository-url https://test.pypi.org/legacy/ dist/*

For pypi.org

twine upload dist/*

STEP 6: INSTALL YOUR PACKAGE ON YOUR PC AND TEST IT.

pip install nearest-square>>> from nearest_square import nearest_square as nsqr
>>> nsqr(101)
100

Congratulations you have published your own python package on PyPI server.

CONCLUSION

I chose a fairly simple module to find nearest square of any given number. Your python package can be as simple or as complex as you want. The objective of this demonstration was to show the setup and configurations required to upload your package to PyPi. Thanks for reading and Happy Learning!

--

--

Rahul Gupta

Director - Technology @ Cadient | Data-driven Leader | Agile evangelist | Business Intelligence | CAPM® | OCP®