Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
smheidrich opened this issue Oct 5, 2024 · 1 comment · May be fixed by #1606
Open

Comments

@smheidrich
Copy link

A common problem for packages with optional dependencies / extras is that the default user experience when these dependencies haven't been installed by the package consumer isn't great: the end user will just get an ordinary ModuleNotFoundError stemming from the first import of a missing module, without a hint that this module is part of an extra or how to fix it.

As a result, authors of packages with optional dependencies often want to introduce custom handling for missing dependencies, such as raising a more helpful exception that includes the name of the extra that is missing in case of libraries, or showing a more helpful message to the end user in case of applications.

I believe it would make a lot of sense to include guidance on how to do this in the Packaging Guide.

One problem is that there doesn't currently seem to be a good good way of doing this until pypa/packaging-problems#317 has been fixed, but a guide showing the various bad ways and pointing to tickets relevant to future improvements is better than users having to piece the information together from 10 different StackOverflow answers and GitHub issues.

@sinoroc
Copy link
Contributor

sinoroc commented Oct 5, 2024

I guess the recommendation would be simple and something like this:

try:
    import some_library
except ImportError, ModuleNotFoundException:
    handle_missing_dependency()

But the handle_missing_dependency can only be case by case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants