Python 安装MySQLdb出错问题排查

Python安装MySQL-python提示Error:Command errored out with exit status 1:...的问题排查

Posted by Steven on 2021-07-18
Estimated Reading Time 3 Minutes
Words 622 In Total
Viewed Times

使用pip在安装第三方库时:

1
pip install MySQL-Python

时,报错:

WechatIMG433

google了好多原因,不乏于:

需要安装mysql-connector-c、权限用户、安装其他的依赖等等,试了好多都不行,最终还是尝试再次读抛错的异常信息,发现自己刚才没有看到的一点信息

1
xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun

看到CommandLineTools想起是不是xcode-select没有安装,安装:

1
xcode-select --install

等待安装完成,再次pip install MySQL-Python,又报错了

1
2
3
4
5
6
7
8
9
10
11
12
13
pip install MySQL-python
......
running build_ext
building '_mysql' extension
creating build/temp.macosx-10.15-x86_64-2.7
cc -fno-strict-aliasing -fno-common -dynamic -g -Os -pipe -fno-common -fno-strict-aliasing -fwrapv -DENABLE_DTRACE -DMACOSX -DNDEBUG -Wall -Wstrict-prototypes -Wshorten-64-to-32 -iwithsysroot /usr/local/libressl/include -DNDEBUG -g -fwrapv -Os -Wall -Wstrict-prototypes -DENABLE_DTRACE -I/usr/local/opt/openssl@1.1/include -arch x86_64 -pipe -Dversion_info=(1,2,5,'final',1) -D__version__=1.2.5 -I/usr/local/Cellar/mysql/8.0.19_1/include/mysql -I/Users/zhangli53/.virtualenvs/mega/include/python2.7 -c _mysql.c -o build/temp.macosx-10.15-x86_64-2.7/_mysql.o
_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
----------------------------------------
ERROR: Failed building wheel for MySQL-python

找不到my_config.h文件,尝试查找一下

1
find / -name "my_config.h"

并没有找到,尝试再次在安装mysql的安装目录中查找:

最后发现目录 /usr/local/include/mysql 下有个 mysql.h 文件,这个就应该是我们要找的my_config.h 文件了吧:

1
cp mysql.h my_config.h

再次 pip install MySQL-python 就已经安装成功了!!!

其实最开始考虑的是,如果还是不行,尝试在其他的环境找一个文件copy过来,结果成功了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
steven@192 PyMySQL-0.10.0 % cd /usr/local/include/mysql
steven@192 mysql % ls
errmsg.h my_compress.h mysql.h mysql_version.h mysqlx_error.h
field_types.h my_list.h mysql_com.h mysqld_error.h mysqlx_version.h
my_command.h mysql mysql_time.h mysqlx_ername.h
steven@192 mysql % cp mysql.h my_config.h
steven@192 mysql % pip install MySQL-python
DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. pip 21.0 will drop support for Python 2.7 in January 2021. More details about Python 2 support in pip can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support pip 21.0 will remove support for this functionality.
Defaulting to user installation because normal site-packages is not writeable
Collecting MySQL-python
Using cached MySQL-python-1.2.5.zip (108 kB)
Building wheels for collected packages: MySQL-python
Building wheel for MySQL-python (setup.py) ... done
Created wheel for MySQL-python: filename=MySQL_python-1.2.5-cp27-cp27m-macosx_11_4_x86_64.whl size=44343 sha256=8d0732701cba17e4cfe5876ff1b64119637e475451a548ecbadc603f002dffad
Stored in directory: /Users/steven/Library/Caches/pip/wheels/55/eb/3b/661bdcd5ca5a576f0331400468db9d5dcbda118fb6c85fd3ee
Successfully built MySQL-python
Installing collected packages: MySQL-python
Successfully installed MySQL-python-1.2.5


如果您喜欢此博客或发现它对您有用,则欢迎对此发表评论。 也欢迎您共享此博客,以便更多人可以参与。 如果博客中使用的图像侵犯了您的版权,请与作者联系以将其删除。 谢谢 !