Shouldn't the imports be absolute by default in python27?

Imagine the directory structure:


File /a/ looks like:

import c
should_be_absolute = c

All the other files (including __init__) are empty.

When running a test script (using python 2.7):

import a.b
print a.b.should_be_absolute

with PYTHONPATH=/ from an empty directory (so nothing is added to PYTHONPATH from current directory) I get

<module 'a.c' from '/a/'>

where according to PEP 328 and the statement import <> is always absolute I would expect:

<module 'c' from '/'>

The output is as expected when I remove the /a/ file.

What am I missing? And if this is the correct behavior - how to import the c module from b (instead of a.c)?


According to python dev mailing list it appears to be a bug in the documentation. The imports are not absolute by default in python27.


you need to add from __future__ import absolute_import or use importlib.import_module('c') on Python 2.7

It is default on Python 3.

There was a bug in Python: and its documentation claim absolute imports became mandatory in 2.7, but they didn't.

If you are only adding / to your PYTHONPATH, then the search order could still be looking for c in the current directory. It would be a lot better if you placed everything under a root package, and referred to it absolutely:



So in your a.c you would do and of these:

from myPackage import c
from myPackage.c import Foo
import myPackage.c

This way, it is always relative to your package.

"Absolute" doesn't mean the one that you think it does; instead, it means that the "usual" package resolving procedure takes place: first, it looks in the directory of the package, then in all the elements of sys.path; which includes the elements from PYTHONPATH.

If you really want to, you can use tools like the imp module, but I'd recommend against it for something like this. Because in general, you shouldn't ever have to create a module with the same name as one in the standard Python distribution.

Need Your Help

Android Studio 2.2.2: All packages are not available for download

android android-studio sdk

I installed Android Studio 2.2.2 on my system. After when I openend one of the projects which have compileSdkVersion 23, got following error,

How can I implement Offset Path Effect in SVG without using Javascript or dilate/erode filters?

javascript vector svg effect

I have a long time project: a basic vector graphic tool which runs in browser and uses SVG and Javascript (maybe you have seen somekind of these elsewhere). The tool has only very limited set of