Python 加载 MySQLdb 模块时出错:没有名为“MySQLdb”的模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39574813/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 22:28:45  来源:igfitidea点击:

Error loading MySQLdb module: No module named 'MySQLdb'

pythonmysqldjangopython-3.x

提问by Amandeep Dhiman

I have tried a lot to solve this issue but I did not solve it. I have searched a lot on google and stackoverflow, no option is working for me. Please help me. Thanks in advance. I am using django 1.10, python 3.4. I have tried :

我已经尝试了很多来解决这个问题,但我没有解决它。我在 google 和 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.
  1. pip 安装 mysqldb。
  2. 点安装mysql。
  3. pip 安装 mysql-python。
  4. pip 安装 MySQL-python。
  5. easy_install mysql-python。
  6. easy_install MySQL-python。

Anything else left ?

还剩下什么?

      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'

采纳答案by Yonsy Solis

MySQLdb is only for Python 2.x. You can't install in Python 3.x versions. Now from your question i can see that you are working with Django. In this case you have three alternatives, from Django mysql notes:

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

  • mysqldb
  • mysqlclient
  • mysql-connect-python
  • 数据库
  • mysql客户端
  • mysql-connect-python

This gives to you two alternatives, mysqlclient and mysql-connect-python, The first one requires compilation from extensions for the plugin and, in Windows, this implies VStudio Libraries and a know-how for compile native extensions.

这为您提供了两个选择,mysqlclient 和 mysql-connect-python,第一个需要从插件的扩展中编译,在 Windows 中,这意味着 VStudio 库和编译本机扩展的专业知识。

mysql-connect-python is not compiled (and i don't recommend this for production, maybe only for dev) so you are going to need to install this.

mysql-connect-python 未编译(我不建议将其用于生产,可能仅用于开发),因此您将需要安装它。

You can try:

你可以试试:

pip3 install mysql-connect-python

or

或者

pip3 install http://cdn.mysql.com/Downloads/Connector-Python/mysql-connector-python-2.0.4.zip

if the first one fails.

如果第一个失败。

回答by Vamei

MySQLdb is the interface to MySQL database. As mentioned by other posts, MySQLdb doesn't support Python 3.x. I used PyMySQL as the replacement. You need to install it first:

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

pip install PyMySQL

The next step is to replace 'MySQLdb' with 'pymysql' in all the codes, which is intimidating. Luckily, PyMySQL can be loaded as MySQLdb dyanamically. In order to achieve it in Django, you need to add the following lines to __init__.py file under the dir of the project's default app (If your have a project named 'myproject', add lines to myproject/myproject/init.py):

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

import pymysql
pymysql.install_as_MySQLdb()

This __init__.py would be executed when you run the Django project, and MySQLdb will be replaced. You problem is then solved.

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

回答by ettanany

You can use mysqlclientinstead of MySQLdb. MySqLdb is not compatible with Python 3.

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

pip install mysqlclient

回答by Daniel Roseman

MySQLdb is not compatible with Python 3. Use mysql-client or mysql-connect.

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

回答by lazy.coder

You can also try installing mysqlclient-pythondirectly from source:

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

  1. Download source by git clone or zipfile (URL - https://github.com/PyMySQL/mysqlclient-python.git).

  2. Customize site.cfg (you can give the path for mysql_config)

  3. python setup.py install

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

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

  3. python setup.py 安装

回答by ChAnDu353

use this package for python 2.7 on windows http://www.codegood.com/archives/129

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

回答by Jadhav Gaurav

MySQLdb is not available for Python 3.x

MySQLdb 不适用于 Python 3.x

I have CentOS server

我有 CentOS 服务器

This worked for me

这对我有用

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

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

Python 3.5.0 (default, Dec 12 2017, 17:00:35) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-18)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 
>>> import MySQLdb