解决 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 语法不是很了解,只能从环境上下手了。

【注】Python 其他版本也适用,毕竟是编译器的问题,并非 Python 版本问题。

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

./configure --enable-optimizations
make
sudo make install

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

# 报错一:
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
# 报错二:
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。然后执行下面的命令:

./configure
make
sudo make install

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

Python 镇楼!!!点下关注好不好,我想申请博客专家!!! 哭唧唧!!!
在这里插入图片描述
个人博客: Roc’s Blog

  • 7
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Roc.Chang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值