[Python-Dev] Draft: PEP for imports

Chris Reedy chrisandannreedy at comcast.net
Fri Jan 30 18:00:14 EST 2004


Armin Rigo wrote:

>  import neatTricks in "/home/guido/lib/python"      # no package
>  import package.module in "/home/guido/lib/python"  # package
>  import foo in "."                                  # relative import
>  from neatTricks in "../cmds" import a, b, c
>  s=os.path.join("some", "where"); import foo in s   # expression path
>
>where the semantics would be to search sys.path if and only if no 'in' clause
>is specified.  ('in' doesn't sound quite right...)
>
Armin -

Thinking about the semantics of the above, including corner cases, I 
find myself wanting to make the semantics of:

  <some_kind_of_import_statement> in "<path>"

to be equivalent to:

  sys.path.insert(0, "<path>")
  <some_kind_of_import_statement>
  del sys.path[0] # assuming the import doesn't modify sys.path

That doesn't seem to be quite equivalent to what you've proposed. On the 
other hand, that seems to provide at least semi-reasonable answers to 
questions about what happens to sys.modules, what happens if 
sys.modules['package'] is defined when importing package.module, what's 
the correct search path when a module being imported does an import, etc.

Having said that, that makes me feel negatively about the proposal, 
since you could do it yourself directly.

  Chris





More information about the Python-Dev mailing list