加载MySQLdb模块时出错:没有名为' MySQLdb'

时间:2016-09-19 13:47:28

标签: python mysql django python-3.x

我已经尝试了很多来解决这个问题,但我没有解决它。我在谷歌和stackoverflow上搜索了很多,没有选项对我有用。请帮我。提前致谢。我使用的是django 1.10,python 3.4。 我试过了:

  1. pip install mysqldb。
  2. pip install mysql。
  3. pip install mysql-python。
  4. pip install MySQL-python。
  5. easy_install mysql-python。
  6. easy_install MySQL-python。
  7. 还剩下什么?

          C:\Users\benq\Desktop\dimo-develop\Project>python manage.py runserver
    Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x0332D348>
    Traceback (most recent call last):
      File "C:\Python34\lib\site-packages\django\db\backends\mysql\base.py", line 25, in <module>
        import MySQLdb as Database
    ImportError: No module named 'MySQLdb'
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "C:\Python34\lib\site-packages\django\utils\autoreload.py", line 226, in wrapper
        fn(*args, **kwargs)
      File "C:\Python34\lib\site-packages\django\core\management\commands\runserver.py", line 109, in inner_run
        autoreload.raise_last_exception()
      File "C:\Python34\lib\site-packages\django\utils\autoreload.py", line 249, in raise_last_exception
        six.reraise(*_exception)
      File "C:\Python34\lib\site-packages\django\utils\six.py", line 685, in reraise
        raise value.with_traceback(tb)
      File "C:\Python34\lib\site-packages\django\utils\autoreload.py", line 226, in wrapper
        fn(*args, **kwargs)
      File "C:\Python34\lib\site-packages\django\__init__.py", line 18, in setup
        apps.populate(settings.INSTALLED_APPS)
      File "C:\Python34\lib\site-packages\django\apps\registry.py", line 108, in populate
        app_config.import_models(all_models)
      File "C:\Python34\lib\site-packages\django\apps\config.py", line 202, in import_models
        self.models_module = import_module(models_module_name)
      File "C:\Python34\lib\importlib\__init__.py", line 109, in import_module
        return _bootstrap._gcd_import(name[level:], package, level)
      File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
      File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
      File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
      File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
      File "<frozen importlib._bootstrap>", line 1129, in _exec
      File "<frozen importlib._bootstrap>", line 1471, in exec_module
      File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
      File "C:\Python34\lib\site-packages\django\contrib\auth\models.py", line 4, in <module>
        from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
      File "C:\Python34\lib\site-packages\django\contrib\auth\base_user.py", line 49, in <module>
        class AbstractBaseUser(models.Model):
      File "C:\Python34\lib\site-packages\django\db\models\base.py", line 108, in __new__
        new_class.add_to_class('_meta', Options(meta, app_label))
      File "C:\Python34\lib\site-packages\django\db\models\base.py", line 299, in add_to_class
        value.contribute_to_class(cls, name)
      File "C:\Python34\lib\site-packages\django\db\models\options.py", line 263, in contribute_to_class
        self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
      File "C:\Python34\lib\site-packages\django\db\__init__.py", line 36, in __getattr__
        return getattr(connections[DEFAULT_DB_ALIAS], item)
      File "C:\Python34\lib\site-packages\django\db\utils.py", line 212, in __getitem__
        backend = load_backend(db['ENGINE'])
      File "C:\Python34\lib\site-packages\django\db\utils.py", line 116, in load_backend
        return import_module('%s.base' % backend_name)
      File "C:\Python34\lib\importlib\__init__.py", line 109, in import_module
        return _bootstrap._gcd_import(name[level:], package, level)
      File "C:\Python34\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'
    

7 个答案:

答案 0 :(得分:27)

MySQLdb是MySQL数据库的接口。正如其他帖子所提到的,MySQLdb不支持Python 3.x.我使用PyMySQL作为替代品。您需要先安装它:

pip install PyMySQL

下一步是在所有代码中用'pymysql'替换'MySQLdb',这是令人生畏的。幸运的是,PyMySQL可以作为MySQLdb动态加载。为了在Django中实现它,你需要在项目的默认应用程序的目录下的__init__.py文件中添加以下行(如果你有一个名为'myproject'的项目,添加到myproject / myproject / init.py的行) :

import pymysql
pymysql.install_as_MySQLdb()

运行Django项目时会执行__init__.py,MySQLdb将被替换。然后你的问题就解决了。

答案 1 :(得分:11)

MySQLdb仅适用于Python 2.x.您无法在Python 3.x版本中安装。现在从你的问题我可以看到你正在使用Django。在这种情况下,您有三种选择,来自Django mysql notes

  • mysqldb
  • 的mysqlclient
  • 的MySQL-CONNECT-蟒

这给你两个选择,mysqlclient和mysql-connect-python,第一个需要从插件的扩展编译,在Windows中,这意味着VStudio库和编译原生扩展的专有技术。

没有编译mysql-connect-python(我不建议将其用于生产,也许只适用于开发),因此您需要安装它。

您可以尝试:

<p id="instructions">Click and drag either div to see automatic arrow adjustments.</p>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.0/jquery-ui.min.js"></script>
<svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100%">
  <defs>
    <marker id="arrowhead" viewBox="0 0 10 10" refX="3" refY="5"
        markerWidth="6" markerHeight="6" orient="auto">
      <path d="M 0 0 L 10 5 L 0 10 z" />
    </marker>
  </defs>
  <g fill="none" stroke="black" stroke-width="2" marker-end="url(#arrowhead)">
    <path id="arrowLeft"/>
    <path id="arrowRight"/>
  </g>
</svg>
<div id="a">Div 1</div>
<div id="b">Div 2</div>

pip3 install mysql-connect-python

如果第一个失败。

答案 2 :(得分:9)

您可以使用 mysqlclient 代替MySQLdb。 MySqLdb与Python 3不兼容。

pip install mysqlclient

答案 3 :(得分:2)

MySQLdb与Python 3不兼容。使用mysql-client或mysql-connect。

答案 4 :(得分:1)

您也可以尝试直接从源代码安装 mysqlclient-python

  1. 通过git clone或zipfile下载源代码(网址 - https://github.com/PyMySQL/mysqlclient-python.git)。

  2. 自定义site.cfg(您可以提供 mysql_config 的路径)

  3. python setup.py install

答案 5 :(得分:0)

在Windows上将此软件包用于python 2.7 http://www.codegood.com/archives/129

答案 6 :(得分:0)

MySQLdb不适用于Python 3.x

我有CentOS服务器

这对我有用

python3 -m pip install --user https://github.com/davispuh/MySQL-for-Python-3/archive/1.0.tar.gz

.AddCommand()