How to manage dependencies and their versions when working with pip for Python projects?
sobes.tech AI
Answer from AI
To manage dependencies and their versions in Python projects using pip, the following approaches are commonly used:
-
requirements.txt — a file listing all dependencies with specific versions or version ranges. For example:
requests==2.28.1 numpy>=1.21,<1.23Installing all dependencies from the file:
pip install -r requirements.txt -
Virtual environments (venv, virtualenv) — allow isolating project dependencies so that different projects do not conflict in library versions.
-
pip-tools — a utility for managing dependencies that helps lock versions and update them safely.
-
Poetry or Pipenv — more modern tools for managing dependencies and virtual environments that automate the process and simplify work.
Thus, the main method is to lock versions in requirements.txt and use a virtual environment to isolate the project.