Check this tutorial if you want to learn how to use pip to install Python packages using the requirements.txt. It is a simple configuration file where we can list the exact packages with their versions. It will ensure that our Python project uses the correct packages. No matter whether you build it on a new machine or other members of your team use it on their systems. Without this, it can be really difficult to find issues that may occur due to an incompatible package version.
Using pip and requirements.txt to install Python packages
- How to create a requirements.txt to install project dep
- How to Install packages using the pip -r requirements.txt
- Export project configuration using pip freeze and more
How to create a requirements.txt to install project dep
When starting a new project, a requirements.txt file is useful to mention the dependencies needed for the project. It allows the developer to document and share the required Python packages and their versions. Before, we move ahead it will be good to understand more about this file.
Follow This: Leann Python Coding from Scratch
What is the requirements.txt file?
The requirements.txt file is a configuration file for declaring the project dependencies with their respective versions. It contains a list of external libraries or packages that are required for the project to run. On top of that, the
pip tool can read this file and easily install the necessary Python packages.
The pip which is a Python package manager was introduced in 2008 and since then, it is giving support for requriements.txt. However, as
pip evolved, it continued to enhance its support for
The requirements.txt syntax
For writing a perfect requirement file, it is important to know its syntax. The requirements.txt’s syntax is fairly simple, yet it comes with many variations and offers quite a bit of flexibility. Let’s understand it.
# The standard requirements.txt
Let’s break down the syntax:
<pkg_name>: It is the name of the Python package or the dependency you want to add.
<version_specifier>: It is the version specifier, such as
<version_number>: Here, you can place the specific version number or version range.
Moreover, from the syntax, you can see that we can even add comments using the # sign in this file.
Let’s look at the sample requirements.txt file.
# This is a comment
requests~=2.26.0 # Another comment
# You can add notes using comments
# numpy package with a version less than or equal to 1.21.0
Practice Python: Python Exercises for Beginners
Different variations of requirements.txt
With the syntax in our hand, it is easy for us to create this configuration file. However, there are still a few things to explore. These are different variations of the version specifier that we can utilize depending on our use case. Check from the below table. You can see it is listing down which specifier to use to install a specific Python package version depending on the need.
|When we have to install
|Minimum Version (>=)
|Maximum Version (<=)
|Not Equal To
|Compatible Range (~=)
|Wildcard (Any Version)
|Latest Version (No Specifier)
|Alternative Packages (Condition)
Now, let’s create a sample configuration file for a demo project.
Sample requirements.txt configuration file
Let’s take a demo data science project that needs us to specify some dependencies. In this example, we’ll add libraries for data manipulation, visualization, and machine learning. The following is the sample requirements.txt file for this project. Once the content of the file is ready, make sure you save it to the disk. After that, use the Python pip command to install the packages, read about it more in the next section.
# For Data Manipulation and Analysis
# Install Pandas version between 1.2.0 and 1.3.3 (inclusive)
# Install NumPy version greater than or equal to 1.20.0 but not equal to 1.21.1
# For Data Visualization
# Install any version in the range of 3.4.0 to less than 3.5.0 for Matplotlib
# Install from a range for Seaborn
# For Machine Learning
# Install scikit-learn version between 0.24.0 and 0.24.2 (inclusive)
# Install the exact version for TensorFlow
We hope the above sample will help you learn to manage and install dependencies for your own Python projects.
How to Install packages using the pip -r requirements.txt
Make sure you have saved the configuration in the requirements.txt file. Now, there few steps you should follow to install Python packages from it correctly.
Activate virtual environment
It’s often a good practice to work within a virtual environment to isolate project dep. If you’re not using a virtual environment, you can skip this step. If yes, then activate it using the following based on the OS type:
source venv/bin/activate # For Linux/macOS
venv\Scripts\activate # For Windows
Install and verify dependencies using pip
Run the following command to install dep from the requirements.txt file:
pip install -r requirements.txt
pip install: This is the basic command to install Python packages using the
-r requirements.txt: This flag tells pip to install packages listed in the
-rstands for “requirements.”
So, when you run
pip install -r requirements.txt, you are instructing
pip to read the list of dependencies from the
requirements.txt file and install them in your Python environment.
After the installation is complete, you can verify whether the packages were installed correctly or not:
The above command will display a list of installed Python packages along with their versions. You can check and confirm.
Using pip3 on some systems
On systems where both Python 2 and Python 3 are present, you might need to use
pip3 instead of
pip3 install -r requirements.txt
Python -m pip for Python 3
For Python 3, you can also use the following syntax to ensure you’re using the correct version of
python -m pip install -r requirements.txt
Installing Packages Individually
Just in case, you need to install Python packages individually, then you can run the pip install command by specifying the package with its version. Check the following example:
pip install pkg_name==1.2.3
In most cases,
pip install -r requirements.txt is the go-to command for installing dependencies. However, you may need some adjustments depending on the Python version you are using.
Export project configuration using pip freeze and more
When we’re working on a project and want to share it with others or need to set up the same environment on a different machine, we need to export the configuration. There are a few choices that can help us achieve this:
pip freeze command outputs the names and versions of installed packages in the current environment. You can redirect this output to a file:
pip freeze > requirements.txt
The above command creates or overwrites the
requirements.txt file with the current environment’s package listing.
pipdeptree provides a tree view of installed packages and their dependencies. You can install it with:
pip install pipdeptree
This is how you can generate a requirements.txt file using it:
pipdeptree --warn silence --warn silence | grep -P '^\S' > requirements.txt
This approach includes only the packages that your project directly depends on, excluding dependencies of dependencies.
If you’re using
pipenv for managing dependencies, you can use the following command:
pipenv lock -r > requirements.txt
This generates a
requirements.txt file based on the
Pipfile.lock in a
You need to run these commands in the same
env where your project deps are installed. After exporting, you can share the
requirements.txt file along with your project’s source code
All the very best,