Python·100 questions

How to properly install dependencies in a project?

Answer

Properly installing dependencies in a Python project is the key to your application's stability and predictable behavior across different computers or servers. Randomly installing packages into the global system environment inevitably leads to version conflicts and broken projects.

The first step when creating any new project should be isolation via a virtual environment. Create it using the command python -m venv venv, and then activate it: source venv/bin/activate for Linux and macOS, or venv Scripts activate for Windows.

After activating the environment, you can safely install the required libraries.

Install packages via the standard manager using the command python -m pip install package_name so that they go strictly into your project's isolated folder.
Freeze the current state of dependencies into a text file using the command pip freeze > requirements.txt to document the exact versions of all installed libraries.
When moving the project to another computer or deploying it to a server, restore the environment with a single command: pip install -r requirements.txt.
For large and complex projects, consider using modern advanced dependency management tools such as Poetry or uv, which automatically resolve conflicts and manage versions.

Following this simple workflow will save you from a lot of headaches during team development and deploying the finished product to production.

Was this answer helpful?

More questions in this topic

Related questions from other topics