How do you build and publish a Python package?
IntermediateConceptEnvironments, dependencies & packaging
Answer
pip install build twine
python -m build # dist/*.whl and dist/*.tar.gz
twine check dist/*
twine upload --repository-url https://pypi.internal/ dist/*
The build backend (setuptools, hatchling, flit-core, pdm-backend, poetry-core) is declared in [build-system]. For internal tools we publish to a private index (CodeArtifact, Artifactory, Nexus, GitLab package registry). For PyPI, use Trusted Publishing from CI via OIDC instead of API tokens.
More on Environments, dependencies & packaging
What is a virtual environment and why do we always use one?BeginnerWhat does `error: externally-managed-environment` mean?Beginner`requirements.txt` vs `pyproject.toml`: what goes where?IntermediateWhy is `pip freeze > requirements.txt` not a great lock strategy?IntermediateWhat do version specifiers like `~=`, `==`, `>=,<` mean?BeginnerWhat are `pipx` and `uv`, and when do you use them?Intermediate