Fix version detection when invoked with python -m <package>
#1531
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
click.version_option attempts to determine the version using
pkg_resources
when no version is passed. This fails when the program was invoked withpython -m <package>
.Traceback
The reason version detection fails is that
__name__
in the calling module is"__main__"
, whileentry_point.module_name
is"<package>.__main__"
:click/src/click/decorators.py
Lines 265 to 269 in 19fdc85
click/src/click/decorators.py
Lines 288 to 293 in 19fdc85
This PR fixes the issue by prefixing the module name with
__package__
from the calling module, if it was"__main__"
to begin with.Minimal reproducible example:
# foobar/__init__.py (empty)