How to use pip requirements file? [SOLVED]


Python

Author: Omer Cakmak
Reviewer: Deepak Prasad

What is pip requirements file?

A pip requirements file is a file specifying a list of packages and their versions that should be installed using the pip install command. This file is commonly named "requirements.txt" and is used to define the dependencies of a project, allowing others to easily set up the same environment to run the code. The file format consists of one package per line, with an optional version number specified after a == symbol. An example of a requirements file might look like this:

numpy==1.18.5
pandas==1.0.3
matplotlib==3.2.2

The contents of a requirements file can be installed using the following command:

pip install -r requirements.txt

 

Symbols used in requirements file

You must be familiar with the below set of symbols which are actively used inside pip requirements file:

  • ==: This symbol specifies an exact version number for a package. For example, requests==2.24.0 indicates that the requests package with version 2.24.0 should be installed.
  • >=: This symbol specifies a lower bound for the version number of a package. For example, numpy>=1.18.5 indicates that the numpy package with a version number of 1.18.5 or higher should be installed.
  • <: This symbol specifies an upper bound for the version number of a package. For example, pandas<2.0 indicates that the pandas package with a version number less than 2.0 should be installed.
  • !=: This symbol specifies a version number that should not be installed. For example, matplotlib!=3.3.0 indicates that the matplotlib package with version 3.3.0 should not be installed.
  • ,: This symbol is used to separate multiple version constraints for a single package. For example, numpy>=1.18.5,<2.0 indicates that the numpy package with a version number of 1.18.5 or higher and less than 2.0 should be installed.
  • git+: This prefix is used in front of a Git repository URL to specify that the package should be installed from a Git repository. For example, git+https://github.com/user/repo.git@master#egg=repo-0.1 indicates that the repo package with version 0.1 should be installed from the specified Git repository.
  • https:// or http://: These prefixes are used in front of a URL to specify that the package should be installed from a file at that URL. For example, https://github.com/user/repo/archive/master.zip#egg=repo-0.1 indicates that the repo package with version 0.1 should be installed from the specified ZIP archive.

 

How to create pip requirements file

You can start by creating an empty file and make sure to name it "requirements.txt"

foc@ubuntu22:~$ touch requirements.txt

Now we add the following content in our requirements.txt file:

distlib == 0.3.5      # Version Matching. Must be version 0.3.4
filelock >= 3.8.0     # Minimum version 3.7.1
six != 1.16.0         # Version Exclusion. Anything except version 1.16.0
virtualenv ~= 20.15   # Compatible release. Same as >= 20.15, == 20.*
pycrumbs              # No version specified

Now install the packages with requirements.txt containing these packages:

foc@ubuntu22:~$ pip3 install -r requirements.txt
Defaulting to user installation because normal site-packages is not writeable
Collecting distlib==0.3.5
  Downloading distlib-0.3.5-py2.py3-none-any.whl (466 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 467.0/467.0 KB 752.5 kB/s eta 0:00:00
Collecting filelock>=3.8.0
  Downloading filelock-3.9.0-py3-none-any.whl (9.7 kB)
Collecting six!=1.16.0
  Downloading six-1.15.0-py2.py3-none-any.whl (10 kB)
Collecting virtualenv~=20.15
  Downloading virtualenv-20.17.1-py3-none-any.whl (8.8 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 8.8/8.8 MB 2.8 MB/s eta 0:00:00
Collecting pycm<=3.8,>=3.7
  Downloading pycm-3.8-py2.py3-none-any.whl (66 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 66.1/66.1 KB 1.8 MB/s eta 0:00:00
Collecting pycrumbs
  Downloading pycrumbs-0.1.3-py3-none-any.whl (15 kB)
Collecting platformdirs<3,>=2.4
  Downloading platformdirs-2.6.2-py3-none-any.whl (14 kB)
Collecting virtualenv~=20.15
  Downloading virtualenv-20.17.0-py3-none-any.whl (8.8 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 8.8/8.8 MB 2.2 MB/s eta 0:00:00
  Downloading virtualenv-20.16.7-py3-none-any.whl (8.8 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 8.8/8.8 MB 2.0 MB/s eta 0:00:00
  Downloading virtualenv-20.16.6-py3-none-any.whl (8.8 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 8.8/8.8 MB 2.8 MB/s eta 0:00:00
  Downloading virtualenv-20.16.5-py3-none-any.whl (8.8 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 8.8/8.8 MB 1.9 MB/s eta 0:00:00
Collecting art>=1.8
  Downloading art-5.8-py2.py3-none-any.whl (595 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 595.7/595.7 KB 2.0 MB/s eta 0:00:00
Collecting numpy>=1.9.0
  Downloading numpy-1.24.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (17.3 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 17.3/17.3 MB 2.2 MB/s eta 0:00:00
Collecting GitPython>=3.0.0
  Downloading GitPython-3.1.30-py3-none-any.whl (184 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 184.0/184.0 KB 2.1 MB/s eta 0:00:00
Collecting gitdb<5,>=4.0.1
  Downloading gitdb-4.0.10-py3-none-any.whl (62 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 62.7/62.7 KB 2.2 MB/s eta 0:00:00
Collecting smmap<6,>=3.0.1
  Downloading smmap-5.0.0-py3-none-any.whl (24 kB)
Installing collected packages: distlib, smmap, six, platformdirs, numpy, filelock, art, virtualenv, pycm, gitdb, GitPython, pycrumbs
  WARNING: The scripts f2py, f2py3 and f2py3.10 are installed in '/home/foc/.local/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
  WARNING: The script virtualenv is installed in '/home/foc/.local/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed GitPython-3.1.30 art-5.8 distlib-0.3.5 filelock-3.9.0 gitdb-4.0.10 numpy-1.24.1 platformdirs-2.6.2 pycm-3.8 pycrumbs-0.1.3 six-1.15.0 smmap-5.0.0 virtualenv-20.16.5

At the end of the installation, you can see that the packages are installed with the following versions:

  • distlib = 0.3.5
  • filelock = 3.9.0
  • six = 1.15.0
  • virtualenv = 20.16.5
  • pycm = 3.8
  • pycrumbs = 0.1.3

Now you can install the packages needed for your project by writing them in a requirements.txt file.

 

Another sample requirements file

Let me also share another file which contains more complex handling of versions:

# Packages from PyPI
requests==2.24.0
Flask==1.1.2

# Package from specific version control repository
git+https://github.com/user/repo.git@master#egg=repo-0.1

# Package from specific URL
https://github.com/user/repo/archive/master.zip#egg=repo-0.1

# Package with specific version constraints
numpy>=1.18.5,<2.0
pandas>=1.0.3,!=1.1.0,<2.0
matplotlib>=3.2.2,<4.0

Here,

  • The == symbol indicates that the exact version specified should be installed for requests and Flask.
  • The git+ prefix tells pip to install from a Git repository, and the rest of the line specifies the repository URL and the version of the package to be installed (in this case, the master branch).
  • The #egg=repo-0.1 part specifies the name and version of the package to be installed from the provided URL.
  • The >= symbol indicates a lower bound for the version, and the < symbol indicates an upper bound. The != symbol excludes a specific version. In this example, the packages numpy, pandas, and matplotlib are required to be version 1.18.5 or higher and less than 2.0, version 1.0.3 or higher but not equal to 1.1.0 and less than 2.0, and version 3.2.2 or higher and less than 4.0, respectively.

 

Summary

A requirements file is used in Python projects to specify the dependencies of the project, and to ensure that these dependencies can be easily installed on any system that needs to run the code. It is an essential tool for ensuring that Python projects are easily reproducible, collaborate, maintainable, and deployable.

stackoverflow.com - What does " -r " do in pip install -r requirements.txt

 

Omer Cakmak

Omer Cakmak

He is highly skilled at managing Debian, Ubuntu, CentOS, Oracle Linux, and Red Hat servers. Proficient in bash scripting, Ansible, and AWX central server management, he handles server operations on OpenStack, KVM, Proxmox, and VMware. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment