2010-05-27

FIX: ImportError: cannot import name ImmutableList

Hi, it is NISHIO Hirokazu. I'm moving my Django app into Google App Engine with Django-nonrel. I encountered the error and it took a lot of time from me. Google search didn't know anything about the problem.

The version of Django in Google App Engine is 1.0. Thus that of Django-nonrel(djangoappengine) is 1.2. It is the root of the problem.

Of cource, some code to avoid such a versioning problem was wrote in Django-nonrel. See djangoappengine/main/main.py


parent_dir = os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
f parent_dir not in sys.path:
sys.path.insert(0, parent_dir)

# Remove the standard version of Django
if 'django' in sys.modules and sys.modules['django'].VERSION < (1, 2):
for k in [k for k in sys.modules
if k.startswith('django\.') or k == 'django']:
del sys.modules[k]


If sys.path doesn't have the path to Django in Django-nonrel, it add the path. If already old Django was imported, it remove that. But unfortunately, in my case, sys.path has the path, and the path to Django in Google App Engine preceded, but it's not imported yet.

It's mystery. I can't tell why it happened. But it actually happened.

I wrote a workaround for the problem. It bring the path to correct Django always on top.


parent_dir = os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
if parent_dir in sys.path:
sys.path.remove(parent_dir)
sys.path.insert(0, parent_dir)

1 comment:

Anonymous said...

This is not recommended to modify main.py. See discussion at

http://groups.google.com/group/django-non-relational/browse_thread/thread/d46e7ef843cdf944

If some handlers modify sys.path the root must be inserted in the beginning:

sys.path.insert(0, root)