top of page

Data Scientist Program


Free Online Data Science Training for Complete Beginners.

No prior coding knowledge required!

Packaging and Publishing a Python Program

This article aims to explain how a Python program can be packaged and published so that other users and developers could access and use it. It will show you how to install the tools required to package a python project, what the files structure should look like, how to build the package and publish it to the Python Package Index, PyPI which is the official third-party software repository for Python.

Packages are a way of structuring Python’s module namespace by using “dotted module names”. For example, the module name X.Y designates a submodule named Y in a package named X.

First lets install the Package Installer of Python, pip with the following commands.

# UNIX/Mac
python3 -m pip install --upgrade pip

# Windows
py -m pip install --upgrade pip

That installs/upgrades your pip to the latest version.

Once we have the tool required in place, lets look at how the file structure must be for our 'example_package' package to be considered a package by python.

└── src/
    └── example_package/
        └── is essential for the folder to be recognized by Python as a package. An empty file makes all functions from the above modules available when this package is imported. Note that you can optionally define functions from individual modules to be made available by importing them in the file.

Now, lets add files that are important for publishing our package to the project's directory.

├── src/
   └── example_package/

Every package published need to have a metadata that describes it.

There are two types of metadata: static and dynamic.

  • Static metadata (setup.cfg): guaranteed to be the same every time. This is simpler, easier to read, and avoids many common errors, like encoding errors.

  • Dynamic metadata ( possibly non-deterministic. Any items that are dynamic or determined at install-time, as well as extension modules or extensions to setuptools, need to go into

Lets now walk through each new file introduced to the project's directory : is the build script for setuptools. It tells setuptools about your package (such as the name and version) as well as which code files to include. The content of this file typically looks like this:

import setuptools

with open("", "r", encoding="utf-8") as fh:
    long_description =

    author="Example Author",
    description="A small example package",
        "Bug Tracker": "",
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
    package_dir={"": "src"},
) : This file acts as the long_description of our package — which we set up in the file.

LICENSE : It’s important for every package uploaded to the Python Package Index to include a license. This tells users who install your package the terms under which they can use your package

Now, it is time to generate a distribution package and publish it. A distribution package is the one that could be published to PyPI and can be installed with pip.

Run the following command to build the package.

# Unix/Mac
python3 -m build
# Windows
py -m build

The following new directory and files will be created after that.

Next, lets build the package.

Finally, it is time to upload our package to PyPI.

The first thing you’ll need to do is register an account on TestPyPI, which is a separate instance of the package index intended for testing and experimentation.

The last tool we need here is twine that can be installed as follows.

python3 -m pip install --upgrade twine
# Windows
py -m pip install --upgrade twine

Once twine is installed, lets upload our package to the test pypi! You will be prompted for a username and password. For the username, use __token__. For the password, use the token value, including the pypi- prefix.

# Unix/Mac
python3 -m twine upload --repository testpypi dist/*
# Windows
py -m twine upload --repository testpypi dist/*

That's pretty much it! We have successfully uploaded our package and it can be installed by pip.


Recent Posts

See All
bottom of page