admin管理员组

文章数量:1391995

I have a package with a pyproject.toml that has a core package & an optional GUI package with its own extra dependencies. This is the project structure:

.
└── my_project/
    ├── src/
    │   └── my_package/
    │       ├── core/
    │       │   └── __init__.py
    │       ├── gui/
    │       │   └── __init__.py
    │       └── __init__.py
    ├── poetry.lock
    └── pyproject.toml

PEP 771 specifies how extras can be installed using square brackets. I have found plenty of resources on how to install extras, but frustratingly none on how to define them in your own project.

How do I set up my project so that a user installing with poetry add my_package installs just the core package, but poetry add my_package[gui] installs both core and gui?

I have a package with a pyproject.toml that has a core package & an optional GUI package with its own extra dependencies. This is the project structure:

.
└── my_project/
    ├── src/
    │   └── my_package/
    │       ├── core/
    │       │   └── __init__.py
    │       ├── gui/
    │       │   └── __init__.py
    │       └── __init__.py
    ├── poetry.lock
    └── pyproject.toml

PEP 771 specifies how extras can be installed using square brackets. I have found plenty of resources on how to install extras, but frustratingly none on how to define them in your own project.

How do I set up my project so that a user installing with poetry add my_package installs just the core package, but poetry add my_package[gui] installs both core and gui?

Share Improve this question edited Mar 17 at 1:39 mkrieger1 23.5k7 gold badges64 silver badges82 bronze badges asked Mar 17 at 0:11 frimannfrimann 1177 bronze badges 3
  • does this help: stackoverflow/questions/60971502/…. possible a dupe – Nath Commented Mar 17 at 0:56
  • also the behaviour and config structure has changed lots between v1 and v2 so take care to look at the warnings and stuff you get – Nath Commented Mar 17 at 0:57
  • Sadly no it does not - I don't want to add a dependency as an extra, I want to define part of my package as an extra for users to optionally install. – frimann Commented Mar 17 at 1:00
Add a comment  | 

1 Answer 1

Reset to default 1

Extras don't work like that, they only apply to your packages dependencies. If you put the code in same package it will get bundled in when the package is built. Extras only apply to the dependencies of your package. In your case regardless of whether the user specifies [gui]or not they will get the gui code bundled into the package. what you can do is use extras to prevent all the gui dependencies from being installed when the user isn't using the gui.

To make the error pretty clear for the user i've put something like this in optional modules __init__.py

try:  
    import some_gui_depedency  ...
except ModuleNotFoundError:
    raise Exception("Gui dependencies not installed, use `poetry add <package>  -e gui` to use Gui 

generally python code is tiny so it doesn't really matter that your shipping code that won't work, its usually the dependencies that have lots of binaries etc. thats really what makes things take a while to install.

If your trying to hide the gui code from the user (maybe for commercial reasons?) you will need to make them seperate packages.

本文标签: pythonHow to specify package as an extra in poetrypyprojecttomlStack Overflow