How to Distribute a Python Module

Initial Project Structure

color
├── README.md
├── __init__.py
├── color.py
└── setup.py

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

Apart from main project color.py, we also need __init__.py and setup.py.

__init__.py

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

setup.py

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

import setuptools

with open("README.md", "r") as fh:
    long_description = fh.read()

setuptools.setup(
    name="color",
    version="0.0.1",
    author="ex10si0n",
    author_email="[email protected]",
    description="A Python Module for rich text output in console",
    long_description=long_description,
    long_description_content_type="text/markdown",
    url="https://github.com/ex10si0n/color.py",
    packages=setuptools.find_packages(),
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
    ],
    python_requires='>=3.5',
)

setup.py can automatically searching subdirectories including __init__.py as package name.

Generating Package

Make sure you have installed dependencies.

pip install --user --upgrade setuptools wheel twine

Generate Project

python setup.py sdist bdist_wheel

Project Structure

color
├── README.md
├── __init__.py
├── __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
├── color.py
├── dist
│   ├── color-0.0.1-py3-none-any.whl
│   └── color-0.0.1.tar.gz
├── screenshot.png
└── setup.py

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 https://test.pypi.org/legacy/ dist/*

Then you can see your packages at https://test.pypi.org/project/color/0.0.1/

Installing test packages

pip install --index-url https://test.pypi.org/simple/ --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/*

Installation

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 *