DEV Community

Cover image for How to Create and Use Requirements.txt in Python
Wesley Bertipaglia
Wesley Bertipaglia

Posted on

How to Create and Use Requirements.txt in Python

After setting up your Python project, creating a requirements.txt file is essential for simplifying the installation process of dependencies for anyone cloning your project. This file lists all the Python packages required to run your project.

By creating a requirements.txt file, you ensure that others can easily replicate your project's environment, maintaining consistency and compatibility across different systems.

Before installing dependencies, consider create a virtual environment to isolate your project from other projects on your system. This prevents conflicts and ensures a clean environment for your project.

You can learn more about virtual by following one of my tutorials here.


Creating the requirements.txt File:
To generate the requirements.txt file, execute the following command in your project directory:

pip freeze > requirements.txt
Enter fullscreen mode Exit fullscreen mode

Installing Dependencies:
To install all project dependencies from the requirements.txt file, use the following command:

pip install -r requirements.txt
Enter fullscreen mode Exit fullscreen mode

Troubleshooting Tips:

  • Remember to update your requirements.txt file whenever you add, remove, or update dependencies in your project.
  • If you encounter dependency conflicts or compatibility issues, consider revising the version constraints in your requirements.txt file.

Top comments (4)

Collapse
 
drjasonharrison profile image
Jason Harrison

The kind of requirements.txt file you create with "pip freeze" is great for exactly reproducing your current environment but doesn't take into account that some requirements should not be pinned to specific versions of they don't need to be.

When you install almost any python package, let's call these second level packages, it will have packages it depends on, also known as third level packages.

Typically those third level packages will be specified using "requirements specifiers" that might pin the package to a specific version, to a specific major version, or a range of versions. This allows other package requirements to be resolved when pip attempts to install All of the second level packages and resolve the requirements specifications.

If you are using "pip freeze" then you are recording the resolution for today, and ignoring the improvements and new versions that might be available tomorrow or later in the future.

Hopefully your next five part series will discuss the importance of requirements specifiers, the need to keep a separate list of second level packages, and when to update your requirements.txt.

Collapse
 
wesleybertipaglia profile image
Wesley Bertipaglia

Thank you Jason, I'm writing what I'm learning, these articles help me to keep my understanding of these topic, I will study more about it.

Collapse
 
neusacodes profile image
neusacodes

Thank you for that!

Collapse
 
wesleybertipaglia profile image
Wesley Bertipaglia

Thanks ;)