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


当前回答

如果你的系统上安装了Windows,那么在cmd上输入以下命令:

pip install mysql-connector

如果上述命令不起作用,请尝试使用:

pip install mysql-connector-python

现在,如果上面的命令不能完成工作,尝试使用:

pip install mysql-connector-python-rf

好了,你现在可以走了。

其他回答

这很容易,但很难记住正确的拼写:

pip install mysqlclient

如果你需要1.2。x版本(仅适用于遗留的Python),使用pip安装MySQL-python

注意:在运行上述命令时,有些依赖项可能必须就位。一些关于如何在不同平台上安装这些工具的提示:

Ubuntu 14, Ubuntu 16, Debian 8.6 (jessie)

sudo apt-get install python-pip python-dev libmysqlclient-dev

Fedora 24:

sudo dnf install python python-devel mysql-devel redhat-rpm-config gcc

苹果操作系统

brew install mysql-connector-c

如果失败了,那就试试

brew install mysql

你可以去这个网站下载软件包。

如果你的系统上安装了Windows,那么在cmd上输入以下命令:

pip install mysql-connector

如果上述命令不起作用,请尝试使用:

pip install mysql-connector-python

现在,如果上面的命令不能完成工作,尝试使用:

pip install mysql-connector-python-rf

好了,你现在可以走了。

第一个

pip install pymysql

然后将下面的代码放入__init__.py (projectname/__init__.py)

import pymysql
pymysql.install_as_MySQLdb()

我的环境是(python3.5, django1.10),这个解决方案适合我!

希望这能有所帮助!!

实际上,遵循@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
>>>