How to Publish Your Python Code as a Pip Package in 5 Simple Steps
Make your code accessible to all
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
│ ├── __init__.py
│ ├── module_file1.py
│ └── module_file2.py
│ ├── test_module_file1.py
│ └── test_module_file2.py
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
description='Details about the package',
# 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;
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 install module_name
That’s it! Your package is ready. Congratulations!
I followed the exact steps as above to get the
I have open-sourced the same. Here’s the link to the corresponding Github repo.
GitHub - aditya-xq/tagging_module: A lightweight rule based tech content tagging module for Python
A lightweight rule based tech content tagging module for Python The TaggingService class is designed to provide a…
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.