All Articles

How to publish your Python Package to pip

pip install

Here is a step-by-step guide to follow to publish your Python package to PyPI, so anyone interested can download your package using the ‘pip install ’ command. By making it available on PyPI, you are essentially making your library open source.

This article will not focus on developing the python package, but it will be a guide to publish the package in PyPI.

If you want to read about my first Open Source Python Library - PyCalendly, check out my article I Published my first opensource python library.

Step 1: Naming your package

Naming your package is the important stuff. As of this writing, there are over 313,035 packages available in PyPI. Select a unique name for your package so potential users can find your package without any hustle.

Selecting a Package name is like selecting a domain name. Make sure it is easy to remember and unique.

Tip: You can use the search option in to check if the name that you are interested in is available.

Step 2: Create package configuration files.

A Python package is expected to have a few configuration files set up before publishing it to PyPI.

| |-
| |- <src_files>.py
|--tests/* :

Create a README file that explains your package, functionality and link them to the proper documentation or guide to use the package. You can also include contribution guidelines and issues tracker mechanisms in the README.

Here’s a template that I found interesting that you can make use of - README Template.


Since you are making your package Open Source, you need to include a License in your project. When it comes to License, there are multiple options, carefully choose one that fits your needs.

It might seem overwhelming initially understanding these different Licenses, here’s a tool that can help you zero in on a LICENSE suitable for your project - Choose a License. :

You can use the following template for your and edit more as you proceed to advanced functionalities.

import setuptools

long_desc = open("").read()
required = [<Dependent Packages>] # Comma seperated dependent libraries name

    version="<VERSION_NUMBER>", # eg:1.0.0
    author="<YOUR NAME>",
    author_email="<YOUR EMAIL>",
    description="<SHORT DESCRIPTION>",
    packages = ['<YOUR_PACKAGE_NAME>'],
    # project_urls is optional
        "Bug Tracker": "<BUG_TRACKER_URL>",
    key_words="<KEY WORDS>",

pyproject.toml :

pyproject.toml informs the build tools about the requirements to build our package. Here’s a template that you can customize to your needs

requires = [
build-backend = "setuptools.build_meta"

tests (directory):

Create an empty tests directory for now. You can research on PyTests and how to write tests later.

Step 3: Create accounts in Test PyPI and PyPI.

Both the interfaces(Test PyPI and PyPI) look very similar but remember they are two independent platforms. As the name suggests, Test PyPI is what you use to test your package, and PyPI is where you publish it to the world.

Register on both platforms independently (You can follow the links below to set up a new account)\

Step 4: Set up API tokens for both Test PyPI and PyPI.

Login to PyPI/Test PyPI, navigate to ‘Account settings’, scroll down till you can see the ‘API Tokens’ section. Create a new token.

After creating a new token, you next need to create the .pypirc file in your machine. Create a file ’.pypirc’ in the path: “$HOME/.pypirc”.

(Note: HOMEinwindowsisC:/Users/<username>.Inmac,HOME in windows is *C:/Users/<username>.* In mac,HOME is the user folder)

Replace the and with the API tokens that you have created before.

username = __token__
password = <PyPI token>

username = __token__
password = <TestPyPI token>

Step 5: Build distribution archives

Next, we generate distribution archives. These are the files that will be uploaded to the PyPI.

python -m pip install --upgrade build

python -m build

Once these commands run successfully, it will generate distribution archive files under the dist/ folder inside the project.

Step 6: Upload archives to Test PyPI

To upload the archives to the Package Index(PI), we will make use of the twine library.

If you haven’t installed twine yet, install using the command: python -m pip install --upgrade twine

Once twine is installed, we upload the archives by executing the following command

python -m twine upload --repository testpypi dist/*

If everything goes well and the execution is complete, you can see your package on the site.

You can then test the installation and functionality by creating a new virtual environment and installing the package from Test PyPI.

Install a package from Test PyPI: python -m pip install --index-url your-package

Step 7(Final!): Uploading to PyPI

Uploading to PyPI is similar to Test PyPI.

execute the following command:

python -m twine upload dist/*

Once the upload is successful, you can see your package on the PyPI website. You can navigate to ‘your projects’ in PyPI and find the uploaded package there.

Now the library is available to the world. Anyone can install your library by running the following command.

pip install <your_package_name>

Bonus: Updating your package

If you make some changes, add some new features, and want to release a new version of the package, follow the steps as follows.

- Update the version number in the file

- Repeat Step5 to Step7 again. Check if the dist folder is properly generated after Step 5.

This will update the package, and the latest version can be seen by the users.

Reference: - Packaging Projects - .pypirc file