Advanced Lesson
Packaging & Publishing Python Libraries to PyPI
Learn how to turn your Python code into a real, installable package that anyone can use with pip install.
๐ฆ What You'll Learn
This lesson teaches you how to publish professional Python packages:
- Structuring a library properly
- Creating pyproject.toml configuration
- Building wheels and source distributions
- Uploading to TestPyPI and PyPI with twine
- Versioning and metadata best practices
- Making your package discoverable and trustworthy
After this lesson, anyone can install your code with:
pip install your-library-name๐ฅ Python Download & Setup
Download Python from: python.org/downloads
Latest version recommended (3.11+)
Part 1: Package Structure & Building
1. What Is a Python Package?
๐ฆ Real-World Analogy:
Think of a package like a product in a box. The box (folder) contains everything needed: the product itself (your code), instructions (README), warranty info (LICENSE), and a label with specs (pyproject.toml). Anyone can "buy" your product with pip install!
A package is just a directory with an __init__.py file that can be imported.
Example structure:
Package Structure
my_cool_lib/
โโ src/
โ โโ my_cool_lib/
โ โโ __init__.py
โ โโ core.py
โ โโ utils.py
โโ tests/
โโ pyproject.toml
โโ README.md
โโ LICENSE
โโ .gitignore| File/Folder | Purpose | Required? |
|---|---|---|
src/my_cool_lib/ | Your actual library code | โ Yes |
pyproject.toml | Build config & metadata | โ Yes |
README.md | Documentation (shown on PyPI) | โ Highly recommended |
LICENSE | Usage permissions | โ Highly recommended |
tests/ | Unit tests | Optional but best practice |
The src/ layout is recommended because it catches import mistakes early.
2. Basic Code Layout
Minimal library example:
src/my_cool_lib/__init__.py:
__init__.py
__all__ = ["say_hello", "__version__"]
from .core import say_hello
__version__ = "0.1.0"src/my_cool_lib/core.py:
core.py
def say_hello(name: str) -> str:
return f"Hello, {name}!"Users will be able to do:
Usage Example
from my_cool_lib import say_hello
print(say_hello("World"))3. The Modern Way: pyproject.toml
pyproject.toml is now the standard way to describe:
- Project metadata (name, version, description)
- Build backend (like setuptools)
- Dependencies
Minimal example using setuptools:
pyproject.toml
[build-system]
requires = ["setuptools>=61.0", "wheel"]
build-backend = "setuptools.build_meta"
[project]
name = "my-cool-lib"
version = "0.1.0"
description = "A tiny example Python library."
readme = "README.md"
requires-python = ">=3.9"
license = { text = "MIT" }
authors = [
{ name = "Jane Developer", email = "jane@example.com" }
]
dependencies = [
"requests>=2.31.0",
]
[project.urls]
"Homepage" = "https://github.com/yourname/my-cool-lib"
"Bug Tracker" = "https://github.com/yourname/my-c
...Key points:
nameโ must be unique on PyPIversionโ follows semantic versioning (e.g. 0.1.0)requires-pythonโ minimum Python version you supportdependenciesโ packages installed when someone installs your library
4. Semantic Versioning (How to Version Properly)
๐ฏ Real-World Analogy:
Think of versions like software updates on your phone. A PATCH (15.0.1 โ 15.0.2) fixes bugs quietly. A MINOR update (15.1) adds features. A MAJOR update (15 โ 16) might change everything and require you to relearn things โ that's a breaking change.
Use Semantic Versioning:
MAJOR.MINOR.PATCH
e.g. 1.4.2
| Part | When to Bump | Example |
|---|---|---|
| PATCH | Bug fixes, no breaking changes | 0.1.0 โ 0.1.1 |
| MINOR | New features, backwards compatible | 0.1.1 โ 0.2.0 |
| MAJOR | Breaking changes | 0.2.0 โ 1.0.0 |
Store the version in one place (e.g. __init__.py) and keep pyproject.toml in sync or use a tool like setuptools-scm later.
5. Writing a Good README for PyPI
Your README.md becomes the landing page on PyPI.
Include:
- Short description
- Installation instructions
- Basic usage example
- Features list
- Links (docs, repo, issues)
Example skeleton:
README.md Template
# my-cool-lib
A small Python library that says hello.
## Installation
```bash
pip install my-cool-lib
```
## Quick Start
```python
from my_cool_lib import say_hello
print(say_hello("World")) # โ "Hello, World!"
```
## Features
- Simple API
- Type hints
- Tested with pytestMake sure readme = "README.md" is set correctly in pyproject.toml.
6. Building the Distribution (Wheel + sdist)
๐ Real-World Analogy:
Building a package is like baking bread. sdist is like shipping the recipe and ingredients โ the customer bakes it themselves. Wheel is like shipping the finished loaf โ ready to eat immediately. Most people prefer the wheel (faster to install)!
Install build tools:
Install Build
pip install buildThen from your project root (same folder as pyproject.toml):
Build Package
python -m buildThis will create:
Build Output
dist/
โโ my_cool_lib-0.1.0.tar.gz # Source distribution (sdist)
โโ my_cool_lib-0.1.0-py3-none-any.whl # Wheel (built package)| Type | File Extension | Install Speed | When Used |
|---|---|---|---|
| Wheel | .whl | โก Fast | Default installation |
| sdist | .tar.gz | ๐ข Slower | Fallback, building from source |
7. Uploading to TestPyPI (Safe Practice Run)
Before using the real PyPI, publish to TestPyPI.
Install twine:
Install Twine
pip install twineUpload to TestPyPI:
Upload to TestPyPI
twine upload --repository-url https://test.pypi.org/legacy/ dist/*You'll be asked for:
- username: often
__token__if using API tokens - password: your TestPyPI API token
Then test install it in a fresh venv:
Test Install from TestPyPI
python -m venv test-env
source test-env/bin/activate # or .\test-env\Scripts\activate on Windows
pip install -i https://test.pypi.org/simple/ my-cool-libCheck it works:
Verify Installation
from my_cool_lib import say_hello
print(say_hello("World"))If everything looks good, you're ready for real PyPI.
8. Publishing to the Real PyPI
- Create an account on pypi.org
- Generate an API token
- Configure ~/.pypirc (optional but helpful):
.pypirc Configuration
[distutils]
index-servers =
pypi
[pypi]
username = __token__
password = pypi-xxxxxxxxxxxxxxxxxxxxxxxxxxxxThen upload:
Upload to PyPI
twine upload dist/*After a successful upload, users can:
User Installation
pip install my-cool-libAnd import it like any other library.
9. Updating Your Package (New Versions)
When you change the code:
- Bump the version (e.g. 0.1.0 โ 0.1.1) in:
- pyproject.toml
- __init__.py (if you mirror the version there)
- Rebuild:
python -m build - Upload again:
twine upload dist/*
PyPI will reject duplicate versions, so you must use a new version number each time.
10. Minimal Checklist Before Publishing
Part 2: Advanced Package Features
11. Adding Optional Features With "Extras"
Sometimes you want optional dependencies that users can install only if they need them.
Example: a core library with an optional cli or dev feature:
Optional Dependencies
[project.optional-dependencies]
cli = [
"typer>=0.12.0",
"rich>=13.0.0",
]
dev = [
"pytest>=8.0.0",
"mypy>=1.10.0",
"ruff>=0.6.0",
]Users can then install:
Installing Extras
pip install my-cool-lib[cli]
pip install my-cool-lib[dev]This keeps the base install lightweight, while still offering powerful extras for people who want them.
12. Entry Points & Console Scripts (Installing a CLI)
You can ship a command-line tool with your package so users get a global command after installing.
In pyproject.toml:
Console Script Entry Point
[project.scripts]
mycool = "my_cool_lib.cli:main"Then create:
CLI Module
# src/my_cool_lib/cli.py
def main() -> None:
print("Hello from mycool CLI!")After installing:
Using the CLI
pip install my-cool-lib
mycool
# โ Hello from mycool CLI!This is how black, pytest, pip, etc. expose commands.
13. Classifiers: Telling PyPI & Tools What Your Package Supports
Classifiers inform:
- Supported Python versions
- Intended audience
- License
- Topic (e.g. web, ML, data)
In pyproject.toml:
Classifiers
[project]
classifiers = [
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.10",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
"Intended Audience :: Developers",
"Topic :: Software Development :: Libraries",
]This improves:
- Discoverability on PyPI
- Confidence for users (they can see support clearly)
- Filtering in tools / searches
14. Handling Dependencies Safely
Some tips for dependencies:
1. Pin or semi-pin versions in development, but keep install requirements slightly relaxed.
Version Constraints
[project]
dependencies = [
"requests>=2.31,<3.0",
]In your own dev environment you can use a lock file from tools like pip-tools, Poetry, or uv.
2. Avoid unnecessary dependencies
Lean packages are:
- Easier to install
- Less likely to break
- More secure
3. Put dev-only tools into optional dev extras, not into main dependencies.
15. Testing Before Publishing
Before uploading to PyPI, always run tests in a clean environment.
Example with pytest:
Testing Before Publishing
python -m venv .venv
source .venv/bin/activate # Windows: .venv\Scripts\activate
pip install -e ".[dev]" # editable install + dev extras
pytest-eโ editable install (imports code directly from src/).[dev]โ includes dev dependencies from optional-dependencies
If tests pass in a clean venv, chances are much higher that users won't hit import errors.
16. Testing Installation Like a Real User
After building and uploading to TestPyPI, test your package the way a real user would:
- Create a fresh venv:Try it Yourself ยป
Create Fresh Venv
Pythonpython -m venv test-env source test-env/bin/activate - Install from TestPyPI:Try it Yourself ยป
Install from TestPyPI
Pythonpip install -i https://test.pypi.org/simple/ my-cool-lib - Open Python and try:Try it Yourself ยป
Test Import
Pythonfrom my_cool_lib import say_hello print(say_hello("World"))
If that works, your packaging, imports, and metadata are all aligned correctly.
17. Common Packaging Mistakes (and How to Avoid Them)
a) Forgetting __init__.py
If __init__.py is missing, Python won't treat the directory as a package.
Required __init__.py
src/
my_cool_lib/
__init__.py โ must exist
core.pyb) Wrong where for packages
If you use the src/ layout, you must tell setuptools:
Package Discovery
[tool.setuptools.packages.find]
where = ["src"]Otherwise your built wheel may contain no code, and imports will fail.
c) Importing the package from the project root during dev
Better approach:
- Use
pip install -e .and run scripts viapython -m my_cool_lib.something, or - Put your scripts under src/ and run through modules
d) Uploading the same version twice
PyPI will reject re-uploads of a version. If you made a mistake:
- Bump the version (e.g. 0.1.0 โ 0.1.1)
- Rebuild with
python -m build - Upload again with twine
18. Keeping Secrets Safe (Do Not Hardcode Keys)
Never put secrets (tokens, passwords) in your package.
Bad:
Bad: Hardcoded Secret
API_KEY = "sk-123456"Instead:
- Use environment variables (
os.environ["MY_API_KEY"]) - Or read from config files that are not in the published package
- Or let users pass keys into your functions/classes
Anything inside src/ will be visible to anyone on PyPI or GitHub, so treat it as public.
19. Automating Publishing With CI (Overview)
Later on, you can automate releases using CI like GitHub Actions.
General flow:
- Tag a release (e.g. v0.2.0)
- CI workflow runs:
- installs dependencies
- runs tests
- builds package (
python -m build) - uploads with twine using a PyPI token stored as a secret
Very rough example .github/workflows/release.yml structure:
GitHub Actions Workflow
name: Publish to PyPI
on:
push:
tags:
- "v*.*.*"
jobs:
build-and-publish:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.11"
- run: pip install build twine
- run: python -m build
- run: twine upload dist/*
env:
TWINE_USERNAME: __token__
TWINE_PASSWORD: ${{ secrets.PYPI_API_TOKEN }}After this, releasing is as simple as pushing a tag.
20. Making Your Package Friendly for Contributors
A polished package is easier for others to use and contribute to.
Good practices:
- Include a CONTRIBUTING.md explaining:
- how to set up dev environment
- how to run tests
- coding style or lint rules
- Add a simple Makefile or tasks.py with shortcuts
- Use a linter/formatter (ruff, black, isort) and mention them in dev extras
21. Summary of the Packaging Workflow
Full high-level flow:
- Create Structure
Use src/your_package/ layout. Add __init__.py, modules, tests
- Describe Project
Create pyproject.toml with metadata, dependencies, optional extras, scripts
- Write Code + Tests
Implement core logic. Add unit tests with pytest
- Build
python -m build
- Test on TestPyPI
Upload with twine to TestPyPI. Install in a clean venv. Import and run sample code
- Publish to PyPI
Configure PyPI token. twine upload dist/*
- Repeat with New Versions
Bump version for each change. Rebuild and re-upload
Part 3: Professional Polish & Best Practices
22. Handling Versioning Properly (Semantic Versioning Made Simple)
PyPI expects clear, predictable versioning. The best standard is SemVer:
MAJOR.MINOR.PATCH
- MAJOR โ breaking changes
- MINOR โ new features, no breaks
- PATCH โ bug fixes
Examples:
- 1.0.0 โ first stable release
- 1.1.0 โ added new features
- 1.1.5 โ bug fixes only
23. Cross-Platform Compatibility
Make your package work on Windows, macOS, and Linux.
Use pathlib for paths:
Cross-Platform Paths
from pathlib import Path
config = Path.home() / ".mycoolconfig"This works everywhere.
24. Adding Type Hints for Better DX (Developer Experience)
Typed packages are dramatically easier to use.
You can add:
- Inline type hints
- Or a separate py.typed file to mark package typing support
Add inside: src/my_cool_lib/py.typed
And declare in pyproject.toml:
Typed Package Declaration
[tool.setuptools.package-data]
"my_cool_lib" = ["py.typed"]This lets editors like VSCode, PyCharm & MyPy give:
- Autocomplete
- Type checking
- Safer refactors
Typed libraries are considered higher-quality on PyPI.
25. Adding Documentation (README, Examples, Tutorials)
A strong PyPI project must have:
- A clear README
- Installation instructions
- Usage examples
- API documentation
README.md can include:
- Badges (version, downloads)
- Examples
- Feature list
- Contribution guide
- License
PyPI will display this directly on the project page.
26. Adding a License (Important for Public Use)
If you publish to PyPI without a license, companies legally cannot use your code.
Popular options:
- MIT License (very permissive)
- Apache 2.0 (enterprise-friendly)
- GPL (viral, requires open sourcing forks)
Add a LICENSE file at project root and reference it in pyproject.toml:
License Declaration
[project]
license = { file = "LICENSE" }Open-source developers expect this.
27. Keeping Your Package Secure
PyPI packages must remain safe. Follow these guidelines:
โ ๏ธ Never include:
- Secrets
- API keys
- OAuth tokens
- Database passwords
- Embedded credentials
โ ๏ธ Avoid risky patterns:
- Using eval()
- Arbitrary imports based on user input
- Downloading code from the internet
โ Use safety tools:
Security Tools
pip install bandit
bandit -r src/
pip install safety
safety checkSecurity matters for your reputation and your package's adoption.
28. Distributing Both sdist and Wheel (Why It Matters)
When you run python -m build, you get two types of packages:
mycoollib-1.0.0.tar.gzโ source distributionmycoollib-1.0.0-py3-none-any.whlโ wheel
Why both?
- sdist = source code (for platforms that build from scratch)
- wheel = pre-built, faster installs
Wheels install instantly and are preferred on modern Python.
29. Supporting Multiple Python Versions
You choose which Python versions your package supports.
In pyproject.toml:
Python Version Requirement
[project]
requires-python = ">=3.9"Then use:
- tox
- or nox
to test on multiple interpreters. This makes your package more stable and predictable for users.
30. Writing a Professional __init__.py
Your package's public API should be explicitly controlled.
Example:
Professional __init__.py
from .core import greet, add
from .utils import logger
__all__ = ["greet", "add", "logger"]Benefits:
- Clear public interface
- Cleaner imports for users
- No accidental API exposure
Good packages have well-designed import paths.
31. Packaging Non-Python Files (Assets, Templates, etc.)
Some libraries include:
- Config templates
- JSON/YAML files
- Static resources
- HTML templates
Use:
Package Data
[tool.setuptools.package-data]
"my_cool_lib" = ["templates/*.html", "data/*.json"]Then access them:
Accessing Package Data
from importlib.resources import files
template = files("my_cool_lib").joinpath("templates/index.html").read_text()This is cleaner than bundling file paths manually.
32. Popular Tools That Improve the Publishing Workflow
Recommended add-ons:
- black โ automatic formatting
- ruff โ ultra-fast linting
- mypy โ typing enforcement
- pre-commit โ hooks to auto-format before commit
- uv/Pipenv/Poetry โ dependency managers
These make your package "production-grade."
33. Final Practical Checklist Before Publishing
Here is the final checklist used by professional Python maintainers:
If all boxes are checked โ ship it.
๐ Final Summary
You've now mastered professional Python package publishing to PyPI.
You can now:
- Structure a library with src/ layout
- Configure pyproject.toml with all metadata
- Build wheels and source distributions
- Test on TestPyPI before publishing
- Publish to PyPI with twine
- Version properly with semantic versioning
- Add optional extras and CLI tools
- Keep packages secure and cross-platform
- Automate releases with CI/CD
These skills let you publish professional Python libraries used by developers worldwide โ just like requests, FastAPI, and thousands of other packages on PyPI.
๐ Quick Reference โ Packaging & PyPI
| Tool / File | What it does |
|---|---|
| pyproject.toml | Modern project metadata and build config |
| python -m build | Build wheel and sdist |
| twine upload dist/* | Upload to PyPI |
| twine check dist/* | Validate package before upload |
| pip install -e . | Install package in editable/dev mode |
๐ Great work! You've completed this lesson.
You can now package your Python code and publish it to PyPI โ making your work installable by anyone in the world with pip.
Up next: Files & Streams โ work with large datasets, binary files, and efficient I/O patterns.
Sign up for free to track which lessons you've completed and get learning reminders.