Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Poetry ignore dependency in pyproject.toml

I currently have a Python3 project set up with Poetry as the main package manager. Next to that I also have set up a build and some automated testing via Github workflows. My package depends on Tensorflow, although the automated tests can run without it. Unfortunately Tensorflow (which is quite big) is installed every time the Github workflow runs these tests. As Tensorflow is not needed for these tests and as I would like to speed up my build, I would like to ignore the Tensorflow dependency when poetry install is called from the build pipeline.

Does anybody know a way to exclude a dependency when using Poetry?

like image 885
Daan Klijn Avatar asked Jul 19 '20 10:07

Daan Klijn


People also ask

How do you remove dependency from a poem?

lock file, Poetry will create one after dependency resolution. You can specify to the command that you do not want the development dependencies installed by passing the --no-dev option. If you want to remove old dependencies no longer present in the lock file, use the --remove-untracked option.

How do you update a Pyproject poem toml?

To update to the latest versions, use the update command. This will fetch the latest matching versions (according to your pyproject. toml file) and update the lock file with the new versions. (This is equivalent to deleting the poetry.

Where does poetry install dependencies from?

poetry install : Installs the dependencies specified in pyproject. toml. The first time a project's dependencies are installed, a . lock file is created, which contains the actual version numbers of each package that was installed (i.e.: if Flask = "*" resulted in downloading Flask version 1.0.

Should you commit poetry lock?

According to the maintainers, Commit your poetry. lock file to version control. Committing this file to VC is important because it will cause anyone who sets up the project to use the exact same versions of the dependencies that you are using.


Video Answer


1 Answers

The only other approach that comes to mind would be to move the tensorflow dependency to an extra category, which in poetry would look like this:

$ poetry add --extras tensorflow

This means that it won't be installed when you run poetry install, unless it is part of a named group that you install explicitly. This can be achieved by adding this to your pyproject.toml:

[tool.poetry.extras]
runtime = ["tensorflow"]  # any name goes, I chose "runtime"  because it sounded like it'd make sense

The list can be extended with any other package that you only need during runtime, and not during tests. If you want to install your code to actually run it, you'll have to execute this before:

$ poetry install --extras runtime

This will cleanly separate your dependencies, you'll have to evaluate whether it makes sense in your case. As a rule of thumb, it's usually better to run hacks to make tests work rather than worsen the client experience, so your current workflow has a good chance of being better than what I just wrote.

like image 189
Arne Avatar answered Oct 18 '22 20:10

Arne