安装MySQL-python失败

9

我正在尝试在Python 2.7虚拟环境中安装MySQL-Python,但出现了以下错误:

Installing collected packages: MySQL-python
Running setup.py install for MySQL-python ... error
ERROR: Command errored out with exit status 1:
 command: /home/jhylands/py2/bin/python -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-scNGlE/MySQL-python/setup.py'"'"'; __file__='"'"'/tmp/pip-install-scNGlE/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 /tmp/pip-record-FUMQGL/install-record.txt --single-version-externally-managed --compile --install-headers /home/jhylands/py2/include/site/python2.7/MySQL-python
     cwd: /tmp/pip-install-scNGlE/MySQL-python/
Complete output (30 lines):
running install
running build
running build_py
creating build
creating build/lib.linux-x86_64-2.7
copying _mysql_exceptions.py -> build/lib.linux-x86_64-2.7
creating build/lib.linux-x86_64-2.7/MySQLdb
copying MySQLdb/__init__.py -> build/lib.linux-x86_64-2.7/MySQLdb
copying MySQLdb/converters.py -> build/lib.linux-x86_64-2.7/MySQLdb
copying MySQLdb/connections.py -> build/lib.linux-x86_64-2.7/MySQLdb
copying MySQLdb/cursors.py -> build/lib.linux-x86_64-2.7/MySQLdb
copying MySQLdb/release.py -> build/lib.linux-x86_64-2.7/MySQLdb
copying MySQLdb/times.py -> build/lib.linux-x86_64-2.7/MySQLdb
creating build/lib.linux-x86_64-2.7/MySQLdb/constants
copying MySQLdb/constants/__init__.py -> build/lib.linux-x86_64-2.7/MySQLdb/constants
copying MySQLdb/constants/CR.py -> build/lib.linux-x86_64-2.7/MySQLdb/constants
copying MySQLdb/constants/FIELD_TYPE.py -> build/lib.linux-x86_64-2.7/MySQLdb/constants
copying MySQLdb/constants/ER.py -> build/lib.linux-x86_64-2.7/MySQLdb/constants
copying MySQLdb/constants/FLAG.py -> build/lib.linux-x86_64-2.7/MySQLdb/constants
copying MySQLdb/constants/REFRESH.py -> build/lib.linux-x86_64-2.7/MySQLdb/constants
copying MySQLdb/constants/CLIENT.py -> build/lib.linux-x86_64-2.7/MySQLdb/constants
running build_ext
building '_mysql' extension
creating build/temp.linux-x86_64-2.7
x86_64-linux-gnu-gcc -pthread -fno-strict-aliasing -Wdate-time -D_FORTIFY_SOURCE=2 -g -fdebug-prefix-map=/build/python2.7-1x6jhf/python2.7-2.7.18~rc1=. -fstack-protector-strong -Wformat -Werror=format-security -fPIC -Dversion_info=(1,2,5,'final',1) -D__version__=1.2.5 -I/usr/include/mysql -I/usr/include/python2.7 -c _mysql.c -o build/temp.linux-x86_64-2.7/_mysql.o
_mysql.c:44:10: fatal error: my_config.h: No such file or directory
   44 | #include "my_config.h"
      |          ^~~~~~~~~~~~~
compilation terminated.
error: command 'x86_64-linux-gnu-gcc' failed with exit status 1
----------------------------------------
ERROR: Command errored out with exit status 1: /home/jhylands/py2/bin/python -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-scNGlE/MySQL-python/setup.py'"'"'; __file__='"'"'/tmp/pip-install-scNGlE/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 /tmp/pip-record-FUMQGL/install-record.txt --single-version-externally-managed --compile --install-headers /home/jhylands/py2/include/site/python2.7/MySQL-python Check the logs for full command output.

我已经尝试安装这篇文章这篇文章中提供的解决方案。


操作系统是Ubuntu 20.04。 - jhylands
2个回答

16

所以,我通过以下命令解决了这个问题:

sudo wget https://raw.githubusercontent.com/paulfitz/mysql-connector-c/master/include/my_config.h -P /usr/include/mysql/

我从这个答案的评论中发现了它。


15
根据MySQL 8文档: https://dev.mysql.com/doc/relnotes/mysql/8.0/en/news-8-0-2.html 以下是与客户端程序开发相关的更改: 客户端程序只需要#include 头文件。特别地,my_config.h不再需要并且不再安装。
因此,新安装的libmysqlclient-dev可能会排除my_config.h。 然而,这也提供了一个非常简单的答案 - 当程序尝试包含my_config.h时,用mysql.h代替它:
sudo apt install libmysqlclient-dev
sudo ln -s /usr/include/mysql/mysql.h /usr/include/mysql/my_config.h

在添加了软链接后,我成功地在Ubuntu 20.04 LTS上安装了MySQL-python==1.2.5


仅使用链接命令,在Centos 8上工作。 - vinsent paramanantham

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接