Search code examples
pythonpiprequirements.txt

Python Reference requirements in other requirements file


I have a file with pip install requirements for a repo (requirements.txt). For testing I have another file (requirements_tests.txt) that should contain same requirements and more. Can I reference requirements.txt from inside requirements_tests.txt?


Solution

  • Solution

    Add -r requirements.txt at first line i.e. -r path/to/file in your requirements_test.txt.
    Which means when you do pip install -r requirements_test.txt it will come at line -r requirements.txt and will install all it's requirement and will again continue in requirements_test.txt

    E.g.

    requirements.txt

    pandas
    numpy
    matplotlib
    

    requirements_test.txt

    -r requirements.txt
    tox
    pytest
    black
    flake
    

    PS: There are some other options also depending on what you are using for managing the environment or any other packages for managing different environments from a single file.

    Poetry manages Python virtual environment, environment-based dependency management, build, and publish.
    There's also Hatch which is adopted by PyPa. Seems hacky to use at the moment but follows PEP guidelines and adopts new guidelines.