packages, modules and double imports - oh my!

Chris Rebert clp2 at rebertia.com
Mon Oct 3 06:22:32 EDT 2011


On Mon, Oct 3, 2011 at 3:11 AM, Chris Withers <chris at simplistix.co.uk> wrote:
> Hi All,
>
> The attached package gives that smallest possible example of problems I'm
> hitting with some SQLAlchemy declarative classes.
>
> In short, I want to be able to do:
>
> python -m pack.module and have if the __name__=='__main__' block spit out
> the SQL to create the tables necessary for the modules in that class...
<snip>
> cwithers at cwlin:~> python -m pack
> /usr/bin/python: pack is a package and cannot be directly executed
>
> That's annoying, why isn't pack/__init__.py's __name__=='__main__' block
> executed?
>
> cwithers at cwlin:~> python pack
> /usr/bin/python: can't find '__main__.py' in 'pack'
>
> Wha? First I've ever heard of __main__.py... where's that documented?

http://docs.python.org/library/runpy.html :
"The runpy module['s] main use is to implement the -m command line switch"
"If the supplied module name refers to a package rather than a normal
module, then that package is imported and the __main__ submodule
within that package is then executed and the resulting module globals
dictionary returned."

(`runpy` having appeared in your first Traceback. Definitely seems
like some explicit cross-references should be added to the docs.)

Cheers,
Chris



More information about the Python-list mailing list