3

我正在尝试在作为数据库的 Google App Engine SDK(本地)上运行我的 Django 应用virtualenv程序MySQL。我requirements.txt文件中的所有内容都安装得很好。当我启动 Google App Engine SDK 环境时,它向我抛出了一个看起来很常见的令人讨厌的错误,但 Stack Overflow 还没有任何在 Google App Engine SDK 中解决此问题的示例。

这是我项目根目录的工作流程...

virtualenv venv && mkdir lib

source venv/bin/activate

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

当我运行以下命令启动 SDK 时......

dev_appserver.py app.yaml

我在回溯中收到以下错误...

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?

我的要求.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

的内容lib/ Contents of <code>lib/</code>

我也在我的 app.yaml 中调用 MySQLdb ......

libraries:
- name: MySQLdb
  version: "latest"

appengine_config.py 的内容

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

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

我在调试时检查了一些事情......

1)在我的虚拟环境中MySQL-python==1.2.5安装时安装。pip freeze

2)MySQL已安装并在我的本地计算机上完美运行。

3) 到目前为止,我已经浏览了 Stack Overflow 的大部分问题,但似乎都没有帮助。

4

2 回答 2

4

安装是根据Google 的建议设置的,没有明显的错误。该问题无法在外部环境中重现(类似的 Django 设置工作正常),所以我们只是找到了一种解决方法,见下文(设置PYTHONPATH)。

GAE 要求将第三方库安装到lib/目录中,使其成为穷人的虚拟环境。然后通过lib/调用vendor.add('lib').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.

于 2018-07-04T10:04:07.277 回答
0

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

于 2018-07-05T17:34:19.330 回答