问题:没有名为MySQLdb的模块

我正在使用Python 2.5.4版并安装MySQL 5.0版和Django。Django在Python上运行良好,但在MySQL上运行良好。我在Windows Vista中使用它。

I am using Python version 2.5.4 and install MySQL version 5.0 and Django. Django is working fine with Python, but not MySQL. I am using it in Windows Vista.


回答 0

您需要使用以下命令之一。哪一个取决于您拥有和使用的操作系统和软件。

  1. easy_install mysql-python(混合OS)
  2. pip安装mysql-python(mix os / python 2)
  3. pip安装mysqlclient(mix os / python 3)
  4. apt-get install python-mysqldb(Linux Ubuntu,…)
  5. cd / usr / ports / databases / py-MySQLdb &&使安装干净(FreeBSD)
  6. yum安装MySQL-python(Linux Fedora,CentOS …)

对于Windows,请参见以下答案:安装mysql-python(Windows)

You need to use one of the following commands. Which one depends on what OS and software you have and use.

  1. easy_install mysql-python (mix os)
  2. pip install mysql-python (mix os/ python 2)
  3. pip install mysqlclient (mix os/ python 3)
  4. apt-get install python-mysqldb (Linux Ubuntu, …)
  5. cd /usr/ports/databases/py-MySQLdb && make install clean (FreeBSD)
  6. yum install MySQL-python (Linux Fedora, CentOS …)

For Windows, see this answer: Install mysql-python (Windows)


回答 1

…并且记住没有针对python3.x的MySQLdb

(我知道问题是关于python2.x的,但是谷歌对这篇文章的评价很高)


编辑:如评论中所述,有一个MySQLdb的fork添加了Python 3支持:github.com/PyMySQL/mysqlclient-python

…and remember there is no MySQLdb for python3.x

(I know the question is about python2.x but google rates this post quite high)


EDIT: As stated in the comments, there’s a MySQLdb’s fork that adds Python 3 support: github.com/PyMySQL/mysqlclient-python


回答 2

如果您的python版本是3.5,请执行pip install mysqlclient,其他操作对我不起作用

if your python version is 3.5, do a pip install mysqlclient, other things didn’t work for me


回答 3

mysqldb是未预安装或未随Django一起安装的Python模块。您可以mysqldb 在此处下载。

mysqldb is a module for Python that doesn’t come pre-installed or with Django. You can download mysqldb here.


回答 4

Ubuntu:

sudo apt-get install python-mysqldb

Ubuntu:

sudo apt-get install python-mysqldb

回答 5

请注意,这并未针对python 3.x进行测试

在CMD中

pip install wheel
pip install pymysql

在settings.py中

import pymysql
pymysql.install_as_MySQLdb()

它和我一起工作

Note this is not tested for python 3.x

In CMD

pip install wheel
pip install pymysql

in settings.py

import pymysql
pymysql.install_as_MySQLdb()

It worked with me


回答 6

pip install PyMySQL

然后将这两行添加到您的Project / Project / init .py

import pymysql
pymysql.install_as_MySQLdb()

适用于WIN和python 3.3+

pip install PyMySQL

and then add this two lines to your Project/Project/init.py

import pymysql
pymysql.install_as_MySQLdb()

Works on WIN and python 3.3+


回答 7

尝试这个。

pip install MySQL-python

Try this.

pip install MySQL-python

回答 8

对于窗口:

pip install mysqlclient pymysql

然后:

导入pymysql pymysql.install_as_MySQLdb()

对于python 3 Ubuntu

sudo apt-get install -y python3-mysqldb

for window :

pip install mysqlclient pymysql

then:

import pymysql pymysql.install_as_MySQLdb()

for python 3 Ubuntu

sudo apt-get install -y python3-mysqldb

回答 9

如果pip install mysqlclient产生错误并且您使用Ubuntu,请尝试:

sudo apt-get install -y python-dev libmysqlclient-dev && sudo pip install mysqlclient

If pip install mysqlclient produces an error and you use Ubuntu, try:

sudo apt-get install -y python-dev libmysqlclient-dev && sudo pip install mysqlclient

回答 10

我在Windows下遇到了同样的情况,并寻找了解决方案。

看到这篇文章安装mysql-python(Windows)

它指出,安装这样的pip环境是困难的,需要许多其他依赖项。

但我最终知道,如果我们使用mysqlclient的版本降至1.3.4,则不再需要该要求,请尝试:

pip install mysqlclient==1.3.4

I met the same situation under windows, and searched for the solution.

Seeing this post Install mysql-python (Windows).

It points out installing such a pip environment is difficult, needs many other dependencies.

But I finally know that if we use mysqlclient with a version down to 1.3.4, it don’t need that requirements any more, so try:

pip install mysqlclient==1.3.4

回答 11

  • 使用转到您的项目目录cd
  • 源/ bin /激活(如果以前没有激活过,请激活环境)。
  • 运行命令 easy_install MySQL-python
  • Go to your project directory with cd.
  • source/bin/activate (activate your env. if not previously).
  • Run the command easy_install MySQL-python

回答 12

pip install --user mysqlclient 

上面的对我来说就像魅力一样对我有效。我实际上是从sqlalchemy出错。环境信息:

Python:3.6,Ubuntu:16.04,conda 4.6.8

pip install --user mysqlclient 

above works for me like charm for me.I go the error from sqlalchemy actually. Environment information :

Python : 3.6, Ubuntu : 16.04,conda 4.6.8


回答 13

感谢derevo,但我认为还有另一种好方法:

  1. 下载并安装ActivePython
  2. 打开命令提示符
  3. 类型 pypm install mysql-python
  4. 阅读特定于此软件包的注释。

我认为pypm它比easy_install

Thanks to derevo but I think there’s another good way for doing this:

  1. Download and install ActivePython
  2. Open Command Prompt
  3. Type pypm install mysql-python
  4. Read the notes specific to this package.

I think pypm is more powerful and reliable than easy_install.


回答 14

对于Python 3+版本

安装mysql-connector为:

pip3 install mysql-connector 

示例Python DB连接代码:

import mysql.connector
db_connection = mysql.connector.connect(
  host="localhost",
  user="root",
  passwd=""
)
print(db_connection)

输出:

> <mysql.connector.connection.MySQLConnection object at > 0x000002338A4C6B00>

这意味着数据库已正确连接。

For Python 3+ version

install mysql-connector as:

pip3 install mysql-connector 

Sample Python DB connection code:

import mysql.connector
db_connection = mysql.connector.connect(
  host="localhost",
  user="root",
  passwd=""
)
print(db_connection)

Output:

> <mysql.connector.connection.MySQLConnection object at > 0x000002338A4C6B00>

This means, database is correctly connected.


回答 15

我个人建议使用pymysql而不是使用真正的MySQL连接器,该连接器为您提供独立于平台的界面,可以通过安装pip

您可以这样编辑SQLAlchemy URL模式: mysql+pymysql://username:passwd@host/database

I personally recommend using pymysql instead of using the genuine MySQL connector, which provides you with a platform independent interface and could be installed through pip.

And you could edit the SQLAlchemy URL schema like this: mysql+pymysql://username:passwd@host/database


回答 16

对于Python 3.6或更高版本sudo apt-get install libmysqlclient-dev,并pip3 install mysqlclient 不会把戏

For Python 3.6+ sudo apt-get install libmysqlclient-dev and pip3 install mysqlclient does the trick


回答 17

如果您在Vista上运行,则可能要签出Bitnami Django堆栈。它是由Apache,Python,MySQL等组成的一站式堆栈,与Bitrock跨平台安装程序打包在一起,使上手非常容易。它可以在Windows,Mac和Linux上运行。哦,是完全免费的:)

If you are running on Vista, you may want to check out the Bitnami Django stack. It is an all-in-one stack of Apache, Python, MySQL, etc. packaged with Bitrock crossplatform installers to make it really easy to get started. It runs on Windows, Mac and Linux. Oh, and is completely free :)


回答 18

我已经尝试了上面的方法,但是仍然没有名为“ MySQLdb”的模块,最后,我成功了

easy_install mysql-python

我的环境是unbuntu 14.04

I have tried methods above, but still no module named ‘MySQLdb’, finally, I succeed with

easy_install mysql-python

my env is unbuntu 14.04


回答 19

在OSX上,这些命令对我有用

brew install mysql-connector-c 
pip install MySQL-python

On OSX these commands worked for me

brew install mysql-connector-c 
pip install MySQL-python

回答 20

如果您使用的是SQLAlchemy,并且错误位于/site-packages/sqlalchemy/dialects/mysql/mysqldb.py

from ...connectors.mysqldb import (
                        MySQLDBExecutionContext,
                        MySQLDBCompiler,
                        MySQLDBIdentifierPreparer,
                        MySQLDBConnector
                    )

因此您可能错过了mysqldb连接器,SQLAlchemy解决方法是在安装mysql-python模块后重新安装sqlalchemy 。

If your are using SQLAlchemy and the error is in /site-packages/sqlalchemy/dialects/mysql/mysqldb.py:

from ...connectors.mysqldb import (
                        MySQLDBExecutionContext,
                        MySQLDBCompiler,
                        MySQLDBIdentifierPreparer,
                        MySQLDBConnector
                    )

so you may have missed mysqldb connector for SQLAlchemy and the solution is to re-install sqlalchemy after installing mysql-python module.


回答 21

Win10 / Python27对我有用:

easy_install mysql-python

所有其他“ pip install …”失败,并出现相关性错误

Win10 / Python27 this worked for me:

easy_install mysql-python

all other ‘pip install…’ failed with dependency errors


回答 22

以上都不是通过docker image在Ubuntu 18.04全新安装上为我工作的。

以下为我解决了它:

apt-get install holland python3-mysqldb

None of the above worked for me on an Ubuntu 18.04 fresh install via docker image.

The following solved it for me:

apt-get install holland python3-mysqldb


回答 23

在运行Catalina v10.15.2的Mac上,出现以下MySQLdb版本冲突:

ImportError: this is MySQLdb version (1, 2, 5, 'final', 1), but _mysql is version (1, 4, 6, 'final', 0)

为了解决这个问题,我做了以下工作:

pip uninstall MySQL-python
pip install MySQL-python

On my mac running Catalina v10.15.2, I had the following MySQLdb version conflict:

ImportError: this is MySQLdb version (1, 2, 5, 'final', 1), but _mysql is version (1, 4, 6, 'final', 0)

To resolve it, I did the following:

pip uninstall MySQL-python
pip install MySQL-python

回答 24

在Debian Buster上,以下解决方案适用于python 3.7:

sudo apt-get install libmysqlclient-dev
sudo apt-get install libssl-dev
pip install mysqlclient

On Debian Buster, the following solution worked for me with python 3.7:

sudo apt-get install libmysqlclient-dev
sudo apt-get install libssl-dev
pip install mysqlclient

回答 25

我在ubuntu(linux),对我有用的是

sudo apt-get install python3-dev default-libmysqlclient-dev build-essential

然后最后

pip install mysqlclient

I am at ubuntu (linux) and what worked for me was

sudo apt-get install python3-dev default-libmysqlclient-dev build-essential

and then finally

pip install mysqlclient

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。