解决python2虚拟环境中MySQL-Python安装失败问题

本人在python2.7环境中安装MySQL-Python得时候报了以下错误:

_mysql.c:44:10: fatal error: 'my_config.h' file not found
    #include "my_config.h"
             ^~~~~~~~~~~~~
    1 error generated.
    error: command 'cc' failed with exit status 1
	Command "/Users/xf/.virtualenvs/project_display/bin/python -u -c "import setuptools, 
	tokenize;__file__='/private/var/folders/pg/1bt_g5h17p720sqqcq_8b64w0000gn/T/pip-install-zOi3EX/MySQL-
	python/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', 
	'\n');f.close();exec(compile(code, __file__, 'exec'))" install --record 
	/private/var/folders/pg/1bt_g5h17p720sqqcq_8b64w0000gn/T/pip-record-tjXICs/install-record.txt --single-version-
	externally-managed --compile --install-headers 
	/Users/xf/.virtualenvs/project_display/include/site/python2.7/MySQL-python" failed with error code 1 in 
	/private/var/folders/pg/1bt_g5h17p720sqqcq_8b64w0000gn/T/pip-install-zOi3EX/MySQL-python/

经过请教大牛,给出以下方案。而且成功解决问题:
问题原因,mysql-python安装时主要的错误是:在虚拟环境中没有MySQL相关的包:
解决办法
将环境中的mysql相关的包cp到相关的虚拟环境中:

sudo cp /usr/local/lib/python2.7/site-packages/_mysql* ~/.virtualenvs/subdomain/lib/python2.7/site-packages/
sudo cp -r /usr/local/lib/python2.7/site-packages/MySQL* ~/.virtualenvs/subdomain/lib/python2.7/site-packages

(路径需要换为自己的路径–查看路径:which python2.7)

copy完之后,成功完成安装:

在这里插入图片描述

除非需求必然要用mysql-Python,可以选择pymysql,较好的代替mysql-python,而且在python3中也可以使用

你可能感兴趣的:(python)