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

时间:2022-01-17 23:12:36

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 * posts about the topic, but I still can't get it to work.

我是Python新手,正在尝试安装Django项目来使用MySql。我已经通读了关于这个主题的文档和其他一些*文章,但是我还是不能让它工作。

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的解决方案都没有成功。

4 个解决方案

#1


7  

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:

我需要在设置中看到您的数据库配置。py,但是看起来它试图加载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...

注意发动机的部分……这告诉Django使用mysql连接器而不是MySQLDB…

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

更多信息请访问:http://bunwich.blogspot.com/2014/02/finally-mysql- connecter -that-work -with.html http://dev.mysql.com/doc/connector- python/en/connec-django -backend.html

and if you are expecting to use 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 connecter是GPL,使用该代码可能存在许可证问题。在这里看到的:

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

groups.google.com/forum/ # ! / django开发人员/ 8 r_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驱动程序一节

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

pypi.python.org/pypi/mysqlclient的客户……

-Scott

斯科特

#2


12  

pip install mysqlclient works for me python3.5

pip install mysqlclient works for me python3.5

#3


11  

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

现有的答案在Ubuntu服务器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配置。

#4


10  

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 py文件:

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

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

这将使pymysql函数类似于MySQLdb,并适用于我。

#1


7  

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:

我需要在设置中看到您的数据库配置。py,但是看起来它试图加载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...

注意发动机的部分……这告诉Django使用mysql连接器而不是MySQLDB…

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

更多信息请访问:http://bunwich.blogspot.com/2014/02/finally-mysql- connecter -that-work -with.html http://dev.mysql.com/doc/connector- python/en/connec-django -backend.html

and if you are expecting to use 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 connecter是GPL,使用该代码可能存在许可证问题。在这里看到的:

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

groups.google.com/forum/ # ! / django开发人员/ 8 r_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驱动程序一节

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

pypi.python.org/pypi/mysqlclient的客户……

-Scott

斯科特

#2


12  

pip install mysqlclient works for me python3.5

pip install mysqlclient works for me python3.5

#3


11  

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

现有的答案在Ubuntu服务器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配置。

#4


10  

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 py文件:

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

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

这将使pymysql函数类似于MySQLdb,并适用于我。