PEP 338: Executing modules inside packages with '-m'

Nick Coghlan ncoghlan at
Mon Dec 13 12:10:36 CET 2004

Fredrik Lundh wrote:
> I'd say that for a typical user,
>     $ python -m arg
> is a marginal improvement over
>     $ python -c "import" arg

This doesn't work. Any code protected by "if __name__ == '__main__':" won't run 
in this context (since '' is being imported as a module, not run as a 

Even 'python -c "from import _main; _main()" arg' isn't quite right, 
since sys.argv[0] will be wrong (it will say '-c', instead of giving the 
module's filename). There's also the problem that there is no standard idiom for 
_main() functions.

> compared to
>     $ bar arg

This is true, but it has its own problems, mainly in the area of namespace 
conflicts on the packaging side:

1. Namespace conflicts between different Python versions
2. Namespace conflicts between different Python packages
3. Namespace conflicts between Python packages and other programs
4. Additional overhead to create an installed module that is usable as a script
   a. Add a shebang line for *nix style systems
   b. Think about how to deal with the previous 3 points
   c. Update the installer to copy the file to the right place with a good name
   d. Realise you're screwed on Windows, since you can't control the file 
associations and the script will always run with the default interpreter.

An extended -m, on the other hand deals with all those problems automatically:

   python -m arg     # Default interpreter, foo's bar
   python -m arg     # Default interpreter, bar's bar
   python24 -m arg   # Force Python 2.4, foo's bar
   python24 -m arg   # Force Python 2.4, bar's bar
   bar arg                   # Unrelated application called bar

Points 1, 3 & 4 were the justification for adding the current version of -m to 
Python 2.4 (obviously, point 2 didn't apply, since the current version doesn't 
work for modules insides packages). Specifically, it makes it trivial to get 
hold of the right version of pdb and profile for the interpreter you're working 

For usability, you can hide all of the above behind a menu item or desktop 
shortcut. However, the major target of the feature is Python developers rather 
than the end-users of applications built using Python.


Nick Coghlan   |   ncoghlan at   |   Brisbane, Australia

More information about the Python-list mailing list