Wednesday, June 29, 2005

PEP 238 - import .relative

Python Imports

PEP 238 which has been accepted and may have an impact on any Python packages you've made so far, depending on your import style. This PEP will change the way imports work in a package. By default, all imports will be concidered absolute. Previously, if you had a Python package named 'Foo' with modules 'Bar' and 'Fig', you could import 'Fig' from the 'Bar' module by typing 'import Fig'. Once this new feature is implemented in Python 2.5, it will look for 'Fig' in sys.path first. They also plan to implement relative imports as well. With this you will be able to import the 'Fig' module from 'Bar' by typing 'import .Fig' for more a better example, see Guido's Decision or read the entire PEP.

Other features include importing groups like:

from os import (path,
sys,
mkdir)

Which allows for easyier multi-line imports. Check out the PEP for additional information.

Note that you can use the new features in Python 2.4 using:

  'from __future__ import absolute_import'

In Python 2.5, any package that results in a relative import from a module from within a package will result in a DeprecationWarning. Python 2.6 will include the fully implemented and on by default PEP 238.

PEP 238 - Abstract

The import statement has two problems:

  • Long import statements can be difficult to write, requiring
    various contortions to fit Pythonic style guidelines.

  • Imports can be ambiguous in the face of packages; within a package,
    it's not clear whether import foo refers to a module within the
    package or some module outside the package. (More precisely, a local
    module or package can shadow another hanging directly off
    sys.path.)

For the first problem, it is proposed that parentheses be permitted to
enclose multiple names, thus allowing Python's standard mechanisms for
multi-line values to apply. For the second problem, it is proposed that
all import statements be absolute by default (searching sys.path
only) with special syntax (leading dots) for accessing package-relative
imports.

Feedback

Let me know if this post was useful or not, so I'll know wether or not to post something like this again. I hope everyone finds this post to be useful.

3 Comments:

Blogger PJE said...

You should've checked Python 2.4; from __future__ import absolute_import doesn't exist.

The only part of PEP 238 implemented in Python 2.4 was the parentheses; everything else is deferred pending implementation. Also, I'm not 100% positive, but I think the fact that absolute imports aren't backward compatible really kills forced absolute importing prior to 3.0. (I personally will argue against it being implemented on that ground, as the current relative/absolute confusion is actually useful and used by Python programs now, e.g. to forcibly trick a module into seeing different module versions than it otherwise would.)

6:15 PM  
Blogger Grig Gheorghiu said...

Brandon,

I think your post is right on target as far as the goal as this blog is concerned. Keep posting :-) !!!

Grig

8:22 AM  
Blogger Brandon King said...

You'd think you could trust that an 'accepted' PEP would contain real and valid information.


>>> from __future__ import absolute_import
File "<stdin>", line 1
SyntaxError: future feature absolute_import is not defined


Well, good thing someone checked that. Any way, I hope the rest of the information was valuable. I'll test 'implemented features' before posting next time.

11:33 AM  

Post a Comment

<< Home