-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
moved main() into __main__.py to keep __init__.py minimal #67
Open
seancmonahan
wants to merge
2
commits into
pypa:main
Choose a base branch
from
seancmonahan:master
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +0,0 @@ | ||
|
||
def main(): | ||
"""Entry point for the application script""" | ||
print("Call your main application code here") | ||
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
|
||
def main(): | ||
"""Entry point for the application script""" | ||
print("Call your main application code here") | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
__name__
will never not equal__main__
in__main__.py
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
__name__
equalssample.__main__
once the project is installed and run via the executable 'sample' generated by setup.py.Without the
if __name__ == "__main__":
check (but still callingmain()
at the end of__main__.py
):There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suppose the same effect could be had by reverting
__init__.py
andsetup.py
, and making__main__.py
be (like pip's__main__.py
):My motivation was twofold though:
python -m sample
__init__.py
as clean as possible.For example, pip's primary
__init__.py
is a single line definition of__version__
, andsetup.py
usespip._internal:main
as its console_script entrypoint. (I do see that ultimatelymain()
is defined insidepip/_internal/__init__.py
¯\_(ツ)_/¯ )Regardless of where
def main():
ends up, my first goal (to make the package runnable) requires the addition of a__main__.py
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, you're right about
__name__
during import...IMO, the double execution is evidence of abuse.
__main__.py
is meant to control the behavior ofpython -m
, yet here it is tangled up in the execution of the entry point.Instead of
__main__.py
and the entry point leading the user to the same call (e.g.sample.cli.main()
), the latter leads to the former which has to have a special hack added to make things work.The docs even describe
__main__.py
as the equivalent ofif __name__ == '__main__'
for packages (implying packages should not need it):There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, you're correct about the double-execution behavior. Seems that I was accidentally running from a virtualenv with an outdated install of sample with the console_script entrypoint pointing to
__main__
still! Should I close this pull request?Would a single patch for adding a
__main__.py
consisting solely ofbe accepted/useful? It would allow the package to be run via
python -m sample
and avoid the nonsense I would've introduced by movingdef main():
into__main__.py
.Also, would adding a
sample/cli.py
module be in the scope of this project? It seems like a useful way to make minimize__init__.py
to either an empty file or a singlefrom .cli import main
. (This does trigger a flake8 error for an unused import.)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that is a reasonable
__main__.py
, and I would say acli.py
module is in-scope for the project since someone previously defined anentry_point
.Not sure how we want to manage imports in
__init__.py
... I think generally people use that file to define the package API (which often implies unused imports), but it's not explicitly required since modules within the package can be imported independently. I'd probably just leave it empty to avoid a# noqa
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I still think this is not something that has an "obvious" best practice approach (as evidenced by the discussion going on here) and users would be better served with documentation that discussed the various approaches and the pros and cons, rather than having one particular approach added to this sample project. Let's also not forget that most projects don't even need to be runnable.
A new section in https://packaging.python.org/guides/ would likely be a better way of dealing with this, IMO.