Sobes.tech
Junior — Middle

How and where is it best to manage dependencies in a Python project?

sobes.tech AI

Answer from AI

In Python, virtual environments and package description files are commonly used to manage dependencies:

  • Virtual Environment (venv, virtualenv) — isolates project dependencies from system-wide packages to avoid conflicts.

  • requirements.txt — a simple text file listing required packages with their versions. Suitable for small projects.

  • Pipenv — a tool that combines virtual environment management and dependency management, creating Pipfile and Pipfile.lock.

  • Poetry — a modern dependency and packaging manager that manages dependencies and versions in pyproject.toml.

It is best to create a virtual environment in the project root and store dependencies in one of these files. This allows other developers and CI/CD systems to quickly reproduce the environment.

Example of creating a virtual environment and installing dependencies from requirements.txt:

python -m venv venv
source venv/bin/activate  # for Linux/macOS
venv\Scripts\activate  # for Windows
pip install -r requirements.txt

This approach ensures stability and reproducibility of the environment.