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

Move main out of __init__ #65

Closed
dmtucker opened this issue Feb 26, 2018 · 6 comments
Closed

Move main out of __init__ #65

dmtucker opened this issue Feb 26, 2018 · 6 comments

Comments

@dmtucker
Copy link
Contributor

It'd probably be better to encourage people to keep __init__.py minimal.

@seancmonahan
Copy link

Move it into __main__.py per https://docs.python.org/3/library/__main__.html?

@seancmonahan
Copy link

Pull request #67

@dmtucker
Copy link
Contributor Author

Personally, I would say __main__.py should be kept minimal also.
I like to define main in a cli module...

cli.py:

def main(argv=None):
    ...

__main__.py:

import sampleproject.cli

sampleproject.cli.main()

@pfmoore
Copy link
Member

pfmoore commented Mar 30, 2018

I think this is a matter of project choice. I don't think there's a particular best practice consensus here. So I'm inclined to keep things as they are as it's the simplest option.

Also, I don't like needing to import from __main__ if I want access to the main() function - the double underscores indicate that it's not something that we should normally be importing from.

The main() function is often needed, for testing or for programmatic access to an application (if the app supports it). Importing that either from the package directory or from a cli submodule seems natural to me - importing it as

    import foo.__main__

    foo.__main__.main([my, arguments, to, supply])

feels clumsy (I wouldn't do from foo.__main__ import main as that would probably clash with my own main function).

@seancmonahan
Copy link

@dmtucker

__main__.py still requires a if __name__ == '__main__': check because any setuptools-created executables will have a different __name__ such as sample.__main__. Btw, sorry the discussion has ended up in two places.

What do others think about just adding a __main__.py:

from . import main

if __name__ == '__main__':
    main()

?

As an aside, would a comment in __init__.py suggesting users consider moving their def main(): elsewhere be useful?

@dmtucker
Copy link
Contributor Author

dmtucker commented Apr 9, 2018

I think mimicking pip should generally be fine...
Personally, I find the extra if __name__ check redundant since __main__.py shouldn't be imported, but maybe that's just me. I would call trying to protect people who do weird things from themselves unpythonic.

My response on the PR is relevant too.

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

No branches or pull requests

4 participants