7

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.

sinoroc
  • 18,409
  • 2
  • 39
  • 70
Rulle
  • 4,496
  • 1
  • 15
  • 21
  • I am not sure I would recommend it, but maybe you can try with `pip install --quiet ...` instead. Knowing that you can add up to 3 times the `--quiet` flag to reduce the output even more: https://pip.pypa.io/en/stable/cli/pip/#quiet – sinoroc Feb 02 '23 at 16:58
  • @sinoroc the question is not about suppressing terminal output when installing. The question is about manipulating the contents of `requirements.txt` when creating it using `poetry export`. – Karl Knechtel Feb 02 '23 at 17:05
  • @KarlKnechtel Thanks, I know. That is mainly why I am only suggesting this in a comment and not writing this as an answer. Anyway, if the whole issue boils down to: "*A coworker thinks that message looks ugly and would like it not to be visible*", then mission accomplished, no? :) – sinoroc Feb 02 '23 at 18:09

1 Answers1

4

The colorama dependency is required by pytest.

I suppose your Docker image is for production use, so it doesn't have to contain pytest which is clearly a development dependency.

You can use poetry export --without-hashes --without dev -f requirements.txt -o requirements.txt to prevent your dev packages to be exported to the requirements.txt file so they won't be installed by pip install.

You can find some poetry export options Here.

klee1611
  • 181
  • 2
  • 4
  • There is no such option '--without dev' – Dat TT Apr 08 '23 at 10:06
  • There is an option `--without` which ignores groups so use `--without dev` to avoid dev dependencies. – klee1611 Apr 10 '23 at 07:26
  • Here is my result: NoSuchOptionException The "--without" option does not exist. ... when try this command; poetry export --without-hashes --without dev -f requirements2.txt -o requirements2.txt – Dat TT Apr 10 '23 at 15:28
  • 1
    Not sure what happened in your case. Maybe is the version of `poetry` you are using? I've tried and it works for me, and it's also documented on their website. – klee1611 Apr 12 '23 at 02:35