Python – ImportError running Google’s python appengine on Ubuntu

google-app-enginelinuxpython

I'm trying to teach myself python using Google's AppEngine, and I can't get the dev server running. I get this error:

Traceback (most recent call last):
File "/opt/google_appengine/google_appengine_1.2.7/dev_appserver.py",
line 60, in
run_file(file, globals()) File
"/opt/google_appengine/google_appengine_1.2.7/dev_appserver.py",
line 57, in run_file
execfile(script_path, globals_) File
"/opt/google_appengine/google_appengine_1.2.7/google/appengine/tools/dev_appserver_main.py",
line 65, in
from google.appengine.tools import os_compat ImportError: cannot import
name os_compat

Ubuntu 9.10 comes with python2.6 (didn't work), and I installed python2.5 (didn't work), and have tried running it with python dev_appserver.py helloWorld (didn't work) as well as running dev_appserver.py after editing the first line to be:

#!/usr/bin/env python2.5

I can't seem to find anything online with this error. The only problem I've found is about using python 2.5, and I think I've solved that.

Kyle suggested I need to set my PYTHONPATH variable. After running

export PYTHONPATH=/opt/google_appengine/google_appengine_1.2.7

I still get the same error trying to run dev_appserver.py. Am I setting PYTHONPATH wrong? Alternatively, how do I uninstall the protocol buffers python project? I have no use for Ubuntu One and had already uninstalled it.

Best Answer

The problem appears to be the fact that Karmic Koala 9.10 (the latest version of Ubuntu) ships with Ubuntu One, a python app that depends on Google's protocol buffers library. The python-protobuf package provides the google.protobuf package in /usr/lib/pymodules/python2.6.

Unfortunately, the AppEngine SDK includes another package called google.appengine. So somewhere in your code, the google package is being imported, and the package that contains protobuf is being found on PYTHONPATH first. Python caches the first package it finds in sys.modules, so the second google package in the SDK will never be imported.

You could move the google AppEngine SDK up to the front of your PYTHONPATH. That should ensure that Python finds the google.appengine package instead of the package provided by python-protobuf.

PYTHONPATH=/opt/google_appengine/google_appengine_1.2.7 \
    python dev_appserver.py helloWorld

This is a bug that should be reported to the AppEngine SDK project.

Update: I've submitted a bug against the AppEngine API.