Repository navigation
Conversation
| To keep your package usable without optional dependencies, import them only where | ||
| needed, and handle their absence explicitly. If a user requests a feature whose | ||
| dependencies are missing, provide an error message explaining which extra to | ||
| install, for example, ``your-project-name[gui]``. |
There was a problem hiding this comment.
I feel like this is easy to mess up. IMO this is the only good way to do this:
try:
import thing
except ModuleNotFound as err:
err.add_note("Install with your-project-name[gui] to get this dependency")
raiseYou should not throw a new error. Which I think this statement might encourage. Also this suggestion will be wrong if using conda, etc.
There was a problem hiding this comment.
Also this suggestion will be wrong if using conda, etc.
Yeah that's why I would not do that. It makes little sense in an open source package at least to have PyPI-specific instructions, that's a no-no in my book. I'd just say something like "Please install optdep to use the called_func function".
I'd be fine with leaving it out, but if we want it in, it must be done in an ecosystem-independent way.
There was a problem hiding this comment.
I took this over from the bigger PR, but in hindsight I'm unhappy with my own PR here. Do we really want to explain this? It feels like library design more than packaging.
There was a problem hiding this comment.
Yeah. I'd agree. In scientific-python, I'd put things like this in the patterns section, and we don't really have that sort of a page here. I think at most maybe "Keep your package usable without optional dependencies." could be added to the previous paragraph, but I could also see that being implied by the word "optional" and being just a library design issue.
There was a problem hiding this comment.
Agreed. I changed it to the one-sentence version, but I'm fine with closing this PR as well. I can see the case for both.
Closes gh-1605
Supersedes and closes gh-1606
📚 Documentation preview 📚: https://python-packaging-user-guide--2207.org.readthedocs.build/en/2207/