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


当前回答

我也有同样的问题。如果您使用的是Windows,请遵循这些步骤。 至: 1.我的电脑 2.系统属性 3.系统高级设置 4. 在“高级”选项卡下单击“环境变量”按钮 5. 然后在“系统变量”下,您必须添加/更改以下变量:PYTHONPATH和Path。下面是我的变量的粘贴: python路径:

C:\Python27;C:\Python27\Lib\site-packages;C:\Python27\Lib;C:\Python27\DLLs;C:\Python27\Lib\lib-tk;C:\Python27\Scripts

路径:

C:\Program Files\MySQL\MySQL Utilities 1.3.5\;C:\Python27;C:\Python27\Lib\site-packages;C:\Python27\Lib;C:\Python27\DLLs;C:\Python27\Lib\lib-tk;C:\Python27\Scripts

参考这个链接

其他回答

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

对于Python3,我需要这样做:

python3 -m pip install MySQL

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

上面的答案很好,但是在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进行安装。它给我们很大的方便!