Python > Deployment and Distribution > Dependency Management > Managing Dependencies with `pip`
Creating a `requirements.txt` file
This snippet demonstrates how to create a `requirements.txt` file, which lists all the dependencies of your Python project. This file is essential for reproducible deployments and allows others to easily install the necessary packages to run your code.
Generating `requirements.txt`
The `pip freeze` command lists all installed packages in your current environment along with their versions. The `>` operator redirects the output of the command into a file named `requirements.txt`. This file will contain a list of packages like `requests==2.26.0`, one package per line.
pip freeze > requirements.txt
Explanation of the `requirements.txt` format
Each line in `requirements.txt` represents a single dependency. The format is `package_name==version_number`. The `==` specifies an exact version. You can also use other specifiers like `>=`, `<=`, `~=`, or `!=` if you have specific version requirements. It's generally best practice to use exact versions for deployments to avoid unexpected behavior due to updates.
Real-Life Use Case
Imagine you're deploying a Flask web application to a server. You need to ensure that the server has the correct versions of Flask, its dependencies (like Werkzeug), and any other packages your application uses (e.g., `requests` for making HTTP requests, `SQLAlchemy` for database interactions). Creating a `requirements.txt` file and using it to install the dependencies on the server ensures that your application runs as expected in the production environment.
Best Practices
Interview Tip
Be prepared to explain the purpose of `requirements.txt`, how it's generated, and how it's used. Also, understand the difference between specifying exact versions (`==`) and using other version specifiers.
When to use `requirements.txt`
Use a `requirements.txt` file whenever you want to deploy your Python application or share it with others. It is essential for creating reproducible environments and ensuring that your application runs consistently across different systems.
Alternatives
Pros
Cons
FAQ
-
How do I install dependencies from a `requirements.txt` file?
Use the command `pip install -r requirements.txt`. -
What if I get conflicting dependencies?
Conflicting dependencies can occur when different packages require different versions of the same dependency. Use tools like `pip-compile` (from `pip-tools`) or consider switching to a more robust dependency management tool like Poetry or Conda to resolve these conflicts.