Introduction
Packaging and Publishing Your Own Python Library. Have you ever written a Python module or utility function that you reused across different projects? Wouldn’t it be awesome if others could benefit from your code too—just by installing it via pip? That’s the power of packaging and publishing your own Python library.
Creating and distributing your own library not only helps the Python community but also enhances your portfolio as a developer. In this blog, we’ll walk through the entire process of creating, packaging, and publishing a Python library to PyPI, the official Python Package Index. Whether you’re a beginner or an intermediate developer, by the end of this guide, you’ll be ready to share your code with the world.
Table of Contents
Why Publish a Python Library?
Before diving into the how-to, let’s talk about why you might want to publish a library:
- Reusability: Avoid repeating code across multiple projects.
- Community Contribution: Help others who face the same problem.
- Open Source Credibility: Strengthens your GitHub and resume profile.
- Learning Opportunity: Understand Python internals, packaging standards, and dependency management.
Prerequisites
Before starting, ensure you have the following:
- Python 3.6 or higher
- pip installed
- An account on PyPI
- A terminal/command prompt
- Basic knowledge of Python
Step 1: Create Your Library Project Structure
Let’s say we want to create a library named mymath that contains simple mathematical functions.
Here’s a recommended folder structure:
mymath/
│
├── mymath/
│ ├── __init__.py
│ └── operations.py
│
├── tests/
│ └── test_operations.py
│
├── LICENSE
├── README.md
├── pyproject.toml
├── setup.cfg
└── setup.py
Let’s break it down:
mymath/: Package folder with your code.__init__.py: Makes it a package.operations.py: Contains your math functions.tests/: Unit tests for your package.README.md: Documentation.setup.py,setup.cfg,pyproject.toml: Configuration for building and installing.
Step 2: Write Your Python Code
Create a simple math function inside mymath/operations.py:
# mymath/operations.py
def add(a, b):
return a + b
def subtract(a, b):
return a - b
Then import it in your __init__.py so users can access it directly:
# mymath/__init__.py
from .operations import add, subtract
__all__ = ["add", "subtract"]
Step 3: Add a README File
Create a README.md to explain what your library does:
# mymath
A simple Python library for basic arithmetic operations.
## Installation
```bash
pip install mymath
Usage
from mymath import add, subtract
print(add(5, 3)) # Output: 8
print(subtract(5, 3)) # Output: 2
A good README makes your project more usable and professional.
---
## Step 4: Create the setup.py File
This is the heart of your package configuration:
```python
# setup.py
from setuptools import setup, find_packages
setup(
name='mymath',
version='0.1.0',
description='A simple math operations library',
long_description=open('README.md').read(),
long_description_content_type='text/markdown',
author='Your Name',
author_email='you@example.com',
url='https://github.com/yourusername/mymath',
packages=find_packages(),
classifiers=[
'Programming Language :: Python :: 3',
'License :: OSI Approved :: MIT License',
],
python_requires='>=3.6',
)
Step 5: Add pyproject.toml and setup.cfg (Optional but Recommended)
pyproject.toml:
[build-system]
requires = ["setuptools>=42", "wheel"]
build-backend = "setuptools.build_meta"
setup.cfg:
[metadata]
name = mymath
version = 0.1.0
description = A simple math operations library
long_description = file: README.md
long_description_content_type = text/markdown
author = Your Name
author_email = you@example.com
url = https://github.com/yourusername/mymath
license = MIT
classifiers =
Programming Language :: Python :: 3
License :: OSI Approved :: MIT License
[options]
packages = find:
python_requires = >=3.6These files help standardize builds and support modern Python packaging.
Step 6: Add a License
Add a file named LICENSE with your chosen license (MIT, GPL, etc). For example, MIT License:
MIT License
Copyright (c) 2025 Your Name
Permission is hereby granted, free of charge, to any person obtaining a copy...
Step 7: Write Tests
Use pytest or Python’s built-in unittest:
# tests/test_operations.py
from mymath import add, subtract
def test_add():
assert add(2, 3) == 5
def test_subtract():
assert subtract(5, 3) == 2
Run tests with:
pytest
Step 8: Build Your Package
Install required tools:
pip install build twine
Build the distribution:
python -m build
You’ll get two files inside a dist/ directory:
mymath-0.1.0.tar.gzmymath-0.1.0-py3-none-any.whl
Step 9: Upload to PyPI
Register at PyPI
Then, upload your package using twine:
python -m twine upload dist/*
It will prompt you for your PyPI username and password.
If successful, your library will be live on:https://pypi.org/project/mymath/
You can now install it anywhere:
pip install mymath
Tips for a Better Library
- Use semantic versioning (e.g., 1.0.0)
- Keep dependencies minimal
- Add a CHANGELOG.md for updates
- Document your code using docstrings
- Include type hints for modern tooling
- Add a
.gitignoreto avoid pushing unnecessary files
Automate Publishing (Optional)
Use GitHub Actions or similar CI/CD tools to auto-publish new versions on tag pushes. Learn more from Python Packaging Authority’s CI/CD guide.
Useful Links
Conclusion
Publishing a Python library is easier than it seems. Once you understand the directory structure and configuration files, you can create, share, and maintain libraries that the community—or even just future-you—will thank you for.
This tutorial covered everything from building a simple Python module to uploading it to PyPI. You’ve now joined a league of developers who contribute to open-source and make Python an even more powerful ecosystem.
So what will your next library be?
Find more Python content at: https://allinsightlab.com/category/software-development
