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


当前回答

在RHEL 7:

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

sudo /bin/pip2 install MySQL-python

其他回答

这招对我很管用:

pip install mysqlclient

这是针对python 3.x的

上面的答案很好,但是在Windows中使用pip安装MySQL-python时可能会出现一些问题

例如,它需要一些与Visual Stdio相关的文件。一个解决方案是安装VS2008或2010......显然,它花费太多。

另一种方法是@bob90937的答案。我是来补充的。

通过http://www.lfd.uci.edu/~gohlke/pythonlibs,你可以下载许多科学开源扩展包的Windows二进制文件,用于Python编程语言的官方CPython发行版。

回到主题,我们可以选择MySQL-python(py2)或Mysqlclient(py3)并使用pip install进行安装。它给我们很大的方便!

转到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()

从新的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/

如果pip3不起作用,您可以尝试:

sudo apt install python3-mysqldb