如何让Django使用mysqlclient而不是mysqlDB

时间:2015-05-22 13:41:37

标签: python mysql django pythonanywhere

我在PythonAnywhere,python 3.4,Django最新版本,virtualenv,使用mac os x。我尝试在PythonAnywhere上的virtualenv上安装mysqlclient和mysqlDB,mysqlDB失败,因为它与python3不兼容,mysqlclient已成功安装,但Django似乎无法检测mysqlclient并使用它代替mysqldb。这是错误日志:

2015-05-22 12:13:36,953 :Traceback (most recent call last):
2015-05-22 12:13:36,953 :  File "/usr/local/lib/python3.4/dist-packages/django/db/backends/mysql/base.py", line 14, in <module>
2015-05-22 12:13:36,954 :    import MySQLdb as Database
2015-05-22 12:13:36,954 :ImportError: No module named 'MySQLdb'
2015-05-22 12:13:36,955 :
2015-05-22 12:13:36,955 :During handling of the above exception, another exception occurred:
2015-05-22 12:13:36,955 :
2015-05-22 12:13:36,955 :Traceback (most recent call last):
2015-05-22 12:13:36,955 :  File "/bin/user_wsgi_wrapper.py", line 130, in __call__
2015-05-22 12:13:36,955 :    self.error_log_file.logger.exception("Error running WSGI application")
2015-05-22 12:13:36,955 :  File "/usr/lib/python3.4/logging/__init__.py", line 1296, in exception
2015-05-22 12:13:36,956 :    self.error(msg, *args, **kwargs)
2015-05-22 12:13:36,956 :  File "/usr/lib/python3.4/logging/__init__.py", line 1289, in error
2015-05-22 12:13:36,957 :    self._log(ERROR, msg, args, **kwargs)
2015-05-22 12:13:36,957 :  File "/usr/lib/python3.4/logging/__init__.py", line 1395, in _log
2015-05-22 12:13:36,958 :    self.handle(record)
2015-05-22 12:13:36,958 :  File "/usr/lib/python3.4/logging/__init__.py", line 1404, in handle
2015-05-22 12:13:36,959 :    if (not self.disabled) and self.filter(record):
2015-05-22 12:13:36,959 :  File "/usr/lib/python3.4/logging/__init__.py", line 692, in filter
2015-05-22 12:13:36,960 :    for f in self.filters:
2015-05-22 12:13:36,960 :  File "/bin/user_wsgi_wrapper.py", line 122, in __call__
2015-05-22 12:13:36,960 :    app_iterator = self.app(environ, start_response)
2015-05-22 12:13:36,960 :  File "/usr/local/lib/python3.4/dist-packages/django/core/handlers/wsgi.py", line 187, in __call__
2015-05-22 12:13:36,961 :    self.load_middleware()
2015-05-22 12:13:36,961 :  File "/usr/local/lib/python3.4/dist-packages/django/core/handlers/base.py", line 45, in load_middleware
2015-05-22 12:13:36,962 :    mw_class = import_by_path(middleware_path)
2015-05-22 12:13:36,962 :  File "/usr/local/lib/python3.4/dist-packages/django/utils/module_loading.py", line 21, in import_by_path
2015-05-22 12:13:36,963 :    module = import_module(module_path)
2015-05-22 12:13:36,963 :  File "/usr/lib/python3.4/importlib/__init__.py", line 109, in import_module
2015-05-22 12:13:36,964 :    return _bootstrap._gcd_import(name[level:], package, level)
2015-05-22 12:13:36,964 :  File "<frozen importlib._bootstrap>", line 2231, in _gcd_import
2015-05-22 12:13:36,966 :  File "<frozen importlib._bootstrap>", line 2214, in _find_and_load
2015-05-22 12:13:36,966 :  File "<frozen importlib._bootstrap>", line 2203, in _find_and_load_unlocked
2015-05-22 12:13:36,966 :  File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
2015-05-22 12:13:36,966 :  File "<frozen importlib._bootstrap>", line 1129, in _exec
2015-05-22 12:13:36,967 :  File "<frozen importlib._bootstrap>", line 1448, in exec_module
2015-05-22 12:13:36,967 :  File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
2015-05-22 12:13:36,967 :  File "/usr/local/lib/python3.4/dist-packages/django/contrib/auth/middleware.py", line 3, in <module>
2015-05-22 12:13:36,972 :    from django.contrib.auth.backends import RemoteUserBackend
2015-05-22 12:13:36,973 :  File "/usr/local/lib/python3.4/dist-packages/django/contrib/auth/backends.py", line 3, in <module>
2015-05-22 12:13:36,974 :    from django.contrib.auth.models import Permission
2015-05-22 12:13:36,974 :  File "/usr/local/lib/python3.4/dist-packages/django/contrib/auth/models.py", line 48, in <module>
2015-05-22 12:13:36,975 :    class Permission(models.Model):
2015-05-22 12:13:36,975 :  File "/usr/local/lib/python3.4/dist-packages/django/db/models/base.py", line 96, in __new__
2015-05-22 12:13:36,976 :    new_class.add_to_class('_meta', Options(meta, **kwargs))
2015-05-22 12:13:36,976 :  File "/usr/local/lib/python3.4/dist-packages/django/db/models/base.py", line 264, in add_to_class
2015-05-22 12:13:36,977 :    value.contribute_to_class(cls, name)
2015-05-22 12:13:36,977 :  File "/usr/local/lib/python3.4/dist-packages/django/db/models/options.py", line 124, in contribute_to_class
2015-05-22 12:13:36,978 :    self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
2015-05-22 12:13:36,978 :  File "/usr/local/lib/python3.4/dist-packages/django/db/__init__.py", line 34, in __getattr__
2015-05-22 12:13:36,978 :    return getattr(connections[DEFAULT_DB_ALIAS], item)
2015-05-22 12:13:36,978 :  File "/usr/local/lib/python3.4/dist-packages/django/db/utils.py", line 198, in __getitem__
2015-05-22 12:13:36,979 :    backend = load_backend(db['ENGINE'])
2015-05-22 12:13:36,979 :  File "/usr/local/lib/python3.4/dist-packages/django/db/utils.py", line 113, in load_backend
2015-05-22 12:13:36,980 :    return import_module('%s.base' % backend_name)
2015-05-22 12:13:36,980 :  File "/usr/lib/python3.4/importlib/__init__.py", line 109, in import_module
2015-05-22 12:13:36,980 :    return _bootstrap._gcd_import(name[level:], package, level)
2015-05-22 12:13:36,980 :  File "/usr/local/lib/python3.4/dist-packages/django/db/backends/mysql/base.py", line 17, in <module>
2015-05-22 12:13:36,980 :    raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e)
2015-05-22 12:13:36,980 :django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named 'MySQLdb'

我的settings.py:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'chart',
        'USER': 'root',
        'PASSWORD': '',
        'HOST': 'localhost',
        'PORT': '3306',
    }
}

我已遍历了stackoverflow上的一些帖子,但似乎没有人解决我的问题。如果您有任何好的建议,请告诉我,谢谢。

3 个答案:

答案 0 :(得分:2)

堆栈跟踪表明您没有从virtualenv运行您的网络应用程序。您需要正确配置您的Web应用程序以使用特定的virtualenv。

此外,settings.py中的连接属性将不起作用。正确的连接详细信息位于您帐户的“数据库”选项卡上。

答案 1 :(得分:1)

真相发现:它实际上是PythonAnywhere的配置问题,他们有自己的MySQL配置你必须遵循,你不能使用mysqlclient,因为他们只使用Oracle mysql连接器。

感谢大家阅读和回答。

答案 2 :(得分:0)

没有“检测mysqlclient”这样的东西。 Mysqlclient是MySQLdb的一个分支,从Python的角度来看,它们是相同的模块,但只是不同的版本。

您可能在两个安装中都存在冲突的文件。我会完全删除MySQLdb和mysqlclient,然后只重新安装mysqlclient。这应该可以解决你的问题。