如何使用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

参考这个链接

其他回答

我在通过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位版本,我认为你会在下面的评论中得到不支持的轮子错误。

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/

我也有同样的问题。如果您使用的是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

参考这个链接

我的环境是:

Windows 10 Pro, Python 3.7 (Python -3.7.1-amd64.exe) MySQL 8.0 (MySQL -install -web-community-8.0.13.0 msi)

PIP安装mysqlclient-1.3.13-cp37-cp37m-win_amd64.whl

对我有用。

import MySQLdb, sys


# --------------------------------------------------
# Connect to MySQL
# --------------------------------------------------
try:
    db = MySQLdb.connect(host="localhost", user="user", passwd="pass", db="database", charset='cp1251')
except MySQLdb.Error as e:
    print ("Error %d: %s" % (e.args[0], e.args[1]))
    sys.exit()

# Creating cursor 
cursor = db.cursor()