1
votes

First of all I'm working on Mac (Yosemite).

I've created a simple Django project with Google App Engine. I'm using Cloud SQL in production and MySQL in development environment as recommended in the docs. The project uses virtualenv on my dev machine of course.

I can run the project with Django's built in runserver command without a problem and even if I deploy it to GAE everything is fine.

However if I run the development server with GAE launcher it raises an error whenever I try to load a page:

ImproperlyConfigured: Error loading MySQLdb module: No module named _mysql

I installed the same version of MySQLdb that GAE has. Currently its 1.2.4. MySQLdb is available for GAE SDK so I have no idea why it cannot import _mysql.

1

1 Answers

6
votes

I've finally found a workaround.

Simply adding MySQLdb to your GAE lib is not enough. MySQLdb tries to import _mysql which is a .so or .c file depends on what version of MySQLdb you use. It seems like importing them directly is not working correctly I suppose. Maybe because it's a low-level C file and as it's stated in the docs GAE does not support any kind of C extensions. If you know a better explanation please share us in the comment section.

The solution:

First of all I had to rename _mysql.so. Mine is just mysql.so now.

Then create a file named _mysql.py in the same directory and put this code in it:

def __bootstrap__():
   global __bootstrap__, __loader__, __file__
   import sys, pkg_resources, imp
   __file__ = pkg_resources.resource_filename(__name__,'mysql.so') #point to your renamed _mysql.so
   __loader__ = None; del __bootstrap__, __loader__
   imp.load_dynamic(__name__,__file__)
__bootstrap__()

GAE should import now MySQLdb without errors.