使用python sqlalchamy的时候,配置mysql 连接的时候:

engine = create_engine("mysql+mysqldb://************")  

报告了错误:

ImproperlyConfigured("Error loading MySQLdb module: %s" % e)  
ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb  

安装

pip install mysql-python  
pip install mysql-python fails with EnvironmentError: mysql_config not found  

(python 2.7 测试成功)使用

sudo pip install MySQL-python  

(python 3.6)解决办法:

pip3 install mysqlclient  

此时还会报错:

/bin/sh: mysql_config: command not found
*************
OSError: mysql_config not found  

这是因为在环境变量中找不到mysqlconfig。 在virtualenv环境中: 修改 VIRTUALENVDIR(虚拟环境路径)/bin/activate 添加:

_OLD_VIRTUAL_PATH="$PATH"  
PATH="$VIRTUAL_ENV/bin:$PATH"  
PATH="$PATH:/usr/local/bin/"  
export PATH  

注意:上一行的路径是我的osx的路径,机器路径在bash中运行:

which mysql_config  

电脑会给出:

/usr/local/bin/mysql_config

然后运行:

source VIRTUALENV_DIR(虚拟环境路径)/bin/activate  

如果没有路径 安装:

yum install mysql-devel (centos)  
apt-get install libmysqlclient-dev (ubuntu)

pip install mysql-connector-python  

再安装:

pip3 install mysqlclient  

即可修复。 来源:StackoverFlow && StackoverFlow 亲测成功。

另外一个解决办法:

up vote  
4  
down vote  
Use pymysql and it should solve your problem.Use pip3 install pymysql. After installing , in file directory project/project/__init__.py write this

import pymysql  
pymysql.install_as_MySQLdb()  

src/twisted/test/raiser.c:4:20: fatal error: Python.h: 没有那个文件或目录
compilation terminated. error: command 'x86_64-linux-gnu-gcc' failed with exit status 1

First install:

sudo apt-get install libmysqlclient-dev  

then:

sudo apt-get install python3.6-dev  

Care: you should install the virsion pythonXX-dev that witch virsion your python is

    _mysql.c:37:20: 致命错误:Python.h:没有那个文件或目录
     #include "Python.h"

解决方案:

yum install python36-devel