Package Management in Python
Manage dependencies with pip and PyPI; consider modern tools for workflows and locking.
Installing and listing
pip install requests
pip show requests
pip list
Requirements and locking
Freeze current env to a file; reinstall later.
pip freeze > requirements.txt
pip install -r requirements.txt
Constraints let you pin transitive deps:
pip install -r requirements.txt -c constraints.txt
For reproducible builds, use lockfiles via pip-tools (pip-compile), poetry.lock, or pipenv.
Editable installs and local packages
Develop libraries in place.
pip install -e .
pyproject.toml (PEP 621) declares metadata and build backends; define extras in [project.optional-dependencies].
Indices and mirrors
Install from custom indices or mirrors.
pip install --index-url https://pypi.org/simple pkg
pip install --extra-index-url https://my.index/simple pkg
Tools
pip-toolsfor pinning/compiling requirementspoetryfor dependency resolution, virtualenvs, and publishingpipenvfor Pipfile/Pipfile.lock workflowscondaecosystems for binary stacks (DS/ML)
Publishing
Build and upload to PyPI.
python -m build
python -m twine upload dist/*
Best practices
- Keep application dependencies separate from tools (via
pipxor separate envs) - Pin exact versions for apps; use ranges for libraries
- Automate upgrades with checkers (e.g., dependabot, renovate)
Summary
- Use
pip/PyPIfor installs; freeze requirements for reproducibility - Consider
pip-tools/poetry/pipenvfor locking and smoother workflows