[Import-SIG] Where to discuss PEP 382 vs. PEP 402 (namespace packages)?

Nick Coghlan ncoghlan at gmail.com
Mon Mar 12 05:23:10 CET 2012

On Mon, Mar 12, 2012 at 12:39 PM, Guido van Rossum <guido at python.org> wrote:
> I'm leaning towards PEP 402 or some variant. Let's have a pow-wow at
> the sprint tomorrow (I'll arrive in Santa Clara between 10 and 10:30).
> I do want to understand Nick's argument better; I haven't studied PEP
> 395 yet.

To save you some reading, the affected part of PEP 395 is the bit
where I want to make it so that direct execution of modules inside
packages just *works*. Currently, most ways of running code inside
packages will do the wrong thing because sys.path[0] is set to a
directory *inside* the package, thus all the module naming and
referencing gets thrown out of whack. Absolute imports fail because
the top-level package isn't on sys.path, while explicit relative
imports also fail, because the interpreter thinks __main__ is a top
level module. My experience answering questions on Stack Overflow is
that this is a major source of confusion that leads directly to the
perception that Python packages are hard to use (far more so than the
requirement to explicitly mark package directories with __init__.py

The only current way to get the paths to work out properly is to use
"-m" to invoke the code with the current working directory set to the
directory that contains the top-level package. That way, sys.path[0]
is set to a value that permits absolute imports of the top-level
package and "-m" ensures __main__.__package__ is set correctly so that
explicit relative imports work.

What I realised when I wrote PEP 395 is that, with explicitly marked
package directories, there's enough information already in the
filesystem for the interpreter to figure out what's going on and do
the right thing *automatically*. So we actually have the power to set
sys.path[0] and __main__.__package__ appropriately, even when the main
module is specified by file name rather than module name, or the
current working directory isn't the one that contains the top level
package directory.

PEP 402 effectively proposes to eliminate one minor source of beginner
confusion (the need to explicitly mark package directories) at the
expense of entrenching a bigger one (the fact that most means of
invoking a submodule as a script don't actually work because they
initialise the import system incorrectly). Once I realised that, my
preference switched firmly back to PEP 382 (since adjusting PEP 395 to
handle PEP 382 style layouts is a trivial tweak to the package
directory detection).


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

More information about the Import-SIG mailing list