Python: Avoid __main__.py files as entry points.
#6091
Merged
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.
According to the official documentation, the purpose of
__main__.pyfiles is that their presence in a package (say,
foo) means one canexecute the package directly using
python -m foo(which will run theaforementioned
foo/__main__.pyfile).In principle this means that adding
if __name__ == "__main__"in thesefiles is superfluous, as they are only intended to be executed (and not
imported by some other file).
However, in practice people often do include the above construct.
Here are some instances of this on LGTM.com:
https://lgtm.com/query/7521266095072095777/
In particular, 10 out of 33 files in
cpythonhave this construct.This causes some confusion in our module naming, as we usually see the
presence of
__name__ == "__main__"as an indication that a file maybe run directly (and hence with "absolute import" semantics). However,
when run with
python -m, the interpreter uses the usual packagesemantics, and this leads to modules getting multiple names.
For this reason, I think it makes sense to simply exclude
__main__.pyfiles from consideration. Note that if there is a
#!line mentioningthe Python interpreter, then they will still be included as entry
points.
This is a minor change, and so I don't think this requires a change note.