Absolute imports in Python 2.4

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Mon Jun 1 20:40:26 EDT 2009


I have a package which includes a module which shadows a module in the 
standard library. For example:

package
+-- __init__.py
+-- ham.py
+-- spam.py
+-- sys.py

Inside that package, I want to import the standard library sys. In other 
words, I want an absolute import. In Python 2.7, absolute imports will be 
the default, and "import sys" will import the standard library module. To 
get to the package.sys module, I'll need "from . import sys".

In Python 2.5 and 2.6, relative imports are the default, and package.sys 
will shadow the std lib version. I can say:

from __future__ import absolute_import

to use the Python 2.7 behaviour.

What can I do in Python 2.4 to get an absolute import?

I've read PEP 328 and googled, but haven't found any useful advice other 
than "well don't do that then". Plenty of pages complaining about 
relative imports, but I haven't found any work-arounds others than 
renaming the offending module. Are there any other ways around this?

http://www.python.org/dev/peps/pep-0328/



-- 
Steven



More information about the Python-list mailing list