[Python-ideas] PEP for executing a module in a package containing relative imports

Josiah Carlson jcarlson at uci.edu
Fri Apr 20 19:38:37 CEST 2007


"Brett Cannon" <brett at python.org> wrote:
> I realized two things that I didn't mention in the PEP.
> 
> One is that Python will have to infer the proper package name for a
> module being executed.  Currently Python only knows the name of a
> module because you asked for something and it tries to find a module
> that fits that request.  But what is being proposed here has to figure
> out what you would have asked for in order for the import to happen.
> So I need to spell out the algorithm that will need to be used to
> figure out ``python bacon/__init__.py`` is the bacon package.  Using
> the '-m' option solves this as the name is given as an argument.

There's also the rub that if you 'run' the module in /a/b/c/d/e/f.py,
but all a-e are packages, the "proper" semantics may state that you need
to import a/__init__.py, a/b/__init__.py, etc., prior to the execution
of f.py .

Of course the only way that you would know that is if you checked the
paths .../e/, .../d/, etc.

The PEP should probably be changed to state the order of imports in a
case similar to this, and whether or not it bothers to check ancestor
paths for package information.


 - Josiah




More information about the Python-ideas mailing list