Skip to content

Add a note in writing-pyproject-toml on how to handle missing extras - #2207

Open
rgommers wants to merge 2 commits into
pypa:mainfrom
rgommers:note-on-missing-extras
Open

rgommers wants to merge 2 commits into
pypa:mainfrom
rgommers:note-on-missing-extras

Conversation

@rgommers

@rgommers rgommers commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Closes gh-1605
Supersedes and closes gh-1606


📚 Documentation preview 📚: https://python-packaging-user-guide--2207.org.readthedocs.build/en/2207/

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]``.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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")
    raise

You should not throw a new error. Which I think this statement might encourage. Also this suggestion will be wrong if using conda, etc.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Should include guidance on how to handle missing optional dependencies / extras at runtime

2 participants