Answers:
这很容易,但是很难记住正确的拼写:
pip install mysqlclient
如果您需要1.2.x版本(仅限旧版Python),请使用 pip install MySQL-python
注意:运行上述命令时,某些依赖项可能必须存在。关于如何在各种平台上安装这些的一些提示:
sudo apt-get install python-pip python-dev libmysqlclient-dev
sudo dnf install python python-devel mysql-devel redhat-rpm-config gcc
brew install mysql-connector-c
如果失败,请尝试
brew install mysql
pip search mysql
并在输出中找到所需的名称。
pip search mysqldb
找到了,却没有找到。因此,我对此答案+1。
从全新的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/
error: command 'x86_64-linux-gnu-gcc' failed with exit status 1
使用问题sudo apt-get install build-essential
第一
pip install pymysql
然后将下面的代码放入__init__.py
(projectname/__init__.py
)
import pymysql
pymysql.install_as_MySQLdb()
我的环境是(python3.5,django1.10),此解决方案对我有用!
希望这可以帮助!!
我在通过Pip(问题编译源)在Windows上安装64位版本的MySQLdb时遇到问题[安装32位版本就可以了]。设法从http://www.lfd.uci.edu/~gohlke/pythonlibs/上的.whl文件安装已编译的MySQLdb 。
然后可以通过pip将.whl文件作为文档安装在https://pip.pypa.io/en/latest/user_guide/#installing-from-wheels中
例如,如果您保存在中,C:/
则可以通过
pip install c:/MySQL_python-1.2.5-cp27-none-win_amd64.whl
后续行动:如果您安装了64位版本的Python,那么您想通过上面的链接安装64位AMD版本的MySQLdb [即,即使您具有Intel处理器]。如果您改为尝试安装32位版本,我认为您会在下面的注释中得到不受支持的滚轮错误。
pip install mysqlclient-1.3.7-cp27-none-win_amd64.whl
工作很棒。
whl
文件(我将64位dl匹配我的64位Python安装)。将文件保存到c:/
目录中并运行pip install c:/MySQL_python-1.2.5-cp27-none-win_amd64.whl
它可以在几秒钟内安装好,很方便。太容易了!
这对我有用:
pip install mysqlclient
这是针对python 3.x
我尝试了所有选项,但无法使其在Redhat平台上运行。我做了以下工作以使其工作:-
yum install MySQL-python -y
安装软件包后,便可以按照解释器中的说明导入模块:-
>>> import MySQLdb
>>>
pip
。您正在使用什么发行版?在Fedora 24,该工程安装模块通过PIP: sudo dnf install python python-devel mysql-devel redhat-rpm-config gcc
,sudo pip install MySQL-python
转到pycharm,然后转到默认设置-> pip(双击)-pymsqldb ..-->安装-在类似这样的程序中安装使用后
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()
如果您使用的是Raspberry Pi [Raspbian OS]
首先需要有install pip命令
apt-get install python-pip
这样就可以依次安装
apt-get install python-dev libmysqlclient-dev
apt-get install python-pip
pip install MySQL-python
如果您无法安装mysqlclient,也可以安装pymysql:
pip install pymysql
这与MySqldb相同。之后,使用pymysql代替MySQLdb
我也遇到了同样的问题。如果您使用的是Windows,请按照以下步骤操作。转至:1.我的电脑2.系统属性3.高级系统设置4.在“高级”选项卡上,单击显示“环境变量”的按钮。5.然后在系统变量下,您必须添加/更改以下变量:PYTHONPATH和路径。这是我的变量的样子的粘贴信息:python path:
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中安装MySQL-python时可能会出现一些问题
例如,它需要一些与Visual Stdio相关的文件。一个解决方案是安装VS2008或2010……显然,这太昂贵了。
另一种方法是@ bob90937的答案。我在这里要做一些补充。
使用http://www.lfd.uci.edu/~gohlke/pythonlibs,您可以下载许多科学开源扩展包的Windows二进制文件,以用于Python编程语言的官方CPython发行版。
返回主题,我们可以选择MySQL-python(py2)或Mysqlclient(py3) 并使用pip install 进行安装。它给我们带来了极大的便利!
pip install mysql-connector-python
如文档中所述:
https://dev.mysql.com/doc/connector-python/zh-CN/connector-python-installation-binary.html
我的环境是:
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()
实际上,按照@Nick T的回答对我不起作用,我尝试apt-get install python-mysqldb
为我工作
root@2fb0da64a933:/home/test_scrapy# apt-get install python-mysqldb
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
libmariadbclient18 mysql-common
Suggested packages:
default-mysql-server | virtual-mysql-server python-egenix-mxdatetime python-mysqldb-dbg
The following NEW packages will be installed:
libmariadbclient18 mysql-common python-mysqldb
0 upgraded, 3 newly installed, 0 to remove and 29 not upgraded.
Need to get 843 kB of archives.
After this operation, 4611 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://deb.debian.org/debian stretch/main amd64 mysql-common all 5.8+1.0.2 [5608 B]
Get:2 http://deb.debian.org/debian stretch/main amd64 libmariadbclient18 amd64 10.1.38-0+deb9u1 [785 kB]
Get:3 http://deb.debian.org/debian stretch/main amd64 python-mysqldb amd64 1.3.7-1.1 [52.1 kB]
Fetched 843 kB in 23s (35.8 kB/s)
debconf: delaying package configuration, since apt-utils is not installed
Selecting previously unselected package mysql-common.
(Reading database ... 13223 files and directories currently installed.)
Preparing to unpack .../mysql-common_5.8+1.0.2_all.deb ...
Unpacking mysql-common (5.8+1.0.2) ...
Selecting previously unselected package libmariadbclient18:amd64.
Preparing to unpack .../libmariadbclient18_10.1.38-0+deb9u1_amd64.deb ...
Unpacking libmariadbclient18:amd64 (10.1.38-0+deb9u1) ...
Selecting previously unselected package python-mysqldb.
Preparing to unpack .../python-mysqldb_1.3.7-1.1_amd64.deb ...
Unpacking python-mysqldb (1.3.7-1.1) ...
Setting up mysql-common (5.8+1.0.2) ...
update-alternatives: using /etc/mysql/my.cnf.fallback to provide /etc/mysql/my.cnf (my.cnf) in auto mode
Setting up libmariadbclient18:amd64 (10.1.38-0+deb9u1) ...
Processing triggers for libc-bin (2.24-11+deb9u3) ...
Setting up python-mysqldb (1.3.7-1.1) ...
root@2fb0da64a933:/home/test_scrapy# python
Python 2.7.13 (default, Nov 24 2017, 17:33:09)
[GCC 6.3.0 20170516] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
>>>