CentOS7上 pip install MySQL-python 安装失败(error: command 'gcc' failed with exit status 1)的解决办法

在CentOS7环境下,使用pip安装MySQL-python时遇到'command 'gcc' failed with exit status 1'的错误。解决方法是通过运行'sudo yum install python-devel'来安装python-devel,然后再次尝试pip安装,问题即可得到解决。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

(pk_env) [dechao@s97-74-237-68 ~]$ pip install MySQL-python                
Collecting MySQL-python
 Using cached MySQL-python-1.2.5.zip
Building wheels for collected packages: MySQL-python
 Running setup.py bdist_wheel for MySQL-python ... error
 Complete output from command /home/dechao/pk_env/bin/python2.7 -u -c "import setuptools, tokenize;_
_file__='/tmp/pip-build-VCoMlt/MySQL-python/setup.py';f=getattr(tokenize, 'open', open)(__file__);cod
e=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" bdist_wheel -d /tmp
/tmp1M4ik7pip-wheel- --python-tag cp27:
 running bdist_wheel
 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
 gcc -pthread -fno-strict-aliasing -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-p
rotector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -D_GNU_SOURCE -fP
IC -fwrapv -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong
--param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -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:29:20: 致命错误:Python.h:没有那个文件或目录
  #include "Python.h"
                     ^
 编译中断。
 error: command 'gcc' failed with exit status 1
  
 ----------------------------------------
 Failed building wheel for MySQL-python
 Running setup.py clean for MySQL-python
Failed to build MySQL-python
Installing collected packages: MySQL-python
 Running setup.py install for MySQL-python ... error
   Complete output from command /home/dechao/pk_env/bin/python2.7 -u -c "import setuptools, tokenize
;__file__='/tmp/pip-build-VCoMlt/MySQL-python/setup.py';f=getattr(tokenize, 'open', open)(__file__);c
ode=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record
/tmp/pip-OAwTQG-record/install-record.txt --single-version-externally-managed --compile --install-hea
ders /home/dechao/pk_env/include/site/python2.7/MySQL-python:
   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
   gcc -pthread -fno-strict-aliasing -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack
-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -D_GNU_SOURCE -
fPIC -fwrapv -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-stron
g --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -fP
IC -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:29:20: 致命错误:Python.h:没有那个文件或目录
    #include "Python.h"
                       ^
   编译中断。
   error: command 'gcc' failed with exit status 1
    
   ----------------------------------------
Command "/home/dechao/pk_env/bin/python2.7 -u -c "import setuptools, tokenize;__file__='/tmp/pip-buil
d-VCoMlt/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-OAwTQG-record/i
nstall-record.txt --single-version-externally-managed --compile --install-headers /home/dechao/pk_env
/include/site/python2.7/MySQL-python" failed with error code 1 in /tmp/pip-build-VCoMlt/MySQL-python/

---------------------------- 安装 python-devel

(pk_env) [dechao@s97-74-237-68 ~]$ sudo yum install python-devel

----------------------------  

之后 pip instal MySQL-python 成功




<think>我们正在解决CentOS7上使用pip安装MySQL-python时出现的问题用户遇到的具体错误是:`EnvironmentError:mysql_confignotfound`以及`Command"pythonsetup.pyegg_info"failed`根据引用的内容,主要原因和解决方案如下:1.引用[1]和[2]指出:错误`mysql_confignotfound`是因为缺少mysql的开发文件2.引用[2]建议可以手动修改mysql_python安装配置,指定mysql_config的路径(但这不是首选,因为需要修改源码)3.引用[3]指出:mysql_config实际上来自于mysql-devel包(在MySQL社区版中叫做mysql-community-devel),同时还需要安装python-devel包因此,解决方案为:1.安装mysql-devel(或mysql-community-devel)和python-devel2.然后重新尝试安装MySQL-python注意:在CentOS7上,我们通常使用yum来安装这些依赖包。步骤:1.安装所需的依赖包(需要root权限或sudo):sudoyuminstallmysql-develpython-develgcc注意:如果使用的是MySQL社区版,则可能需要先安装MySQL官方的yum仓库,然后安装mysql-community-devel。但是,通常使用默认仓库中的mysql-devel即可。2.如果第一步成功,再使用pip安装MySQL-pythonpipinstallMySQL-python但是,用户还提到了pip版本过旧的问题,我们可以先升级pip(但注意,在Python2.7中,pip的升级可能有限制,因为Python2.7已经停止支持,建议升级到pip的最新可用版本即可)。如果使用以上方法仍然失败,可以尝试使用源码安装MySQL-python,并修改配置指向mysql_config的路径(如引用[2]所述),但这是备选方案。因此,我们给出详细步骤:</think> ### CentOS 7 安装 MySQL-python 的解决方案 根据您的报错信息 `mysql_config not found` 和引用[1][3],这是由于缺少 MySQL 开发依赖包导致的。以下是完整的解决方案: #### 1. 安装系统依赖 ```bash # 安装开发工具和MySQL依赖 sudo yum install -y python-devel gcc mysql-devel ``` - **python-devel**: Python 开发头文件(引用[3]) - **mysql-devel**: MySQL 客户端库和开发文件(解决 `mysql_config not found` 错误) - **gcc**: C 编译器(编译二进制扩展) > 如果使用 MySQL 社区版,可能需要额外安装: > `sudo yum install -y mysql-community-devel`(引用[3]) #### 2. 设置环境变量 ```bash # 确保编译器能找到 MySQL 头文件 export PATH=$PATH:/usr/local/mysql/bin # 替换为您的 MySQL 安装路径 ``` > 如引用[2]所述,如果自定义安装路径,需手动指定 `mysql_config` 路径 #### 3. 安装 MySQL-python ```bash # 使用 pip 安装(需 Python 2.7pip install MySQL-python ``` > 如果失败,可尝试强制安装指定版本: > `pip install MySQL-python==1.2.5` #### 4. 验证安装 ```python # Python 2.7 测试脚本 test_mysql.py import MySQLdb try: print("MySQLdb 版本:", MySQLdb.__version__) conn = MySQLdb.connect(host='localhost', user='test', passwd='') print("连接成功!") conn.close() except Exception as e: print("错误:", str(e)) ``` 运行: `python test_mysql.py` #### 常见错误排查 | 错误类型 | 解决方案 | |---------|---------| | `_mysql.so: undefined symbol: SSL_CTX_set_ecdh_auto` | 升级 OpenSSL:`sudo yum update openssl` | | `command 'gcc' failed with exit status 1` | 完整安装开发工具:`sudo yum groupinstall "Development Tools"` | | `pip not found` | 安装 pip:`sudo yum install -y python-pip` | > 注意:CentOS 7 默认 Python 版本为 2.7.5,建议使用虚拟环境避免系统污染 #### 原理说明 MySQL-python 需要编译 C 扩展: 1. `mysql-devel` 提供 MySQL C 头文件和 `mysql_config` 2. `python-devel` 提供 Python C 头文件 3. `gcc` 负责编译扩展模块 缺少任何组件都会导致报错[^1][^3]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值