[Python-Dev] Running a module as a script

Nick Coghlan ncoghlan at email.com
Thu Sep 30 12:11:06 CEST 2004

Patch # 1035498 attempts to implement the semantics suggested by Ilya and
Anthony and co.

"python -m module"

- find the source file for the relevant module (using the standard locations for
module import)

- run the located script as __main__ (note that containing packages are NOT
imported first - it's as if the relevant module was executed directly from the
command line)

- as with '-c' anything before the option is an argument to the interpreter,
anything after is an argument to the script

The allowed modules are those whose associated source file meet the normal rules
for a command line script. I believe that means .py and .pyc files only (e.g.
"python -m profile" works, but "python -m hotshot" does not).

Special import hooks (such as zipimport) almost certainly won't work (since I
don't believe they work with the current command line script mechanism).


Nick Coghlan
Brisbane, Australia

More information about the Python-Dev mailing list