Skip to content

Checking your reading list…

Retry

Stay curious. Build better.

Appearance

Choose how BestHub looks on this device.

Changes apply immediately.

Search articles

Open Discover with these filters

Suggested searches

How to Build and Publish Your Own Python Library to PyPI

This guide walks you through setting up a Python project structure, creating essential packaging files, organizing modules into packages, building distribution archives, and finally uploading the library to PyPI using Twine, enabling others to install it via pip.

Project Documentation Structure

First create the project layout, including optional LICENSE and README.md files to describe the project and its open‑source license (MIT is common). Then add packaging files: MANIFEST.in to list extra files, setup.py for configuration, and optionally an empty setup.cfg. The main source code resides in a folder (e.g., caafinder) that must contain an __init__.py file.

Typical setup.py options are shown in the following image; refer to the official Python Packaging User Guide for details.

PyPA sample project – an official example library structure.

Python Packaging User Guide – the official documentation for packaging.

About Modules

A single .py file is a module; importing is done with import module_name. To avoid name conflicts, Python uses packages—directories containing an __init__.py file. In this example, the caafinder package contains the database and workspace modules.

Packaging the Library

Run the following command to build distribution files (the exact command may vary):

The command can be one of several options (e.g., python setup.py sdist bdist_wheel).

After building, a dist directory appears containing a name‑version.tar.gz archive ready for upload.

Publishing to PyPI

Create a PyPI account and add a ~/.pypirc file to your home directory to store credentials.

Register a new project on the PyPI website (the first‑time step). Then upload the package using twine (install with pip install twine if needed).

After a successful upload, the package appears on PyPI and can be installed with pip install packagename.

About this article

For questions about attribution, corrections or content rights, please contactadmin@besthub.devand we will review it promptly.

PythonpackagingPyPIlibrary developmentsetup.py

Discussion 0

Questions, practical examples and thoughtful responses.

Open full discussion