What Is Requirements.txt For Python? [Explained]

requirements.txt file is a commonly used convention for specifying and managing project dependencies. This file is typically used in conjunction with package management tools like pip to ensure that the correct versions of packages are installed for a Python project.

Here’s what a requirements.txt file typically contains:

  1. Package Names: Each line of the file lists a Python package or library that your project depends on. These package names should be the exact names as they appear on the Python Package Index (PyPI) or other package repositories.
  2. Version Specifiers: You can also specify version constraints for each package to ensure that a specific version or a range of versions is used. Common version specifiers include:
    • Exact version: package-name==1.2.3
    • Minimum version: package-name>=1.2.3
    • Maximum version: package-name<=1.2.3
    • Range of versions: package-name>=1.2.3,<2.0.0
    Version specifiers help ensure that your project uses compatible package versions, which can be important to maintain stability and avoid unexpected issues.

Here’s an example of what a simple requirements.txt file might look like:

requests==2.26.0
numpy>=1.21.0
matplotlib>=3.4.0,<4.0.0Code language: Python (python)

To install the packages listed in a requirements.txt file, you can use the following pip command:

This command will read the requirements.txt file and install the specified packages with their specified versions or version ranges.

Using a requirements.txt file is a good practice in Python development because it helps create a consistent development environment. It’s also commonly used in combination with version control systems like Git to ensure that all developers working on a project have the same dependencies installed.

Should I use requirements txt or setup py?

Whether you should use a requirements.txt file or a setup.py file for managing dependencies in your Python project depends on the context and your project’s needs. These two files serve different purposes, and in many cases, you might use both of them in your project.

Here’s a brief overview of each:

  1. requirements.txt:
    • Purpose: The primary purpose of requirements.txt is to specify the external dependencies of your project. It’s used to list the packages and their versions that your project relies on.
    • Use Cases:
      • Managing development and runtime dependencies.
      • Creating a consistent development environment for your project.
      • Facilitating easy installation of dependencies using pip.
    • Typical Usage: Developers use requirements.txt to document and communicate project dependencies. It’s commonly used when sharing code with others or deploying applications to different environments.
    • Example Use Case: You can use requirements.txt to ensure that a web server running your application has all the necessary packages installed.
    • Installation: You can install the dependencies listed in requirements.txt using pip install -r requirements.txt.
  2. setup.py:
    • Purpose: The primary purpose of setup.py is to define the metadata and build instructions for your Python package. It’s used when you want to distribute your Python project as a package that can be installed using pip.
    • Use Cases:
      • Creating distributable Python packages.
      • Specifying package metadata such as name, version, author, etc.
      • Defining custom installation or build steps.
    • Typical Usage: setup.py is used when you’re building and distributing a Python package, library, or module that others can install and use in their projects.
    • Example Use Case: If you’re developing a Python library that you want others to use, you would define the package metadata and distribution details in setup.py.
    • Installation: Users can install a package defined by setup.py by running pip install . in the package directory.

In many real-world scenarios, you might use both of these files together:

  • You use requirements.txt to manage the project’s external dependencies.
  • You use setup.py when you want to package and distribute your Python project as a reusable library or application.

For example, when you create a Python package to share with others, you typically define your package’s metadata and dependencies in setup.py, and you might also include a requirements.txt file for development and testing dependencies. This way, users can install your package easily with pip, and you can maintain a clear separation between development and distribution requirements.

Read More;

  • Muhammad Nabil

    I am a skilled and experienced Python developer with a huge passion for programming and a keen eye for details. I earned a Bachelor's degree in Computer Engineering in 2019 from the Modern Academy for Engineering and Technology. I am passionate about helping programmers write better Python code, and I am confident that I can make a significant contribution to any team. I am also a creative thinker who can come up with new and innovative ways to improve the efficiency and readability of code. My specialization includes Python, Django, SQL, Apache NiFi, Apache Hadoop, AWS, and Linux (CentOS and Ubuntu). Besides my passion for Python, I am a solo traveler who loves Pink Floyd, online video games, and Italian pizza.

    View all posts

Leave a Comment