Search code examples
pythonpython-poetry

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?


Solution

  • 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.