Fix version detection when invoked with python -m <package>#1531
Closed
cjolowicz wants to merge 2 commits into
Closed
Fix version detection when invoked with python -m <package>#1531cjolowicz wants to merge 2 commits into
python -m <package>#1531cjolowicz wants to merge 2 commits into
Conversation
ab99870 to
58d3932
Compare
Author
|
Rebased against 7.x and added changelog. |
added 2 commits
April 28, 2020 07:08
When the program is invoked with `python -m <package>`, click.version_option fails to determine the version using pkg_resources. The reason it fails is that __name__ in the calling module is "__main__", while entry_point.module_name is "<package>.__main__". Fix this issue by retrieving __package__ from the calling module and prefixing the module name with it, if __name__ is "__main__".
58d3932 to
cfd710f
Compare
Author
|
Rebased to resolve conflicts in |
Contributor
|
Thanks for the PR. There is a new PR for replacing pkg_resources with importlib_metadata, so this probably won't be added to master. This might still be relevant for 7.x though. |
Member
|
This PR won't apply after #1582, but the issue still happens. I've added a commit to that PR based on this fix and listed you as co-author. |
Author
|
Thanks for incorporating it 🚀 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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_resourceswhen 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_nameis"<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)