This will be the second time in my life posting about almost exactly the same thing. The difference between this time and last time is about 9 months, lots of difficult learning, and the fact that for those past 9 months I have never had this issue and have frequently used MySQLdb flawlessly during the entire time period. I recognize that a similar question to this has been asked many times. I have read the responses and performed the suggested fixes. As of yet, I have still been unable to reach a successful conclusion, which is why I am posting my question.
Just to describe my environment: I'm running a Django app on Google App Engine locally using the Google App Engine Launcher. I'm using Mac OS X El Capitan.
When these apps are deployed on Google App Engine, they work flawlessly as intended. However, when running locally, I will consistently get the ImportError upon trying to run the app.
Let me list some of the things I have checked and/or tried to fix the problem:
-Added MySQLdb to "Libraries" section in app.yaml
-Checked PYTHONPATH at runtime to confirm that the proper directories are added to path.
-Add paths before importing as a dummy check
-Updated Google App Engine SDK
I've done even more, but can barely recall them now. When checking the path at runtime, I can clearly see the following path to the google_appengine SDK directory where MySQLdb resides:
- url: /favicon\.ico
- url: /static
- url: /.*
- name: django
- name: MySQLdb
from google.appengine.ext.webapp import util
from django.conf import settings
settings._target = None
app = django.core.handlers.wsgi.WSGIHandler()
if __name__ == '__main__':
found = True
found = False
print('Found: ' + str(found))
ImportError: No module named MySQLdb
After digging around for hours I have finally discovered the root issue. The only significant change that has occurred from the time that MySQLdb was working properly and the time that MySQLdb stopped working properly was upon my upgrading OS X to El Capitan.
otool -L /Library/Python/2.7/site-packages/_mysql.so
_mysql.so was searching for
libmysqlclient.18.dylib in the
/usr/lib directory when really it was located in
/usr/local/lib directory. As a quick fix, I created a symbolic link by running the following (all on one line btw):
sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/local/lib/libmysqlclient.18.dylib
Since El Capitan has implemented Apple's "rootless" mode, and the current user (me) is unable to write to
/usr/lib, creating the symlink in
/usr/local/lib is adequate and probably better for a Mac user.
After performing these steps, MySQLdb was able to load properly; my app initialized and ran successfully for the first time in a long while.