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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26560973/
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 00:40:13  来源:igfitidea点击:

Python3 + MySql: Error loading MySQLdb module: No module named 'MySQLdb'

pythonmysqldjangomysql-python

提问by skaz

I am new to Python and trying to setup a Django project to work with MySql. I have read through the documentation as well as some other StackOverflow posts about the topic, but I still can't get it to work.

我是 Python 新手,正在尝试设置一个 Django 项目以使用 MySql。我已经通读了文档以及关于该主题的其他一些 StackOverflow 帖子,但我仍然无法让它工作。

When I try to run a migrate in Django I get the following error:

当我尝试在 Django 中运行迁移时,出现以下错误:

Error loading MySQLdb module: No module named 'MySQLdb'

I have installed the recommended MySql Python Connector (2.0.1)selecting Ubuntu (since I am on Mint Linux). It installs correctly. I still get the error. I don't need to add this library to the project or anything, right? It looks like python should just be aware of this and run successfully. What can I do? Thanks.

我已经安装了推荐的MySql Python 连接器 (2.0.1),选择了 Ubuntu(因为我使用的是 Mint Linux)。它安装正确。我仍然收到错误。我不需要将此库添加到项目或任何东西中,对吗?看起来python应该知道这一点并成功运行。我能做什么?谢谢。

EDIT: I forgot to mention: I am running Python 3.4 - a lot of typical solutions are still using 2.7, so they don't work and all the solutions with 3.0 I have tried without success.

编辑:我忘了说:我正在运行 Python 3.4 - 许多典型的解决方案仍在使用 2.7,所以它们不起作用,我尝试过的所有 3.0 解决方案都没有成功。

采纳答案by Scott Sharkey

I would need to see your DATABASES configuration in settings.py, but it looks like it is trying to load MySQLDB instead of the Mysql Python Connector that you installed. The DATABASES should look something like this:

我需要在 settings.py 中查看您的 DATABASES 配置,但看起来它正在尝试加载 MySQLDB 而不是您安装的 Mysql Python 连接器。数据库应如下所示:

DATABASES = {
    'default': {
        'NAME': 'mydatabase',
        'ENGINE': 'mysql.connector.django',
        'USER': 'myuser',
        'PASSWORD': 'secretpassword',
        'OPTIONS': {
          'autocommit': True,
        },
    }
}

Note the ENGINE part... that tells Django to use the mysql connector instead of MySQLDB...

请注意 ENGINE 部分...它告诉 Django 使用 mysql 连接器而不是 MySQLDB...

more info available here: http://bunwich.blogspot.com/2014/02/finally-mysql-connector-that-works-with.htmlhttp://dev.mysql.com/doc/connector-python/en/connector-python-django-backend.html

此处提供更多信息:http: //bunwich.blogspot.com/2014/02/finally-mysql-connector-that-works-with.html http://dev.mysql.com/doc/connector-python/en/连接器-python-django-backend.html

and if you are expecting to use South:

如果您希望使用 South:

www.pythonanywhere.com/wiki/UsingMySQL

www.pythonanywhere.com/wiki/UsingMySQL

You may want to note that the Oracle connecter is GPL, and there may be license issues with using that code. See Here:

您可能需要注意 Oracle 连接器是 GPL,使用该代码可能存在许可问题。看这里:

groups.google.com/forum/#!topic/django-developers/8r_RVmUe5ys

groups.google.com/forum/#!topic/django-developers/8r_RVmUe5ys

The Django 1.7 documentation recommends using the mysqlclient driver...

Django 1.7 文档建议使用 mysqlclient 驱动程序...

docs.djangoproject.com/en/1.7/ref/databases/ --see the section on Mysql DB API Drivers

docs.djangoproject.com/en/1.7/ref/databases/--参见Mysql DB API Drivers部分

pypi.python.org/pypi/mysqlclient for that client...

该客户端的 pypi.python.org/pypi/mysqlclient...

-Scott

-斯科特

回答by Michael Scott Cuthbert

If you can install pymysql -- which works well for me with Python 3.4 -- then add these lines to your manage.py file in Django:

如果你可以安装 pymysql——这对我来说很适合 Python 3.4——然后将这些行添加到 Django 中的 manage.py 文件中:

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

This will make pymysql function like MySQLdb and worked for me.

这将使 pymysql 功能类似于 MySQLdb 并为我工作。

回答by Pegasus

pip install mysqlclientworks for me python3.5

pip install mysqlclient对我有用 python3.5

回答by Dan-Dev

None of the existing answers worked for me on Ubuntu Server 16.04 so I ran:

在 Ubuntu Server 16.04 上,现有的答案都不适合我,所以我跑了:

sudo apt-get install libmysqlclient-dev
sudo -H pip3 install mysqlclient

The first command get me the mysql config needed by the second command.

第一个命令为我获取第二个命令所需的 mysql 配置。

回答by Machavity

If you're like me, you're working with a Linux install, and Linux still needs Python 2.7 in most cases. That means that

如果您像我一样,正在使用 Linux 安装,并且在大多数情况下 Linux 仍然需要 Python 2.7。这意味着

pip install mysqlclient

will install the MySQL client for 2.7, which is segmented from Python 3. To make it install for 3.x you need to use

将为 2.7 安装 MySQL 客户端,它是从 Python 3 分割出来的。要安装 3.x,你需要使用

pip3 install mysqlclient

I did not have to install any packages, but mysql-commonwas already installed (Raspbian install) so your mileage may vary there

我不必安装任何软件包,但mysql-common已经安装(Raspbian 安装),因此您的里程可能会有所不同