Publish Your First Python Package on PyPI

Step by step process of making and publishing your first Python package on PyPI

Vaidhyanathan S M
TheLeanProgrammer
5 min readAug 19, 2021

--

In this tutorial, we are going to learn how to develop a python package and publish it on PyPI. We are going to create a package called number-utility, which makes it simple for you to do number manipulation and perform various operations on numbers. We are going to use Git as a VCS for this project and host it on GitHub.

Let us look at the tasks that are to be completed in this process.

  1. Creating the folder structure of the project
  2. Creating the setup.py file
  3. Creating the Module(s)
  4. Writing Tests and Performing Validation
  5. Creating builds and distribution archives
  6. Choosing a License and providing a README file
  7. Publishing to PyPI repository

Project Folder Structure

First of all, let’s look at the folder structure of the project. Create a folder by the name number_utility. Initialize an empty git repository on your local by executing the following command in the terminal:

git init

Next, create a .gitignore file. Head over to gitignore.io and search for Python and copy the content generated to this .gitignore file. Create two files named setup.py and test_number_utility.py. Create a sub-folder named src inside the root (number_utility) folder and create a new python file called number_utility.py. The project folder structure would look like this :

Project Folder Structure

Do not worry about the files such as LICENSE and MANIFEST.in for now. We’ll look at them in the penultimate step of the process. Now that the folder structure of the project is ready, let’s look at the setup.py file.

Creating the setup.py file

The setup.py is a python file that indicates that the module/package is been packaged and distributed. This allows you to easily install python packages. Let’s define certain properties and parameters using the setuptools package. The setup.py for our project would like this :

You may notice that we have configured a parameter extras_require and have defined some dependencies. These dependencies are required only in a development environment. Now that the setup.py is ready, let’s look at how to create the module.

Creating the module

Head over to number_utility.py. This is the file where we are going to implement the functionalities. As highlighted before, the purpose of this package is to help in number manipulation and perform operations on numbers. We are going to implement functions such as reverse_num(), sum_of_digits(), is_prime(), generate_primes(), get_factors() etc pertaining to operations and number manipulation. These functions are not present as built-in in python. In fact, the very purpose of creating a package is to add missing functionalities. The number_utility.py file would look like this :

number_utility.py

Writing Tests and Performing Validation

Before you move on to test the working of the package, you need to make sure to install the package locally in your machine. Also, make sure to install all the dev dependencies (mentioned in the setup.py). In order to do that, execute the following command in the terminal:

pip install -e .[dev]

Next, head over to test_number_utility.py file. Let’s start writing some tests for the module.

test_number_utility.py

To run the test, execute the command in the terminal:

py.test

After running the tests, you would get an output like this if the tests were successful.

Output after running the tests

This would be a great time for you to push your project to GitHub. Create a README.md file on GitHub. Also, choose an appropriate License template and create your LICENSE file too. Pull those changes into your local.

git add .

git commit -m “Done with Testing”

git push

Then make sure to pull the remote changes to your local machine before proceeding any further.

git pull

Now, let’s proceed with the next steps.

Creating builds and distribution archives

Now that the package is ready and tested, the next step is to create the distribution archives. Execute the following command to create the source distribution.

python3 setup.py sdist

After executing that command, you’d see the output as shown below.

Creating Source Distribution

Let’s test the source distribution generated using the following command in the terminal:

tar tzf dist/number_utility-0.0.3.tar.gz

After the running the above command, you’d notice that the LICENSE and test_number_utility.py files are missing. In order to add those missing files, we need to make use of a package called check-manifest. Execute the following set of commands in the terminal:

pip install check-manifest

check-manifest — create

git add MANIFEST.in

Run the tar command again to check if the missing files were added. Next, we need to create the final build of the package. Execute the following command in the terminal:

python3 setup.py bdist_wheel sdist

Do verify if the distributions are created by listing the files present in the dist folder and also notice a build folder being created. Execute the following command to verify the same.

ls dist/

Publishing to PyPI repository

The package is ready to be published to PyPI. In order to do that, we need to make use of twine. Execute the following command in the terminal:

pip install twine

After that, make sure to create an account on PyPI. Then, execute the following command in the terminal :

twine upload dist/*

It’ll ask for your username and password. And that’s it. Your package is live on PyPI !

We come to the end of this tutorial. If you are interested you might even want to contribute to the GitHub Repository. The link to the same is here. Also, have a look at a package, that’ll be helpful for understanding and implementing Data Structures and Algorithms. The link to the same is here.

If you have any queries please post in the comment section below. Connect with me on LinkedIn. Also, if you want to look at my amazing collection of apps developed, don’t forget to check Google Play Store.

Know more about me here.

Don’t forget to follow The Lean Programmer Publication for more such articles, and subscribe to our newsletter tinyletter.com/TheLeanProgrammer

--

--

Vaidhyanathan S M
TheLeanProgrammer

Systems Engineer @TCS | Native Android Developer | Enthusiastic Programmer | Skilled in Python, C/C++, Java, Flutter and Flask.