skip to Main Content

I am trying to run my Django app on Google App Engine SDK (locally) inside a virtualenv with MySQL as a database. Everything in my requirements.txt file is installing perfectly. When I start the Google App Engine SDK environment, it throws me a nasty error that seems common, but Stack Overflow doesn’t have any examples of solving this within Google App Engine SDK yet.

Here is my work flow from the root dir of my project…

virtualenv venv && mkdir lib

source venv/bin/activate

pip install -r requirements.txt -t lib/ && pip install -r requirements.txt

When I run the following command to start up the SDK….

dev_appserver.py app.yaml

I get the following error in my traceback…

File "/Users/username/Repositories/projectname/lib/django/db/utils.py", line 211, in __getitem__
backend = load_backend(db['ENGINE'])
File "/Users/username/Repositories/projectname/lib/django/db/utils.py", line 115, in load_backend
INFO     2018-06-26 20:09:30,812 module.py:846] default: "GET /_ah/start HTTP/1.1" 500 -
return import_module('%s.base' % backend_name)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/Users/username/Repositories/projectname/lib/django/db/backends/mysql/base.py", line 30, in <module>
'Did you install mysqlclient or MySQL-python?' % e
ImproperlyConfigured: Error loading MySQLdb module: No module named _mysql.
Did you install mysqlclient or MySQL-python?

My requirements.txt

Django==1.11.8
djangorestframework==3.8.2
facebook-sdk
oauth2client==2.0.1
google-api-python-client==1.6.2
facebookads==2.11.1
httplib2==0.10.3
enum==0.4.6
requests-toolbelt==0.8.0
google-cloud-storage==1.6.0
google-resumable-media==0.3.1
google-auth
requests==2.18.0
lxml==3.8.0
pycrypto==2.6.1
MySQL-python==1.2.5

Contents of lib/
Contents of <code>lib/</code>

I am calling MySQLdb in my app.yaml as well…

libraries:
- name: MySQLdb
  version: "latest"

Contents of appengine_config.py

# [START vendor]
from google.appengine.ext import vendor

vendor.add('lib')
# [END vendor]

Some things I have checked off the list while debugging…

1) MySQL-python==1.2.5 is installed when I do a pip freeze in my virtual environment.

2) MySQL is installed and works perfectly on my local computer.

3) I’ve looked through the lionshare of Stack Overflow’s questions thus far and none of them seem to help.

2

Answers


  1. The installation was set up according to Google recommendations and it contained no visible errors. The problem could not be reproduced on external environment (a similar Django setup worked fine), so we just came to a workaround, see it below (set PYTHONPATH).

    GAE requests installing third-party libraries into the lib/ directory, making it a poor man’s virtual environment. Then lib/ is being added to a module search path by calling vendor.add('lib') in the appengine_config.py.

    That worked correctly in the author’s setup, so GAE managed to import Django from lib/django/. Then this working config failed misteriously for importing MySQLdb, which was surely installed into the same lib/.

    During the investigation we have checked the idea if PYTHONPATH environment variable could affect anything. It did not, but setting it fixed the issue:

    export PYTHONPATH=/Users/username/Repositories/projectname/lib/
    

    Note: avoid using relative directories in PATH-like environment variables (./lib in Erik’s comment), that creates a security flaw.

    Login or Signup to reply.
  2. The MySQL-Python which is a C extension,will probably fail(No module named _mysql).
    you can give a try with pymysql module

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search