Open In App

Overview of requirements.txt and Direct GitHub Sources

Last Updated : 27 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

When building a Python project a typical way of handling the dependencies is to add them to a requirements.txt. The 'requirements.txt' is a simple text file, which stores names of the modules and their corresponding versions. A user who wants to execute the project in their own system can simply copy the project and install all the necessary dependencies using a simple command 'pip install -r requirements.txt`

pip install -r requirements.txt

This ensures a developer to inform about all the required modules and ensure a consistent development environment across different systems.

Overview of requirements.txt

The requirements.txt file is a simple text file that lists all the dependencies and their versions required for a project. This helps developers avoid compatibility issues and ensure consistent execution of code.

Importance of Specifying Direct GitHub Sources

In addition to installing packages from the Python Package Index (PyPI), you can also install modules directly from public GitHub repositories. This is particularly useful when a package is not available on PyPI or when you need a specific version or branch of a package that is hosted on GitHub.

Basic Structure of requirements.txt

Common Usage and Format

The requirements.txt file typically the package names and optional version numbers. For example:

numpy==1.20.3
pandas

Specifying GitHub Repositories

In addition to installing packages from PyPI, we can also install modules from public github repositories by adding the a line like this to your `requirements.txt`.

git+https://github.com/<username>/<repository_name>.git@<branch_or_commit(optional)>#egg=<package_name>

where,

  • `git+https://github.com/<username>/<repository_name>.git`: public GitHub repository URL with required package
  • `@<branch_or_commit`(optional)>`: Indicates the branch, tag, or commit hash you want to install from.
  • `#egg=<package_name>`: name of the package, which is required.

Examples:

Example 1: Basic Repository Link

git+https://github.com/psf/requests.git#egg=requests

Example 2: Specifying a Branch

git+https://github.com/psf/requests.git@main#egg=requests

Example 3: Specifying a Tag

git+https://github.com/psf/[email protected]#egg=requests

Installing modules using pip and requirements.txt

To install all the modules required by your project in your development environment, you can follow the below steps:

1. Create a text file and rename it to 'requirements'. The file should look like 'requirements.txt'

2. Add all the modules and versions (optional) you need for your project in the requirements.txt file.
example:

numpy==1.20.3
pandas

3. In addition to installing packages from PyPI, we can also install modules from public github repositories by adding the a line like this to your `requirements.txt`.

git+https://github.com/<username>/<repository_name>.git@<branch_or_commit(optional)>#egg=<package_name

Example:

git+https://github.com/psf/requests.git@main#egg=requests
numpy

4. Proceed to install you packages using the following command:

pip install -r requirements.txt

5. The packages should be successfully installed in your development environment and must be ready to use.

requirements
adding dependencies to requirements.txt


installRequirementFromGit
installing requests module from git

Thus, we have successfully installed a package directly from github source.


Next Article
Article Tags :
Practice Tags :

Similar Reads