Skip to content

How to Use requirements.txt Files in Python

How to Use requirements txt Files in Python Cover Image

In this tutorial, you’ll learn how to use a requirements.txt file in Python to manage and handle the dependencies of your project. Using a requirements.txt file is particularly helpful when sharing your code with others via source code management tools, such as Github. The file provides the ability to easily track and identify the packages that you use in a project. Similarly, it makes it easy to install all the packages required for a project.

By the end of this tutorial, you’ll have learned:

  • What a requirements.txt file is and why it matters in Python
  • How to create a requirements.txt file manually and automatically
  • How to install all dependencies in a requirements.txt file using pip

What is a requirements.txt file in Python?

A requirements.txt file in Python lets you keep track of the modules and packages used in your projects. Simply put, a requirements.txt file is simply a .txt file that tracks all of your requirements.

This makes it easier to install the required packages, whether for yourself or for other users of your code. It makes it much simpler to review these requirements, rather than looking through different pieces of code to try and piece it all together.

Similarly, when you share your project code with others, they’re able to see (and install, as you’ll learn later) your project’s requirements. This is particularly useful as the file also keeps track of the versions of these libraries, ensuring that the minimum requirements are met!

How to Create a requirements.txt File in Python

While you’re able to create a .txt file that contains all of your requirements, the pip project manager actually makes it much easier to create a requirements file.

When working in a virtual environment, you can simply use the command below:

pip freeze > requirements.txt

When you run this code, a new file will be created in the directory you’re working in. This file will list the different packages and modules in your virtual environment. That’s really all there is to it!

In addition, running this code will also include the version numbers in the file.

How to Install All Packages from a requirements.txt File Using pip and Python

One of the benefits of using a requirements.txt file is that users of your code can install all required libraries and modules in one command. This can be really helpful when working in a new virtual environment, meaning that you don’t need to install the libraries one by one.

If a requirements.txt file is present in the root folder of the project, you can run the code below to install all of the packages in the file:

pip install -r requirements.txt

This will read the file and install all the packages in that file. Your terminal will output a list of all the libraries that have been installed and raise any errors if they occur.

How to Maintain a requirements.txt File in Python

Now that you’ve created a requirements.txt file, you may be wondering how you maintain it. If you want to check what packages are out of date, you can use the following command:

pip list --outdated

This will list out all the packages where your package version is out of date, listing out your version and the most current version.

In order to update to the latest version of a package, you can use the command below:

pip install -U package_name

This will upgrade your version to the latest version. Once this is done, you can refreeze your requirements by re-running the pip freeze > requirements.txt command.

Conclusion

In this tutorial, you learned how to use a requirements.txt file in Python using the pip package manager. You learned why this file is useful in developing your Python programs, especially if you’re sharing them with others via a SCM like Github. You learned how easy it is to create a requirements.txt file using the pip package manager. Then, you learned how to install all packages from the file using a terminal command. Finally, you learned how to maintain the file by checking which packages need updating.

Additional Resources

To learn more about related topics, check out the tutorials below:

Nik Piepenbreier

Nik is the author of datagy.io and has over a decade of experience working with data analytics, data science, and Python. He specializes in teaching developers how to use Python for data science using hands-on tutorials.View Author posts

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *