在Ubuntu上安装Mysql For Python

时间:2023-03-09 07:37:15
在Ubuntu上安装Mysql For Python

安装:

首先安装pip,并且把pip更新到最小版本

apt-get install python-pip
pip install -U pip

安装mysql开发包

apt-get install python-dev libmysqlclient-dev

通过pip安装Mysql For Python

pip install MySQL-python

测试,在Python命令行里输入import MySQLdb,看报不报错

xxx@ubuntu:~$ python
Python 2.7. (default, Jul , ::)
[GCC 5.4. ] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb

注:用apt-get 命令时需要root权限,如果执行命令时用得不是root账户,需要使用sudo命令,比如安装python-pip时,需要输入

sudo apt-get install python-pip

参考how-to-set-up-mysql-for-python-on-ubuntu/