加载MySQLdb模块时出错

加载MySQLdb模块时出错

我只是想在我的笔记本电脑(Windows 10,pycharm)上从github签出我正在研究的项目。我安装了每个库及其运行所需的连接器,尝试运行server并获取以下信息:

    Unhandled exception in thread started by <function wrapper at 0x046FDEF0>
Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\django\utils\autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "C:\Python27\lib\site-packages\django\core\management\commands\runserver.py", line 113, in inner_run
    autoreload.raise_last_exception()
  File "C:\Python27\lib\site-packages\django\utils\autoreload.py", line 249, in raise_last_exception
    six.reraise(*_exception)
  File "C:\Python27\lib\site-packages\django\utils\autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "C:\Python27\lib\site-packages\django\__init__.py", line 27, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:\Python27\lib\site-packages\django\apps\registry.py", line 108, in populate
    app_config.import_models(all_models)
  File "C:\Python27\lib\site-packages\django\apps\config.py", line 199, in import_models
    self.models_module = import_module(models_module_name)
  File "C:\Python27\lib\importlib\__init__.py", line 37, in import_module
    __import__(name)
  File "C:\Python27\lib\site-packages\django\contrib\auth\models.py", line 4, in <module>
    from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
  File "C:\Python27\lib\site-packages\django\contrib\auth\base_user.py", line 52, in <module>
    class AbstractBaseUser(models.Model):
  File "C:\Python27\lib\site-packages\django\db\models\base.py", line 119, in __new__
    new_class.add_to_class('_meta', Options(meta, app_label))
  File "C:\Python27\lib\site-packages\django\db\models\base.py", line 316, in add_to_class
    value.contribute_to_class(cls, name)
  File "C:\Python27\lib\site-packages\django\db\models\options.py", line 214, in contribute_to_class
    self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
  File "C:\Python27\lib\site-packages\django\db\__init__.py", line 33, in __getattr__
    return getattr(connections[DEFAULT_DB_ALIAS], item)
  File "C:\Python27\lib\site-packages\django\db\utils.py", line 211, in __getitem__
    backend = load_backend(db['ENGINE'])
  File "C:\Python27\lib\site-packages\django\db\utils.py", line 115, in load_backend
    return import_module('%s.base' % backend_name)
  File "C:\Python27\lib\importlib\__init__.py", line 37, in import_module
    __import__(name)
  File "C:\Python27\lib\site-packages\django\db\backends\mysql\base.py", line 28, in <module>
    raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb


安装在我机器上的Pip库:

    pip freeze
altgraph==0.10.2
appdirs==1.4.2
automa==0.1.21
bdist-mpkg==0.5.0
click==6.7
CmdUtils==0.1
decorator==4.0.11
Django==1.10.5
django-bootstrap-themes==3.3.6
django-bootstrap3==7.1.0
django-console==0.4.7
django-mysql==1.1.0
django-mysql-manager==0.1.4
django-mysql-pymysql==0.1
django-registration-redux==1.4
DjangoMySQLDump==0.1
future==0.16.0
HTSQL==2.3.3
httpsproxy-urllib2==1.0
humanfriendly==2.4
Jinja2==2.9.5
macholib==1.5.1
MarkupSafe==0.23
modulegraph==0.10.4
monotonic==1.2
mysql==0.0.1
mysql-autodoc==0.2
mysql-autoxtrabackup==1.4.5
mysql-connector==2.1.4
mysql-connector-python==2.1.5
mysql-connector-python-rf==2.2.2
mysql-connector-repackaged==0.3.1
mysql-corsair==0.0.36
MySQL-python==1.2.5
mysql-replay==0.1.24
mysqlclient==1.3.9
mysqli==0.5.3
networkx==1.11
nltk==3.2.1
nltk-tgrep==1.0.6
packaging==16.8
pid==2.1.1
py2app==0.7.3
PyMySQL==0.7.10
pyparsing==2.1.10
python-dateutil==1.5
pytz==2013.7
PyYAML==3.12
rake-nltk==1.0.0
requests==2.11.1
selenium==3.0.2
six==1.10.0
termcolor==1.1.0
urllib2-file==0.2.1
urllib2-kerberos==0.1.6
urllib2-prior-auth==0.2.0
virtualenv==15.1.0
zope.interface==4.1.1


知道我该如何解决吗?

最佳答案

在settings.py中,在import os下方添加以下内容。

try:
    import pymysql
    pymysql.install_as_MySQLdb()
except:
    pass

关于mysql - Django,mysql;加载MySQLdb模块时出错,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42900979/

10-09 04:26