【问题标题】:Installing Django on Shared Server: No module named MySQLdb?在共享服务器上安装 Django:没有名为 MySQLdb 的模块?
【发布时间】:2010-10-20 17:33:38
【问题描述】:

我收到了这个错误

Traceback (most recent call last):
  File "/home/<username>/flup/server/fcgi_base.py", line 558, in run
  File "/home/<username>/flup/server/fcgi_base.py", line 1116, in handler
  File "/home/<username>/python/django/django/core/handlers/wsgi.py", line 241, in __call__
    response = self.get_response(request)
  File "/home/<username>/python/django/django/core/handlers/base.py", line 73, in get_response
    response = middleware_method(request)
  File "/home/<username>/python/django/django/contrib/sessions/middleware.py", line 10, in process_request
    engine = import_module(settings.SESSION_ENGINE)
  File "/home/<username>/python/django/django/utils/importlib.py", line 35, in import_module
    __import__(name)
  File "/home/<username>/python/django/django/contrib/sessions/backends/db.py", line 2, in ?
    from django.contrib.sessions.models import Session
  File "/home/<username>/python/django/django/contrib/sessions/models.py", line 4, in ?
    from django.db import models
  File "/home/<username>/python/django/django/db/__init__.py", line 41, in ?
    backend = load_backend(settings.DATABASE_ENGINE)
  File "/home/<username>/python/django/django/db/__init__.py", line 17, in load_backend
    return import_module('.base', 'django.db.backends.%s' % backend_name)
  File "/home/<username>/python/django/django/utils/importlib.py", line 35, in import_module
    __import__(name)
  File "/home/<username>/python/django/django/db/backends/mysql/base.py", line 13, in ?
    raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e)
ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb

当我尝试在我的共享服务器上运行此脚本时

#!/usr/bin/python
import sys, os

sys.path.insert(0, "/home/<username>/python/django")
sys.path.insert(0, "/home/<username>/python/django/www") # projects directory

os.chdir("/home/<username>/python/django/www/<project>")
os.environ['DJANGO_SETTINGS_MODULE'] = "<project>.settings"

from django.core.servers.fastcgi import runfastcgi
runfastcgi(method="threaded", daemonize="false")

但是,我的虚拟主机几个小时前刚刚为我安装了MySQLdb。当我从 shell 运行 python 时,我可以导入 MySQLdb 就好了。为什么这个脚本会报告找不到它?

【问题讨论】:

    标签: python django mysql


    【解决方案1】:

    您的 python 路径中缺少 python-mysql 数据库驱动程序。 看看你是否能弄清楚 WSGI 看到的 pythonpath ......这可能与你在 shell 中遇到的不同

    【讨论】:

    • 啊,这是正确的,但我只是从 python shell 中print sys.path,然后将这些路径复制到我的脚本中。修复了这个问题,但又提出了另一个问题……
    • 没关系...我真的不应该将我的文件命名为 django.py :p
    【解决方案2】:

    是否有可能您的 settings.py 中有错误的 DATABASE_ENGINE 设置?那里应该是mysql而不是mysqldb。

    【讨论】:

      猜你喜欢
      • 2016-04-06
      • 2015-08-19
      • 2014-05-20
      • 2012-09-28
      • 2018-05-09
      • 2016-01-24
      • 1970-01-01
      • 2013-07-11
      • 1970-01-01
      相关资源
      最近更新 更多