How to install Python MySQLdb module using pip?

2019-01-01 10:10发布

How can I install the MySQLdb module for Python using pip?

标签: python mysql pip
18条回答
听够珍惜
2楼-- · 2019-01-01 10:18

Starting from a fresh Ubuntu 14.04.2 system, these two commands were needed:

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

Just doing the "pip install" by itself did not work.

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

查看更多
零度萤火
3楼-- · 2019-01-01 10:18

Go to pycharm then go to default setting --> pip (double click) -- pymsqldb..-- > install --after installing use in a program like this

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()
查看更多
弹指情弦暗扣
4楼-- · 2019-01-01 10:18

My environment are:

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

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()
查看更多
有味是清欢
5楼-- · 2019-01-01 10:20

First

pip install pymysql

Then put the code below into init.py(projectname/init.py)

import pymysql
pymysql.install_as_MySQLdb()

My environment (python3.5,django1.10) and it work for me!

Hope help!!

查看更多
后来的你喜欢了谁
6楼-- · 2019-01-01 10:20

If you are unable to install mysqlclient you can also install pymysql:

pip install pymysql

This works same as MySqldb. After that use pymysql all over instead of MySQLdb

查看更多
墨雨无痕
7楼-- · 2019-01-01 10:21

The above answer is great, but there may be some problems when we using pip to install MySQL-python in Windows

for example,It needs some files that are associated with Visual Stdio .One solution is installing VS2008 or 2010……Obviously,it cost too much.

Another way is the answer of @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.

Back to topic,we can choose the MySQL-python(py2) or Mysqlclient(py3) and use pip install to install. it gives us Great convenience!

查看更多
登录 后发表回答