Packages and modules

Peter Hansen peter at engcorp.com
Tue Jul 26 11:47:11 EDT 2005


Dan wrote:
>>>no executable code in
>>>__init__.py is executed, even though "import test" seems to succeed.
> 
> I've discovered that "import test" *does* cause executable code in the
> package to be executed. However, I can't execute it on the command line
> using "python test". Is there a way to do this?

Using the latest version of Python, "python -m test" should do it, 
though I don't know if that works for packages, or just modules.  Hang 
on... appears to work only for modules.

Okay, this should be universal, if slightly more awkward:

   python -c "import xyz"

The problem with this approach is that it won't execute the "if __name__ 
== '__main__':" code at the end, so if you want to execute a particular 
function directly, just add the call manually:

   python -c "import xyz; xyz.main()"

>>There _is_ a standard library package called test
> Oh, you're right. But I've renamed the module to XYZ and I still have
> the problem.

Which problem?



More information about the Python-list mailing list