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.

7 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 Daniel Arbuckle said...

It may be that the __future__ import will be implemented in upcoming versions of 2.4.

11:12 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  
Blogger I-Wanna-Blog said...

Hi Brandon King, I found your blog post PEP 238 - import .relative whilst looking for info on accounting software. Yours is an interesting Blog and I can see why it came up when i use 'accounting software' in the search. Thanks for a great read, I'll be back for more! Bye for now.

5:53 PM  
Blogger Alex said...

Hey, landed on your blog, nice stuff. I found a cool new tool for our blogs... www.widgetmate.com It helps get latest news for our keywords directly on to our blog. I added it on mine. Worked like a charm.

11:36 AM  
Blogger dghnfgj said...

Joy in warcraft leveling living comes wow lvl from having wow lvl fine emotions,wow power level trusting them,power leveling giving them power leveling the freedom of wrath of the lich king power leveling a bird in the open.wlk power leveling Joy in living can age of conan gold never be assumed as a pose,or put on from guildwars gold the outside as a mask. People who have this joy don not need maple story mesos to talk about it; they radiate it. wow gold They just live out their joy and let wow power leveling it splash its sunlight and glow into other lives as naturally as bird sings.

6:00 PM  

Post a Comment

<< Home