How to Distribute a Python Module

Initial Project Structure


Python package is a folder that contains at least 3 .py scripts as followed.

Apart from main project, we also need and

This script runs initially when being imported. This script as a shorthanded way when somebody import your_module_name, just take a look at mine.

from .color import color

If there are more classes, you may need to import in this way.

from mypackage.subpackage_1 import some_class_a
from mypackage.subpackage_1 import some_class_b
from mypackage.subpackage_2 import some_class_c
from mypackage.subpackage_2 import some_class_d
from mypackage.subpackage_3 import some_class_e
from mypackage.subpackage_3 import some_class_f

This script is the most important to distribute your module to PyPI. It’s a such kind of declaration, a template of is provided as followed.

import setuptools

with open("", "r") as fh:
    long_description =

    author_email="[email protected]",
    description="A Python Module for rich text output in console",
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
) can automatically searching subdirectories including as package name.

Generating Package

Make sure you have installed dependencies.

pip install --user --upgrade setuptools wheel twine

Generate Project

python sdist bdist_wheel

Project Structure

├── __pycache__
│   ├── __init__.cpython-38.pyc
│   ├── __init__.cpython-39.pyc
│   ├── color.cpython-38.pyc
│   └── color.cpython-39.pyc
├── build
│   └── bdist.macosx-11.0-arm64
├── color.egg-info
│   ├── PKG-INFO
│   ├── SOURCES.txt
│   ├── dependency_links.txt
│   └── top_level.txt
├── dist
│   ├── color-0.0.1-py3-none-any.whl
│   └── color-0.0.1.tar.gz
├── screenshot.png

Upload Packages to Test-PyPI

Test-PyPI is for testing before distribution. After registering and verifying E-mail address, you are able to upload any python module to Test-PyPI

Using twine upload directories

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

Then you can see your packages at

Installing test packages

pip install --index-url --no-deps color

Publishing Packages

PyPI is stored permanently and cannot be modified once it is released, only can iterate new versions, in addition to ensuring that the package name cannot be renamed with the already released package.

Distributing with twine

python -m twine upload dist/*


pip install colorfulconsole

2 thoughts on “How to Distribute a Python Module”

Leave a Reply

Your email address will not be published. Required fields are marked *