在安装 psutil 模块的时候,遇到个奇怪的现象。在使用 python 3.7.* 或者更新的版本的时候,执行 pip install psutil 会报错;而在切换为 3.6.5 版本后,该模块安装的缺很丝滑,直接就成功了。很是无语。。。
报错内容如下:
#pip install psutil
Collecting psutil
Using cached psutil-5.7.3.tar.gz (465 kB)
ERROR: Command errored out with exit status 1:
command: /root/.pyenv/versions/3.8.6/bin/python3.8 -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-og3nx3nb/psutil/setup.py'"'"'; __file__='"'"'/tmp/pip-install-og3nx3nb/psutil/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /tmp/pip-pip-egg-info-rjtdoy9h
cwd: /tmp/pip-install-og3nx3nb/psutil/
Complete output (11 lines):
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/root/.pyenv/versions/3.8.6/lib/python3.8/site-packages/setuptools/__init__.py", line 23, in <module>
from setuptools.dist import Distribution
File "/root/.pyenv/versions/3.8.6/lib/python3.8/site-packages/setuptools/dist.py", line 34, in <module>
from setuptools import windows_support
File "/root/.pyenv/versions/3.8.6/lib/python3.8/site-packages/setuptools/windows_support.py", line 2, in <module>
import ctypes
File "/root/.pyenv/versions/3.8.6/lib/python3.8/ctypes/__init__.py", line 7, in <module>
from _ctypes import Union, Structure, Array
ModuleNotFoundError: No module named '_ctypes'
----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
在查阅资料,及请教前辈后终于解决该问题。下面记录一下解决该问题的过程
- 首先以“ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.” 切入,结果做到更新 pip 和 setuptools 之后发现跑偏了;
- 然后以“ModuleNotFoundError: No module named '_ctypes'” 切入,发现了问题的关键。
原来是因为 Python3 中有个内置模块叫 ctypes,它是 Python3 的外部函数库模块,它提供兼容C语言的数据类型,并通过它调用 Linux 系统下的共享库(Shared library),此模块需要使用 CentOS7 系统中外部函数库(Foreign function library)的开发链接库(头文件和链接库)。
由于在CentOS7系统中没有安装外部函数库(libffi)的开发链接库软件包,所以在安装pip的时候就报了"ModuleNotFoundError: No module named '_ctypes'"的错误。
- 既然找到了原因那么问题就好解决了
// 首先需要安装外部函数库(libffi)
yum install libffi-devel -y
// 然后再重新安装一下Python就可以了
// 我用的是 pyenv,所以切换起来比较方便
pyenv uninstall 3.8.6
pyenv install 3.8.6
cd ~/pycode
pyenv local 3.8.6
// 最后再次安装 psutil,结果显而易见
pip install psutil
Collecting psutil
Using cached psutil-5.7.3.tar.gz (465 kB)
Using legacy 'setup.py install' for psutil, since package 'wheel' is not installed.
Installing collected packages: psutil
Running setup.py install for psutil ... done
Successfully installed psutil-5.7.3
在使用Python 3.7及以上版本通过pip安装psutil模块时出现错误,而降级到3.6.5则顺利安装。错误主要为'Command errored out with exit status 1'和'ModuleNotFoundError: No module named '_ctypes''。问题根源在于Python的ctypes模块依赖于libffi开发库,未在CentOS7系统中安装该库导致。解决方案是安装libffi的开发链接库。
pip 安装 psutil 模块报错,及解决办法&spm=1001.2101.3001.5002&articleId=109787591&d=1&t=3&u=5f72692122044e728595789929ac7705)
5587

被折叠的 条评论
为什么被折叠?



