[Import-SIG] Revising PEP 395 to handle implicit package directories

Nick Coghlan ncoghlan at gmail.com
Tue Mar 13 23:37:29 CET 2012


On Wed, Mar 14, 2012 at 7:23 AM, Guido van Rossum <guido at python.org> wrote:
> FWIW, Thomas Wouters disagrees with the goal of PEP 395 -- he would
> much rather issue an error message when you're trying to execute a
> file living inside a package (without using -m) instead of Nick's
> proposal to fix the situation. But that's a second-order issue; Eric's
> algorithm (or some variant of it) might work for either approach.
> Anyway, I'd like to stay out of *that* particular discussion -- either
> way it doesn't affect my position on namespace packages.

Your position on namespace packages definitely affects this part of
PEP 395, though :)

My current thought is that I'll revise the affected portion of the PEP
to add a "importlib._package_hint()" (or similar) that gets invoked to
display a message on stderr when either __main__ or a command at the
interactive prompt raises ImportError. This is still just the
glimmering of an idea and there are a lot of practical details still
to figure out, but it should be feasible to produce errors like those
shown below.

Interactive prompt:

    The current working directory appears to be inside a Python
package. This can cause unexpected Import Errors and other strange
behaviour. Consider using "os.chdir('<path>')" to move the working
directory to the parent directory of the package and accessing modules
under their full name.

Module/package execution via -m:

    The current working directory appears to be inside a Python
package. This can cause unexpected Import Errors and other strange
behaviour. Consider changing the working directory to the parent
directory of the package ("<path>") and running the command as "python
-m <full name>".

Direct execution:

    The script directory appears to be inside a Python package. This
can cause unexpected Import Errors and other strange behaviour.
Consider changing the working directory to the parent directory of the
package ("<path>") and running the command as "python -m <full name>".

Cheers,
Nick.

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


More information about the Import-SIG mailing list