TechBeamersTechBeamers
  • Learn ProgrammingLearn Programming
    • Python Programming
      • Python Basic
      • Python OOP
      • Python Pandas
      • Python PIP
      • Python Advanced
      • Python Selenium
    • Python Examples
    • Selenium Tutorials
      • Selenium with Java
      • Selenium with Python
    • Software Testing Tutorials
    • Java Programming
      • Java Basic
      • Java Flow Control
      • Java OOP
    • C Programming
    • Linux Commands
    • MySQL Commands
    • Agile in Software
    • AngularJS Guides
    • Android Tutorials
  • Interview PrepInterview Prep
    • SQL Interview Questions
    • Testing Interview Q&A
    • Python Interview Q&A
    • Selenium Interview Q&A
    • C Sharp Interview Q&A
    • PHP Interview Questions
    • Java Interview Questions
    • Web Development Q&A
  • Self AssessmentSelf Assessment
    • Python Test
    • Java Online Test
    • Selenium Quiz
    • Testing Quiz
    • HTML CSS Quiz
    • Shell Script Test
    • C/C++ Coding Test
Search
  • Python Multiline String
  • Python Multiline Comment
  • Python Iterate String
  • Python Dictionary
  • Python Lists
  • Python List Contains
  • Page Object Model
  • TestNG Annotations
  • Python Function Quiz
  • Python String Quiz
  • Python OOP Test
  • Java Spring Test
  • Java Collection Quiz
  • JavaScript Skill Test
  • Selenium Skill Test
  • Selenium Python Quiz
  • Shell Scripting Test
  • Latest Python Q&A
  • CSharp Coding Q&A
  • SQL Query Question
  • Top Selenium Q&A
  • Top QA Questions
  • Latest Testing Q&A
  • REST API Questions
  • Linux Interview Q&A
  • Shell Script Questions
© 2024 TechBeamers. All Rights Reserved.
Reading: Install Python packages with pip and requirements.txt
Font ResizerAa
TechBeamersTechBeamers
Font ResizerAa
  • Python
  • SQL
  • C
  • Java
  • Testing
  • Selenium
  • Agile Concepts Simplified
  • Linux
  • MySQL
  • Python Quizzes
  • Java Quiz
  • Testing Quiz
  • Shell Script Quiz
  • WebDev Interview
  • Python Basic
  • Python Examples
  • Python Advanced
  • Python OOP
  • Python Selenium
  • General Tech
Search
  • Programming Tutorials
    • Python Tutorial
    • Python Examples
    • Java Tutorial
    • C Tutorial
    • MySQL Tutorial
    • Selenium Tutorial
    • Testing Tutorial
  • Top Interview Q&A
    • SQL Interview
    • Web Dev Interview
  • Best Coding Quiz
    • Python Quizzes
    • Java Quiz
    • Testing Quiz
    • ShellScript Quiz
Follow US
© 2024 TechBeamers. All Rights Reserved.
PIPPython Tutorials

Install Python packages with pip and requirements.txt

Last updated: May 02, 2024 4:52 pm
By Soumya Agarwal
Share
10 Min Read
Manage Python packages using pip install and create requirements.txt file for configuration.
SHARE

Check this tutorial if you want to learn how to use pip to install Python packages using the requirements.txt.

Contents
How to create a requirements.txt to install project depWhat is the requirements.txt file?The requirements.txt syntaxRequirements.txt exampleDifferent variations of requirements.txtSample requirements.txt configuration fileHow to Install packages using the pip -r requirements.txtActivate virtual environmentInstall and verify dependencies using pipUsing pip3 on some systemsUsing Python -m pip for Python 3Installing Packages IndividuallyExport project configuration using pip freeze and moreUsing pip freezeUsing pipdeptreeUsing pipenv

Introduction to pip 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 difficult to find issues that may occur due to an incompatible package version.

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 requirements.txt.

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
<pkg_name><version_specifier><version_number>

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 ==, >=, <=, !=, or ~=.
  • <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.

Requirements.txt example

Let’s look at the sample requirements.txt file.

# This is a comment
requests~=2.26.0  # Another comment
Flask>=2.0.1

# You can add notes using comments
# numpy package with a version less than or equal to 1.21.0
numpy<=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 installSyntax Example
Exact Versionpkg_name==1.2.3
Minimum Version (>=)pkg_name>=1.2.3
Maximum Version (<=)pkg_name<=1.2.3
Not Equal Topkg_name!=1.2.3
Compatible Range (~=)pkg_name~=1.2.3
Wildcard (Any Version)pkg_name==*
Latest Version (No Specifier)pkg_name
Alternative Packages (Condition)pkg_name==1.2.3 ; sys_platform != 'win32'
pip install requriements.txt with different options

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)
pandas>=1.2.0,<=1.3.3

# Install NumPy version greater than or equal to 1.20.0 but not equal to 1.21.1
numpy>=1.20.0,!=1.21.1

# For Data Visualization
# Install any version in the range of 3.4.0 to less than 3.5.0 for Matplotlib
matplotlib~=3.4.0

# Install from a range for Seaborn
seaborn>=0.11.0,<=0.11.2

# For Machine Learning
# Install scikit-learn version between 0.24.0 and 0.24.2 (inclusive)
scikit-learn>=0.24.0,<=0.24.2

# Install the exact version for TensorFlow
tensorflow==2.6.0

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 pip package manager.
  • -r requirements.txt: This flag tells pip to install packages listed in the requirements.txt file. The -r stands 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:

pip list

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 pip:

pip3 install -r requirements.txt

Using 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 pip:

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:

Using pip freeze

The 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.

Using pipdeptree

The 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.

Using pipenv

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 pipenv project.

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

Quick Reference
  • https://pip.pypa.io/en/stable/reference/requirements-file-format/

Enjoy Learning,
Team TechBeamers

You Might Also Like

How to Connect to PostgreSQL in Python

Generate Random IP Address (IPv4/IPv6) in Python

Python Remove Elements from a List

How to Use Extent Report in Python

10 Python Tricky Coding Exercises

Soumya Agarwal Avatar
By Soumya Agarwal
Follow:
I'm a BTech graduate from IIITM Gwalior. I have been actively working with large MNCs like ZS and Amazon. My development skills include Android and Python programming, while I keep learning new technologies like data science, AI, and LLMs. I have authored many articles and published them online. I frequently write on Python programming, Android, and popular tech topics. I wish my tutorials are new and useful for you.
Previous Article Python Programming Interview Questions from Google, Microsoft, Amazon, Facebook, Apple Top 50 Python Programming Interview Questions With Answers
Next Article 7 Ways to Remove Whitespace from a String in Python How to Remove Whitespace from a String in Python

Popular Tutorials

SQL Interview Questions List
50 SQL Practice Questions for Good Results in Interview
SQL Interview Nov 01, 2016
Demo Websites You Need to Practice Selenium
7 Sites to Practice Selenium for Free in 2024
Selenium Tutorial Feb 08, 2016
SQL Exercises with Sample Table and Demo Data
SQL Exercises – Complex Queries
SQL Interview May 10, 2020
Java Coding Questions for Software Testers
15 Java Coding Questions for Testers
Selenium Tutorial Jun 17, 2016
30 Quick Python Programming Questions On List, Tuple & Dictionary
30 Python Programming Questions On List, Tuple, and Dictionary
Python Basic Python Tutorials Oct 07, 2016
//
Our tutorials are written by real people who’ve put in the time to research and test thoroughly. Whether you’re a beginner or a pro, our tutorials will guide you through everything you need to learn a programming language.

Top Coding Tips

  • PYTHON TIPS
  • PANDAS TIPSNew
  • DATA ANALYSIS TIPS
  • SELENIUM TIPS
  • C CODING TIPS
  • GDB DEBUG TIPS
  • SQL TIPS & TRICKS

Top Tutorials

  • PYTHON TUTORIAL FOR BEGINNERS
  • SELENIUM WEBDRIVER TUTORIAL
  • SELENIUM PYTHON TUTORIAL
  • SELENIUM DEMO WEBSITESHot
  • TESTNG TUTORIALS FOR BEGINNERS
  • PYTHON MULTITHREADING TUTORIAL
  • JAVA MULTITHREADING TUTORIAL

Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Loading
TechBeamersTechBeamers
Follow US
© 2024 TechBeamers. All Rights Reserved.
  • About
  • Contact
  • Disclaimer
  • Privacy Policy
  • Terms of Use
TechBeamers Newsletter - Subscribe for Latest Updates
Join Us!

Subscribe to our newsletter and never miss the latest tech tutorials, quizzes, and tips.

Loading
Zero spam, Unsubscribe at any time.
x