Git Product home page Git Product logo

Comments (6)

pfmoore avatar pfmoore commented on July 21, 2024 3

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).

from sampleproject.

dmtucker avatar dmtucker commented on July 21, 2024 2

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()

from sampleproject.

seancmonahan avatar seancmonahan commented on July 21, 2024

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

from sampleproject.

seancmonahan avatar seancmonahan commented on July 21, 2024

Pull request #67

from sampleproject.

seancmonahan avatar seancmonahan commented on July 21, 2024

@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?

from sampleproject.

dmtucker avatar dmtucker commented on July 21, 2024

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.

from sampleproject.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.