如何使用pip为Python安装MySQLdb模块?
这很容易,但很难记住正确的拼写:
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
我也遇到了同样的问题。如果你在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
请参阅此 link 以供参考
如果你无法安装mysqlclient,你也可以安装pymysql:
pip install pymysql
这与MySqldb相同。之后使用pymysql而不是MySQLdb
上面的答案很棒,但是当我们使用pip在Windows中安装MySQL-python时可能会出现一些问题
例如,它需要一些与Visual Studio相关的文件。一个解决方案是安装VS 2008或2010 ......显然,它的成本太高了。
另一种方法是@ bob90937的答案。我在这里做点什么补充。
使用http://www.lfd.uci.edu/~gohlke/pythonlibs,您可以下载许多科学开源扩展包的Windows二进制文件,用于Python编程语言的官方CPython发行版。
回到主题,我们可以选择MySQL-python(py2)或Mysqlclient(py3)并使用pip install来安装。它给了我们极大的便利!
对于Python3,我需要这样做:
python3 -m pip install MySQL
pip install mysql-connector-python
,如文件中所述:
https://dev.mysql.com/doc/connector-python/en/connector-python-installation-binary.html
在RHEL 7上:
sudo yum install yum-utils mariadb-devel python-pip python-devel gcc
sudo /bin/pip2 install MySQL-python
如果pip3不起作用,您可以尝试:
sudo apt install python3-mysqldb
如果您的系统上安装了Windows,则在cmd上键入以下命令:
pip install mysql-connector
如果上述命令不起作用,请尝试使用:
pip install mysql-connector-python
现在,如果上述命令无法完成工作,请尝试使用:
pip install mysql-connector-python-rf
那就是你现在好了。
我的环境是:
pip install 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
>>>
从全新的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上通过Pip安装64位版本的MySQLdb时出现问题(问题编译源)[32位版本安装好]。管理从http://www.lfd.uci.edu/~gohlke/pythonlibs/提供的.whl文件安装已编译的MySQLdb
然后可以通过pip作为https://pip.pypa.io/en/latest/user_guide/#installing-from-wheels中的文档安装.whl文件
例如,如果你保存在C:/
,你可以安装通过
pip install c:/MySQL_python-1.2.5-cp27-none-win_amd64.whl
后续:如果你安装了64位版本的Python,那么你想从上面的链接安装64位AMD版本的MySQLdb [即即使你有英特尔处理器]。如果你试着安装32位版本,我认为你在下面的评论中得到了不支持的车轮错误。
这对我有用:
pip install mysqlclient
这是为python 3.x
第一
pip install pymysql
然后将下面的代码放入__init__.py
(projectname/__init__.py
)
import pymysql
pymysql.install_as_MySQLdb()
我的环境是(python3.5,django1.10),这个解决方案对我有用!
希望这可以帮助!!
我尝试了所有选项,但无法让它在Redhat平台上运行。我做了以下工作以使其工作: -
yum install MySQL-python -y
一旦安装了软件包,就可以在解释器中导入模块如下: -
>>> import MySQLdb
>>>
转到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()
如果你使用Raspberry Pi [Raspbian OS]
首先需要安装pip命令
apt-get install python-pip
所以只需安装Sequently
apt-get install python-dev libmysqlclient-dev
apt-get install python-pip
pip install MySQL-python
你可以去这个website下载包。