[Python-Dev] Issue 4195: Can't execute packages with -m in Python 2.6/3.0

Nick Coghlan ncoghlan at gmail.com
Tue Nov 18 11:55:50 CET 2008

I need a release manager call on whether or not the proposed resolution
of this issue goes beyond what is acceptable for a bug fix in 2.6.1.

Short version:
- Python 2.5 allowed packages to be executed with -m, but in a broken way
- I disabled the broken way for 2.6, but didn't provide a replacement
- The patch attached to 4195 once again allows execution of packages
with -m, but in a clean way similar to the way directories and zipfiles
can now be executed
- I would like to commit that patch for 3.0/2.6.1, but I'm concerned
that it crosses the "no new features" line

Long version:

There was a bug in python 2.5 that allowed a package name to be passed
to the -m switch or runpy.run_module() and it would mostly work.

However, the 'mostly' was due to the fact that doing this put the
internal import machinery into a slightly inconsistent state: the
interpreter was running code from inside a package, but that package
wasn't actually present in sys.modules. This could lead to assorted hard
to trace import-related weirdness, similar to what you can get when
executing a file from inside a package by name. You would often get away
with it, but good luck figuring out what is happening if things go wrong
(and you aren't already an expert on Python import mechanics).

Since the ability to execute packages wasn't intentional, I added the
missing check to block it explicitly in 2.6 (but left it alone for 2.5).
It seemed like a really niche feature, so I didn't figure out a clean
replacement for it at the time.

Benjamin noticed this change earlier in the 2.6 release cycle, I pointed
out it was a deliberate change, and that's the way it stayed until after
2.6 was released.

After the release, Andi Vajda (from the JCC project [1]) contacted me,
and together we worked out a better implementation of package support
for the -m switch (and runpy.run_module) [2] by extending the
__main__.py approach used to support direct execution of zipfiles and
directories [3].

That's what I would like to add, since it nicely complements the ability
to execute zipfiles and directories, while also restoring the ability to
pass a package name to the -m switch (but in a way that keeps the import
machinery in a consistent state).


[1] http://pypi.python.org/pypi/JCC

[2] http://bugs.python.org/issue4195 (package execution with -m)
[3] http://bugs.python.org/issue1739468 (zipfile execution)

Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia

More information about the Python-Dev mailing list