没有名为_mysql的模块 - Google App Engine& Django的 [英] No module named _mysql - Google App Engine & Django

查看:189
本文介绍了没有名为_mysql的模块 - Google App Engine& Django的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先我正在使用 Mac(Yosemite)



我创建了一个简单的 Django 项目与 Google App Engine 。我正在开发环境中使用 Cloud SQL MySQL 文档中的建议。当然,该项目在我的开发机器上使用 virtualenv



我可以使用Django内置的 runserver 命令没有问题,即使我将其部署到GAE一切都很好。



但是,如果我使用GAE启动器运行开发服务器,那么当我尝试加载时会引发错误一个页面:

 不正确的配置:加载MySQLdb模块时出错:没有名为_mysql的模块
/ pre>

我安装了GAE拥有的相同版本的MySQLdb。目前它的1.2.4。
MySQLdb可用于GAE SDK,所以我不知道为什么它不能导入_mysql。

解决方案

我终于找到一个解决方法。



只需将 MySQLdb 添加到您的 GAE lib中是不够的。 MySQLdb尝试导入 _mysql ,这是一个.so或.c文件,取决于您使用的MySQLDB的版本。好像直接导入它们是不正确的,我想。也许是因为它是一个低级别的C文件,正如在文档中所述,GAE不支持任何类型的C扩展。如果您知道更好的解释,请在评论部分与我们分享。



解决方案:



首先我不得不重命名 _mysql.so 。 Mine现在只是 mysql.so



然后在同一目录中创建一个名为 _mysql.py 的文件,把这段代码放在其中:

  def __bootstrap __():
全局__bootstrap__,__loader__,__file__
导入sys,pkg_resources,imp
__file__ = pkg_resources.resource_filename(__ name __,'mysql.so')#point重命名为_mysql.so
__loader__ = None; del __bootstrap__,__loader__
imp.load_dynamic(__ name __,__ file__)
__bootstrap __()

GAE应该导入现在的MySQLdb没有错误。


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.

解决方案

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.

这篇关于没有名为_mysql的模块 - Google App Engine& Django的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆