How to Publish Your Python Code as a Pip Package in 5 Simple Steps

Make your code accessible to all

The Research Nest
Published in
3 min read5 days ago


Photo by Hitesh Choudhary on Unsplash

I have been in the software engineering industry for almost four years now. Interestingly, I never published any Python package before.

It always remained elusive, and finally, I decided to explore how to do it by converting the tagging_module I created for Techflix into a standalone Python package. Hopefully, this documentation helps other tech enthusiasts to publish their own code as an easily accessible pip package.

Here are the general steps to convert your code into a package.

Step 1: Structure your folder correctly

Given that we want to make our code as a module, it is good to follow some best practices and create proper folder structure and classes as required.

Here’s an example file structure

├── module_name
│ ├──
│ ├──
│ └──
├── tests
│ ├──
│ └──
└── .gitignore

The code for the package actually sits in the subfolder by the same name as the root folder. The file here makes the folder into a package.

Step 2: Update the and

Our file must contain the details of the package as follows;

from setuptools import setup

author='Your Name',
description='Details about the package',
packages=['module_name', 'module_name.config'],
'module_name': ['config/rules.yml'],
# List any other dependencies your module requires

Remember to update the version number every time you make a new change and rebuild the project. Also, note how any default config files used in the package (like .yml) are specified in the setup file.

In the package_data parameter, we specify the package name and the relative path to the config.yml file (or whatever name you give it). The include_package_data=True option ensures that the package data is included during the build process. Similarly, we can include a file in our build by specifying the long-description parameter.

Additionally, create a file named in the root directory of your project (same level as if it doesn’t exist. This file should contain the following;

include module_name/path/to/config.yml

Step 3: Build the package

We need to create the necessary files that can actually be uploaded to the Python package index (PyPI). We can run the below command from our root folder to do that.

python sdist bdist_wheel

This command creates a dist directory and generates the source distribution (*.tar.gz) and wheel distribution (*.whl) files for your package.

Step 4: Upload the package to PyPI

You must create an account on PyPI and then use a tool like Twine to upload your package. Install twine if you haven’t already:

pip install twine

Once installed, navigate to the dist directory and run the following command to upload your package. It will prompt you to enter your PyPI username and password and then proceed with the upload.

twine upload *

Step 5: Check that the package is uploaded successfully

We can now verify that we are able to install and use our module via pip.

pip install module_name

That’s it! Your package is ready. Congratulations!

I followed the exact steps as above to get the tagging_module published.

I have open-sourced the same. Here’s the link to the corresponding Github repo.

One thing to note- make sure that you have some documentation on how to use the package. Good documentation is what will help with the discovery and adoption of your package by the broader developer community.

If you are hosting your code on Github, it is possible to automate this process to update your package every time you push a change to your repository via a CI/CD pipeline using tools like Github actions.

I may explore it in a future article.



The Research Nest

Tech 👨‍💻 | Life 🌱 | Careers 👔 | Poetry 🖊️