解决 Centos 安装 Python 3.10 的报错: Could not import runpy module

操作环境CentOS 7Gcc 4.8.5Python 3.10.0

系统上已经有 2.x3.6 版本的 Python 了,但是还是想装一个 3.10 的。因为刚写的脚本文件是较高版本的,在 3.6 上无法正常运行,Python 语法不是很了解,只能从环境上下手了。

下载源码文件,然后开始执行操作:

bash 复制代码
./configure --enable-optimizations
make
sudo make install

然后报错如下,下面是两个机器不同的报错:

bash 复制代码
# 报错一:
gcc -pthread     -Xlinker -export-dynamic -o python Programs/python.o libpython3.10.a -lcrypt -lpthread -ldl  -lutil -lm   -lm 
./python -E -S -m sysconfig --generate-posix-vars ;\
if test $? -ne 0 ; then \
	echo "generate-posix-vars failed" ; \
	rm -f ./pybuilddir.txt ; \
	exit 1 ; \
fi
Could not import runpy module
Traceback (most recent call last):
  File "/opt/Python-3.10.0/Lib/runpy.py", line 15, in <module>
    import importlib.util
  File "/opt/Python-3.10.0/Lib/importlib/util.py", line 14, in <module>
    from contextlib import contextmanager
  File "/opt/Python-3.10.0/Lib/contextlib.py", line 4, in <module>
    import _collections_abc
SystemError: <built-in function compile> returned NULL without setting an exception
generate-posix-vars failed
make: *** [pybuilddir.txt] Error 1
bash 复制代码
# 报错二:
make build_all CFLAGS_NODIST=" -fprofile-use -fprofile-correction" LDFLAGS_NODIST=""
make[1]: Entering directory `/usr/local/src/Python-3.10.0'
./python -E -S -m sysconfig --generate-posix-vars ;\
if test $? -ne 0 ; then \
	echo "generate-posix-vars failed" ; \
	rm -f ./pybuilddir.txt ; \
	exit 1 ; \
fi
Could not import runpy module
Traceback (most recent call last):
  File "/opt/Python-3.10.0/Lib/runpy.py", line 15, in <module>
    import importlib.util
  File "/opt/Python-3.10.0/Lib/importlib/util.py", line 14, in <module>
    from contextlib import contextmanager
  File "/opt/Python-3.10.0/Lib/contextlib.py", line 4, in <module>
    import _collections_abc
SystemError: <built-in function compile> returned NULL without setting an error
generate-posix-vars failed
make[1]: *** [pybuilddir.txt] Error 1
make[1]: Leaving directory `/opt/Python-3.10.0'
make: *** [profile-opt] Error 2

导致原因

在低版本的gcc版本,使用 gcc 命令并带有 --enable-optimizations 参数时会出现上面问题。在 gcc 8.1.0 此问题已经修复。

解决方法

1、升级gcc8.1.0,不是很推荐,可能会影响其他软件的编译。

2、./configure参数中去掉 --enable-optimizations。然后执行下面的命令:

bash 复制代码
./configure
make
sudo make install

如果还不行就将解压的 Python 目录删除掉,重新解压,然后重新执行上面的命令。

Python 镇楼!!!点下关注好不好,我想申请博客专家!!! 哭唧唧!!!

个人博客: Roc's Blog

相关推荐
GarrettGao41 分钟前
Frida常见用法
javascript·python·逆向
Juchecar1 小时前
Pandas技巧:利用 category 类型节省内存
python
跟橙姐学代码2 小时前
Python时间处理秘籍:别再让日期时间卡住你的代码了!
前端·python·ipython
mortimer4 小时前
Python 文件上传:一个简单却易犯的错误及解决方案
人工智能·python
Juchecar5 小时前
NumPy编程:鼓励避免 for 循环
python
Java陈序员5 小时前
直播录制神器!一款多平台直播流自动录制客户端!
python·docker·ffmpeg
c8i5 小时前
drf 在django中的配置
python·django
这里有鱼汤7 小时前
【花姐小课堂】新手也能秒懂!用「风险平价」打造扛造的投资组合
后端·python
databook21 小时前
Manim实现闪光轨迹特效
后端·python·动效
Juchecar1 天前
解惑:NumPy 中 ndarray.ndim 到底是什么?
python