How to install Python packages with pip and requirements.txt

Home /

Table of Contents

In this comprehensive guide, we’ve explored the fundamental task of installing Python packages using the Python Package Installer, commonly known as pip. We’ve specifically focused on installing packages according to the specifications listed in the requirements.txt file from a local directory.

Python programmers and users may effectively manage their Python dependencies and guarantee the seamless integration of third-party libraries and modules into their projects by following the instructions provided in this article. In addition to simplifying the installation procedure, this method improves repeatability and consistency in various settings.

Consider exploring more sophisticated pip usage options, such package version control, virtual environment integration, and dependency resolution. Additionally, to take use of the new features and enhancements provided by tools like pip, keep up with the most recent advancements in Python packaging and distribution techniques. These initiatives will support the upkeep of a reliable and orderly Python development process.

Introduction to pip

Installing and maintaining Python packages and their dependencies is made easier with Pip, a package manager for Python. It makes it simple for users to install, update, and remove packages from local directories and the Python Package Index (PyPI).

Make sure pip is installed in your Python installation and that Python is installed on your machine before using pip. After that, you may use the terminal or command line to execute pip instructions.

Understanding the requirements.txt File

A popular text file used to list a Python project’s dependencies is called requirements.txt. It enumerates the necessary package names and versions, separating them with whitespace or newline characters.

An example of requirements.txt is as follows.

image 2 - How to install Python packages with pip and requirements.txt

###### Requirements without Version Specifiers ######





###### Requirements with Version Specifiers ######

#   See

docopt == 0.6.1             # Version Matching. Must be version 0.6.1

keyring >= 4.1.1            # Minimum version 4.1.1

coverage != 3.5             # Version Exclusion. Anything except version 3.5

Mopidy-Dirble ~= 1.1        # Compatible release. Same as >= 1.1, == 1.*

As with Python code, comments can be included in requirements.txt using #.

Installing Packages from a Local Directory

The following procedures may be used to install packages from a local location using pip and a requirements.txt file:

  • Create a requirements.txt file: In the project root directory, create a requirements.txt file where you may specify the packages you wish to install, including any relevant version information.
  • Move package files to a local directory on your computer: Package files are often saved in.whl or.tar.gz formats. This might be a directory in your project, or it could be anywhere.
  • Specify the local directory: To give the path to the local directory holding the package files, edit the requirements.txt file. Use the absolute or relative path to the directory after the file:// prefix.

Example requirements.txt file with local directory specification:



  • Install packages: Launch a command window or terminal, go to your project’s root directory, and type the following pip command:
pip install -r requirements.txt

With this command, pip is instructed to install every package from the provided local directory as well as those listed in the requirements.txt file.

Create requirements.txt with pip freeze

The packages and versions that are installed in the current environment are reported by pip freeze in a format that may be used as requirements.txt.

$ pip freeze





Using the redirection operator >, you can save the output of pip freeze to a file. This file can be used to install the same versions of packages in a different environment.

Advanced Usage and Options

Pip offers a number of commands and options for complex package management jobs. The following are some helpful choices for installing programs from a requirements.txt file:

  • –no-index: Install packages without accessing the PyPI repository.
  • –no-deps: Install packages without installing their dependencies.
  • –ignore-installed: Ignore installed packages and force installation from scratch.
  • –target: Install packages into a specified directory instead of the default Python installation directory.

For a detailed list of parameters and instructions, see the pip documentation.

Troubleshooting Common Issues

If you experience problems using pip to install packages, take into consideration the following troubleshooting steps:

  • Check package versions: Verify that the packages listed in the requirements.txt file are current and accessible inside the current directory.
  • Verify file paths: Verify again that the file paths listed in the requirements.txt file go to the actual locations of the package files.
  • Debug installation errors: To see comprehensive details about the installation procedure and any issues that occurred, use the verbose (-v) option with pip.

This tutorial has demonstrated how to install Python packages from a local directory using pip in accordance with the requirements.txt file’s instructions. You may effectively manage dependencies for your Python projects by adhering to these guidelines and best practices, which will guarantee smooth package installations and improved development workflows.

If you’re willing to learn more, I suggest reading through pip’s extensive documentation, which provides details on more complex package management chores and advanced pip usage. You may improve your Python programming skills and streamline your package management procedures by becoming acquainted with pip’s whole feature set.

Share The Tutorial With Your Friends

Check Our Ebook for This Online Course

Advanced topics are covered in this ebook with many practical examples.

Other Recommended Article