GGistDev

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-tools for pinning/compiling requirements
  • poetry for dependency resolution, virtualenvs, and publishing
  • pipenv for Pipfile/Pipfile.lock workflows
  • conda ecosystems 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 pipx or separate envs)
  • Pin exact versions for apps; use ranges for libraries
  • Automate upgrades with checkers (e.g., dependabot, renovate)

Summary

  • Use pip/PyPI for installs; freeze requirements for reproducibility
  • Consider pip-tools/poetry/pipenv for locking and smoother workflows