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

Make your code accessible to all

XQ
The Research Nest
3 min readMay 26, 2023

--

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
├── module_name
│ ├── __init__.py
│ ├── module_file1.py
│ └── module_file2.py
├── tests
│ ├── test_module_file1.py
│ └── test_module_file2.py
├── README.md
├── LICENSE
├── setup.py
├── MANIFEST.in
└── .gitignore

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

Step 2: Update the setup.py and MANIFEST.in

Our setup.py file must contain the details of the package as follows;

from setuptools import setup

setup(
name='module_name',
version='1.0.0',
author='Your Name',
author_email='your@email.com',
description='Details about the package',
packages=['module_name', 'module_name.config'],
package_data={
'module_name': ['config/rules.yml'],
},
include_package_data=True,
long_description=open('README.md').read(),
long_description_content_type='text/markdown',
install_requires=[
'dependency1',
'dependency2',
# 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 README.md file in our build by specifying the long-description parameter.

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

include module_name/path/to/config.yml
include README.md

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 setup.py 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.

--

--

XQ
The Research Nest

Exploring tech, life, and careers through content.