1
---
2
name: python-packaging
3
description: Create distributable Python packages with proper project structure, setup.py/pyproject.toml, and publishing to PyPI. Use when packaging Python libraries, creating CLI tools, or distributing Python code.
4
---
5
6
# Python Packaging
7
8
Comprehensive guide to creating, structuring, and distributing Python packages using modern packaging tools, pyproject.toml, and publishing to PyPI.
9
10
## When to Use This Skill
11
12
- Creating Python libraries for distribution
13
- Building command-line tools with entry points
14
- Publishing packages to PyPI or private repositories
15
- Setting up Python project structure
16
- Creating installable packages with dependencies
17
- Building wheels and source distributions
18
- Versioning and releasing Python packages
19
- Creating namespace packages
20
- Implementing package metadata and classifiers
21
22
## Core Concepts
23
24
### 1. Package Structure
25
26
- **Source layout**: `src/package_name/` (recommended)
27
- **Flat layout**: `package_name/` (simpler but less flexible)
28
- **Package metadata**: pyproject.toml, setup.py, or setup.cfg
29
- **Distribution formats**: wheel (.whl) and source distribution (.tar.gz)
30
31
### 2. Modern Packaging Standards
32
33
- **PEP 517/518**: Build system requirements
34
- **PEP 621**: Metadata in pyproject.toml
35
- **PEP 660**: Editable installs
36
- **pyproject.toml**: Single source of configuration
37
38
### 3. Build Backends
39
40
- **setuptools**: Traditional, widely used
41
- **hatchling**: Modern, opinionated
42
- **flit**: Lightweight, for pure Python
43
- **poetry**: Dependency management + packaging
44
45
### 4. Distribution
46
47
- **PyPI**: Python Package Index (public)
48
- **TestPyPI**: Testing before production
49
- **Private repositories**: JFrog, AWS CodeArtifact, etc.
50
51
## Quick Start
52
53
### Minimal Package Structure
54
55
```
56
my-package/
57
├── pyproject.toml
58
├── README.md
59
├── LICENSE
60
├── src/
61
│ └── my_package/
62
│ ├── __init__.py
63
│ └── module.py
64
└── tests/
65
└── test_module.py
66
```
67
68
### Minimal pyproject.toml
69
70
```toml
71
[build-system]
72
requires = ["setuptools>=61.0"]
73
build-backend = "setuptools.build_meta"
74
75
[project]
76
name = "my-package"
77
version = "0.1.0"
78
description = "A short description"
79
authors = [{name = "Your Name", email = "you@example.com"}]
80
readme = "README.md"
81
requires-python = ">=3.8"
82
dependencies = [
83
"requests>=2.28.0",
84
]
85
86
[project.optional-dependencies]
87
dev = [
88
"pytest>=7.0",
89
"black>=22.0",
90
]
91
```
92
93
## Package Structure Patterns
94
95
### Pattern 1: Source Layout (Recommended)
96
97
```
98
my-package/
99
├── pyproject.toml
100
├── README.md
101
├── LICENSE
102
├── .gitignore
103
├── src/
104
│ └── my_package/
105
│ ├── __init__.py
106
│ ├── core.py
107
│ ├── utils.py
108
│ └── py.typed # For type hints
109
├── tests/
110
│ ├── __init__.py
111
│ ├── test_core.py
112
│ └── test_utils.py
113
└── docs/
114
└── index.md
115
```
116
117
**Advantages:**
118
119
- Prevents accidentally importing from source
120
- Cleaner test imports
121
- Better isolation
122
123
**pyproject.toml for source layout:**
124
125
```toml
126
[tool.setuptools.packages.find]
127
where = ["src"]
128
```
129
130
### Pattern 2: Flat Layout
131
132
```
133
my-package/
134
├── pyproject.toml
135
├── README.md
136
├── my_package/
137
│ ├── __init__.py
138
│ └── module.py
139
└── tests/
140
└── test_module.py
141
```
142
143
**Simpler but:**
144
145
- Can import package without installing
146
- Less professional for libraries
147
148
### Pattern 3: Multi-Package Project
149
150
```
151
project/
152
├── pyproject.toml
153
├── packages/
154
│ ├── package-a/
155
│ │ └── src/
156
│ │ └── package_a/
157
│ └── package-b/
158
│ └── src/
159
│ └── package_b/
160
└── tests/
161
```
162
163
## Detailed patterns and worked examples
164
165
Detailed pattern documentation lives in `references/details.md`. Read that file when the navigation tier above is insufficient.
166
167