简体   繁体   English

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

[英]How to install Python MySQLdb module using pip?

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

It's easy to do, but hard to remember the correct spelling:这很容易做到,但很难记住正确的拼写:

pip install mysqlclient

If you need 1.2.x versions (legacy Python only), use pip install MySQL-python如果您需要 1.2.x 版本(仅限旧版 Python),请使用pip install MySQL-python

Note: Some dependencies might have to be in place when running the above command.注意:运行上述命令时,可能必须存在一些依赖项。 Some hints on how to install these on various platforms:关于如何在各种平台上安装这些的一些提示:

Ubuntu 14, Ubuntu 16, Debian 8.6 (jessie) Ubuntu 14、Ubuntu 16、Debian 8.6(杰西)

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

Fedora 24:费多拉 24:

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

Mac OS苹果系统

brew install mysql-connector-c

if that fails, try如果失败,请尝试

brew install mysql

Starting from a fresh Ubuntu 14.04.2 system, these two commands were needed:从全新的 Ubuntu 14.04.2 系统开始,需要这两个命令:

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

Just doing the "pip install" by itself did not work.仅仅做“pip install”本身是行不通的。

From http://codeinthehole.com/writing/how-to-set-up-mysql-for-python-on-ubuntu/来自http://codeinthehole.com/writing/how-to-set-up-mysql-for-python-on-ubuntu/

First第一的

pip install pymysql

Then put the code below into __init__.py ( projectname/__init__.py )然后将下面的代码放入__init__.py ( projectname/__init__.py )

import pymysql
pymysql.install_as_MySQLdb()

My environment is (python3.5, django1.10) and this solution works for me!我的环境是(python3.5,django1.10),这个解决方案对我有用!

Hope this helps!!希望这可以帮助!!

I had problems installing the 64-bit version of MySQLdb on Windows via Pip (problem compiling sources) [32bit version installed ok].我通过 Pip 在 Windows 上安装 64 位版本的 MySQLdb 时遇到问题(编译源问题)[32 位版本安装正常]。 Managed to install the compiled MySQLdb from the .whl file available from http://www.lfd.uci.edu/~gohlke/pythonlibs/设法从http://www.lfd.uci.edu/~gohlke/pythonlibs/提供的 .whl 文件中安装已编译的 MySQLdb

The .whl file can then be installed via pip as document in https://pip.pypa.io/en/latest/user_guide/#installing-from-wheels然后可以通过 pip 将 .whl 文件安装为https://pip.pypa.io/en/latest/user_guide/#installing-from-wheels中的文档

For example if you save in C:/ the you can install via例如,如果您保存在C:/中,您可以通过以下方式安装

pip install c:/MySQL_python-1.2.5-cp27-none-win_amd64.whl

Follow-up: if you have a 64bit version of Python installed, then you want to install the 64-bit AMD version of MySQLdb from the link above [ie even if you have a Intel processor].后续:如果你安装了64位版本的Python,那么你想从上面的链接中安装64位AMD版本的MySQLdb[即即使你有英特尔处理器]。 If you instead try and install the 32-bit version, I think you get the unsupported wheel error in comments below.如果您尝试安装 32 位版本,我认为您会在下面的评论中看到不支持的轮子错误。

well this worked for me:这对我有用:

pip install mysqlclient

this is for python 3.x这适用于 python 3.x

Go to pycharm then go to default setting --> pip (double click) -- pymsqldb..-- > install --after installing use in a program like this转到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()

I tried all the option but was not able to get it working on Redhat platform.我尝试了所有选项,但无法让它在 Redhat 平台上运行。 I did the following to make it work:-我做了以下工作以使其工作:-

yum install MySQL-python -y

Once the package was installed was able to import module as follows in the interpreter:-安装软件包后,就可以在解释器中按以下方式导入模块:-

>>> import MySQLdb
>>> 

Many of the given answers here are quite confusing so I will try to put it simply.这里给出的许多答案都非常令人困惑,所以我会尽量简单地说。 It helped me to install this它帮助我安装了这个

pip install pymysql

and then use the following command in the python file然后在python文件中使用以下命令

import pymysql as MySQLdb

This way you can use MySQLdb without any problems.这样您就可以毫无问题地使用 MySQLdb。

If you are unable to install mysqlclient you can also install pymysql :如果你无法安装mysqlclient你也可以安装pymysql

pip install pymysql

This works same as MySqldb .这与MySqldb相同。 After that use pymysql all over instead of MySQLdb之后全部使用 pymysql 而不是 MySQLdb

If you are use Raspberry Pi [Raspbian OS]如果您使用的是 Raspberry Pi [Raspbian OS]

There are need to be install pip command at first首先需要安装 pip 命令

apt-get install python-pip

So that just install Sequently所以只需依次安装

apt-get install python-dev libmysqlclient-dev

apt-get install python-pip

pip install MySQL-python

在此处输入图像描述

You can go to this website to download the package.你可以去这个网站下载这个包。

I had the same problem too.Follow these steps if you are on Windows.我也有同样的问题。如果你在 Windows 上,请按照这些步骤操作。 Go to: 1.My Computer 2.System Properties 3.Advance System Settings 4. Under the "Advanced" tab click the button that says "Environment Variables" 5. Then under System Variables you have to add / change the following variables: PYTHONPATH and Path.转到:1.我的电脑 2.系统属性 3.高级系统设置 4.在“高级”选项卡下单击“环境变量”按钮 5.然后在系统变量下,您必须添加/更改以下变量:PYTHONPATH和路径。 Here is a paste of what my variables look like: python path:这是我的变量的样子:python 路径:

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

path:小路:

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

See this link for reference请参阅此链接以供参考

If you have Windows installed on your system then type the following command on cmd :如果您的系统上安装了 Windows,请在 cmd 上键入以下命令:

pip install mysql-connector

if the above command does not work try using:如果上述命令不起作用,请尝试使用:

pip install mysql-connector-python

Now,if the above commands do not get the work done, try using:现在,如果上述命令没有完成工作,请尝试使用:

pip install mysql-connector-python-rf

That's it you are good to go now.就是这样,你现在可以走了。

The above answer is great, but there may be some problems when we using pip to install MySQL-python in Windows上面的答案很好,但是我们在Windows中使用pip安装MySQL-python时可能会出现一些问题

for example,It needs some files that are associated with Visual Stdio .One solution is installing VS2008 or 2010……Obviously,it cost too much.例如,它需要一些与Visual Stdio相关的文件。一个解决方案是安装VS2008或2010……显然,它的成本太高了。

Another way is the answer of @bob90937 .另一种方法是@bob90937 的答案。 I am here to do something to add.我是来做点补充的。

with http://www.lfd.uci.edu/~gohlke/pythonlibs , u can download many Windows binaries of many scientific open-source extension packages for the official CPython distribution of the Python programming language.通过http://www.lfd.uci.edu/~gohlke/pythonlibs ,您可以下载许多科学开源扩展包的 Windows 二进制文件,用于 Python 编程语言的官方 CPython 发行版。

Back to topic,we can choose the MySQL-python(py2) or Mysqlclient(py3) and use pip install to install.回到主题,我们可以选择MySQL-python(py2)Mysqlclient(py3)并使用pip install进行安装。 it gives us Great convenience!它给我们带来了极大的便利!

对于 Python3,我需要这样做:

python3 -m pip install MySQL

如果 pip3 不起作用,您可以尝试:

sudo apt install python3-mysqldb

pip install mysql-connector-python as noted in the documentation: pip install mysql-connector-python如文档中所述:

https://dev.mysql.com/doc/connector-python/en/connector-python-installation-binary.html 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

actually, follow @Nick T's answer doesn't work for me, i try apt-get install python-mysqldb work for me实际上,遵循@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
>>> 

My environment are:我的环境是:

  • Windows 10 Pro,视窗 10 专业版,
  • Python 3.7 (python-3.7.1-amd64.exe), Python 3.7 (python-3.7.1-amd64.exe),
  • MySQL 8.0 (mysql-installer-web-community-8.0.13.0.msi) MySQL 8.0 (mysql-installer-web-community-8.0.13.0.msi)

pip install mysqlclient-1.3.13-cp37-cp37m-win_amd64.whl pip install mysqlclient-1.3.13-cp37-cp37m-win_amd64.whl

works for me.为我工作。

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

go to this site to download the package according to the python version. 请转到该站点以根据python版本下载软件包。 and use pip install filename like i have python 3.7.1 and download the file in red circle and open terminal where file downloaded and install that file eg pip install mysqlclient-1.4.4-cp37-cp37m-win32.whl 并使用pip install文件名,如我拥有python 3.7.1并下载红色圆圈中的文件,并打开下载文件并安装该文件的终端,例如pip install mysqlclient-1.4.4-cp37-cp37m-win32.whl

and in setting file 并在设置文件中

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'shoping_cart',
        'HOST': 'localhost',
        'USER': 'root',
        'PASSWORD':'',
        'PORT': '3306',

    }
}

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM