[Python] How to omit dependency when exporting requirements.txt using Poetry

Discussão em 'Python' iniciado por Stack, Março 8, 2025.

  1. Stack

    Stack Membro Participativo

    I have a Python3 Poetry project with a pyproject.toml file specifying the dependencies:

    [tool.poetry.dependencies]
    python = "^3.10"
    nltk = "^3.7"
    numpy = "^1.23.4"
    scipy = "^1.9.3"
    scikit-learn = "^1.1.3"
    joblib = "^1.2.0"

    [tool.poetry.dev-dependencies]
    pytest = "^5.2"


    I export those dependencies to a requirements.txt file using the command poetry export --without-hashes -f requirements.txt --output requirements.txt resulting in the following file requirements.txt:

    click==8.1.3 ; python_version >= "3.10" and python_version < "4.0"
    colorama==0.4.6 ; python_version >= "3.10" and python_version < "4.0" and platform_system == "Windows"
    joblib==1.2.0 ; python_version >= "3.10" and python_version < "4.0"
    nltk==3.8.1 ; python_version >= "3.10" and python_version < "4.0"
    numpy==1.24.1 ; python_version >= "3.10" and python_version < "4.0"
    regex==2022.10.31 ; python_version >= "3.10" and python_version < "4.0"
    scikit-learn==1.2.1 ; python_version >= "3.10" and python_version < "4.0"
    scipy==1.9.3 ; python_version >= "3.10" and python_version < "4.0"
    threadpoolctl==3.1.0 ; python_version >= "3.10" and python_version < "4.0"
    tqdm==4.64.1 ; python_version >= "3.10" and python_version < "4.0"


    that I use to install the dependencies when building a Docker image.

    My question: How can I omit the the colorama dependency in the above list of requirements when calling poetry export --without-hashes -f requirements.txt --output requirements.txt?

    Possible solution: I could filter out the line with colorama by producing the requirements.txt file using poetry export --without-hashes -f requirements.txt | grep -v colorama > requirements.txt. But that seems hacky and may break things in case the Colorama requirement is expressed across multiple lines in that file. Is there a better and less hacky way?

    Background: When installing this list of requirements while building the Docker image using pip install -r requirements.txt I get the message

    Ignoring colorama: markers 'python_version >= "3.10" and python_version < "4.0" and platform_system == "Windows"' don't match your environment


    A coworker thinks that message looks ugly and would like it not to be visible (but personally I don't care). A call to poetry show --tree reveals that the Colorama dependency is required by pytest and is used to make terminal colors work on Windows. Omitting the library as a requirement when installing on Linux is not likely a problem in this context.

    Continue reading...

Compartilhe esta Página