Create distributable Python packages with proper project structure, setup.py/pyproject.toml, and publishing to PyPI...
Comprehensive guide to creating, structuring, and distributing Python packages using modern packaging tools, pyproject.toml, and publishing to PyPI.
src/package_name/ (recommended)package_name/ (simpler but less flexible)my-package/
āāā pyproject.toml
āāā README.md
āāā LICENSE
āāā src/
ā āāā my_package/
ā āāā __init__.py
ā āāā module.py
āāā tests/
āāā test_module.py
[build-system]
requires = ["setuptools>=61.0"]
build-backend = "setuptools.build_meta"
[project]
name = "my-package"
version = "0.1.0"
description = "A short description"
authors = [{name = "Your Name", email = "you@example.com"}]
readme = "README.md"
requires-python = ">=3.8"
dependencies = [
"requests>=2.28.0",
]
[project.optional-dependencies]
dev = [
"pytest>=7.0",
"black>=22.0",
]
my-package/
āāā pyproject.toml
āāā README.md
āāā LICENSE
āāā .gitignore
āāā src/
ā āāā my_package/
ā āāā __init__.py
ā āāā core.py
ā āāā utils.py
ā āāā py.typed # For type hints
āāā tests/
ā āāā __init__.py
ā āāā test_core.py
ā āāā test_utils.py
āāā docs/
āāā index.md
Advantages:
pyproject.toml for source layout:
[tool.setuptools.packages.find]
where = ["src"]
my-package/
āāā pyproject.toml
āāā README.md
āāā my_package/
ā āāā __init__.py
ā āāā module.py
āāā tests/
āāā test_module.py
Simpler but:
project/
āāā pyproject.toml
āāā packages/
ā āāā package-a/
ā ā āāā src/
ā ā āāā package_a/
ā āāā package-b/
ā āāā src/
ā āāā package_b/
āāā tests/
Detailed pattern documentation lives in references/details.md. Read that file when the navigation tier above is insufficient.