如何使用pip安装Python的MySQLdb模块?


当前回答

在RHEL 7:

Sudo yum安装yum-utils mariadb-devel python-pip python-devel GCC

sudo /bin/pip2 install MySQL-python

其他回答

我在通过Pip(问题编译源)在Windows上安装64位版本的MySQLdb时遇到了问题[32位版本安装ok]。设法从http://www.lfd.uci.edu/~gohlke/pythonlibs/上的.whl文件安装编译后的MySQLdb

然后,.whl文件可以通过pip作为https://pip.pypa.io/en/latest/user_guide/#installing-from-wheels中的文档安装

例如,如果你保存在C:/ the,你可以安装通过

pip install c:/MySQL_python-1.2.5-cp27-none-win_amd64.whl

后续:如果你安装了64位版本的Python,那么你想从上面的链接安装64位AMD版本的MySQLdb[即,即使你有英特尔处理器]。如果你尝试安装32位版本,我认为你会在下面的评论中得到不支持的轮子错误。

转到pycharm,然后转到默认设置——> pip(双击)——pymsqldb..——> install——在像这样的程序中安装使用之后

import pymysql as MySQLdb

# Open database connection
db = MySQLdb.connect("localhost","root","root","test" )

# prepare a cursor object using cursor() method
cursor = db.cursor()

# execute SQL query using execute() method.
cursor.execute("show tables")

# Fetch a single row using fetchone() method.
data = cursor.fetchall()
print (data)

# disconnect from server
db.close()

这很容易,但很难记住正确的拼写:

pip install mysqlclient

如果你需要1.2。x版本(仅适用于遗留的Python),使用pip安装MySQL-python

注意:在运行上述命令时,有些依赖项可能必须就位。一些关于如何在不同平台上安装这些工具的提示:

Ubuntu 14, Ubuntu 16, Debian 8.6 (jessie)

sudo apt-get install python-pip python-dev libmysqlclient-dev

Fedora 24:

sudo dnf install python python-devel mysql-devel redhat-rpm-config gcc

苹果操作系统

brew install mysql-connector-c

如果失败了,那就试试

brew install mysql

PIP安装mysql-connector-python 如文档中所述:

https://dev.mysql.com/doc/connector-python/en/connector-python-installation-binary.html

从新的Ubuntu 14.04.2系统开始,需要这两个命令:

 apt-get install python-dev libmysqlclient-dev
 pip install MySQL-python

仅仅自己进行“pip安装”是行不通的。

从http://codeinthehole.com/writing/how-to-set-up-mysql-for-python-on-ubuntu/