AI Engineering
Production AI architecture and engineering practices.
By Yangming Li
Packaging your Python code is the best way to share, reuse, and distribute your work. This guide walks you through every step of building, testing, documenting, and publishing a modern Python package, following best practices and official recommendations.
Start with a clean directory layout. The recommended structure is:
my_package/
├── src/
│ └── my_package/
│ ├── __init__.py
│ └── core.py
├── tests/
├── pyproject.toml
├── setup.py # optional but helpful
├── README.md
└── LICENSE
This structure ensures your package is importable, testable, and future-proof.
In core.py, define your main functions:
def add(a, b):
return a + b
Expose them in __init__.py:
from .core import add
__all__ = ["add"]
This allows users to import directly: from my_package import add
Modern (recommended):
[build-system]
requires = ["setuptools>=42", "wheel"]
build-backend = "setuptools.build_meta"
[project]
name = "my-package"
version = "0.1.0"
description = "Example Python package"
readme = "README.md"
requires-python = ">=3.7"
dependencies = []
Legacy (setup.py):
from setuptools import setup, find_packages
setup(
name="my_package",
version="0.1.0",
packages=find_packages("src"),
package_dir={"": "src"},
install_requires=[],
)
Use pyproject.toml for modern, future-proof packaging.
Add tests in tests/ using pytest or unittest:
def test_add():
from my_package import add
assert add(2, 3) == 5
Testing ensures your package works as expected and is essential for CI/CD.
dist/, *.egg-info/).Install build tools and create a distribution:
pip install wheel build twine
python -m build
Upload to PyPI or TestPyPI:
# Test PyPI
python -m twine upload --repository testpypi dist/*
# Production
python -m twine upload dist/*
Once published, users can install with pip install my-package.
CHANGELOG.md.A simple GitHub Actions workflow for testing and publishing:
# .github/workflows/python-package.yml
name: Python Package
on:
push:
tags:
- 'v*'
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.x'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install pytest
pip install -e .
- name: Test with pytest
run: pytest
publish:
needs: test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.x'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install build twine
- name: Build and publish
env:
TWINE_USERNAME: ${{ secrets.PYPI_USERNAME }}
TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }}
run: |
python -m build
twine upload dist/*
By following these steps and referencing the official guides, you can confidently create, test, document, and release a Python package from scratch. Happy packaging!