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.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
How to Build and Publish Your Own Python Library to PyPI

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.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonpackagingPyPILibrary Developmentsetup.py
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.