如何成功编译python 3.x

问题描述 投票:0回答:6

在尝试编译 python 3.7 时,我点击了

Could not import runpy module
:

jeremyr@b88:$ wget https://www.python.org/ftp/python/3.7.3/Python-3.7.3.tar.xz
....
jeremyr@b88:~/Python-3.7.3$ ./configure --enable-optimizations    
jeremyr@b88:~/Python-3.7.3$ make clean 
jeremyr@b88:~/Python-3.7.3$ make -j32 
.... 

gcc -pthread     -Xlinker -export-dynamic -o Programs/_testembed Programs/_testembed.o libpython3.7m.a -lcrypt -lpthread -ldl  -lutil   -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 "/home/jeremyr/Python-3.7.3/Lib/runpy.py", line 15, in <module>
    import importlib.util
  File "/home/jeremyr/Python-3.7.3/Lib/importlib/util.py", line 14, in <module>
    from contextlib import contextmanager
  File "/home/jeremyr/Python-3.7.3/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
Makefile:603: recipe for target 'pybuilddir.txt' failed
make[1]: *** [pybuilddir.txt] Error 1
make[1]: Leaving directory '/home/jeremyr/Python-3.7.3'
Makefile:531: recipe for target 'profile-opt' failed
make: *** [profile-opt] Error 2

jeremyr@88:~/Python-3.7.3$ lsb_release -a
No LSB modules are available.
Distributor ID: Debian
Description:    Debian GNU/Linux 8.11 (jessie)
Release:    8.11
Codename:   jessie

jeremyr@88:~/Python-3.7.3$ gcc --version 
gcc (Debian 4.9.2-10+deb8u2) 4.9.2
Copyright (C) 2014 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

jeremyr@88:~/Python-3.7.3$ sudo apt upgrade gcc
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Calculating upgrade... gcc is already the newest version.

jeremyr@b88:~/Python-3.7.3$ echo $PYTHONPATH

任何关于如何克服这个问题并安装 python3.7 的建议表示赞赏。

编辑 - 下面列出的解决方案似乎适用于其他各种 python 版本,因此我将标题从 3.7 更改为 python 3.x

python gcc compiler-errors python-3.7 runpy
6个回答
45
投票

看来启用优化是问题所在,

jeremyr@b88:~/Python-3.7.3$ ./configure   
jeremyr@b88:~/Python-3.7.3$ make clean 

在我的情况下会处理它。


12
投票

如果其他人遇到这个问题:我在 Centos 7 上遇到了同样的问题。我也有

--enable-optimizations
但不想删除该标志。更新我的构建依赖项然后重新运行解决了问题。为此,我跑了:

sudo yum groupinstall "Development Tools" -y

如果 yum 组不可用,您还可以使用以下命令单独安装软件包:

sudo yum install bison byacc cscope ctags cvs diffstat doxygen flex gcc gcc-c++ gcc-gfortran gettext git indent intltool libtool patch patchutils rcs redhat-rpm-config rpm-build subversion swig systemtap

2
投票

对于 MicGer 的答案不起作用并且想要保留 --enable-optimizations 的人,请检查您的 gcc 版本。该错误已在 gcc 8.3.0 上为我解决。


1
投票

对于我来说,CentOS 7,编译Python3.9,修复方法是更新gcc和相关包。


0
投票

删除 --enable-optimizations 并没有解决这里的问题,也没有将 GCC 从 gcc-7.5.0 升级到 gcc-8.2.1 尝试在 opensuse-15.2 上构建 Python 3.6.13。

由于某种原因,构建选择本地 python 安装,因此在调用 make 之前,我已更新 PATH env 以包含构建目录(生成 python 文件),例如:export PATH=$(pwd):$PATH


0
投票

#
# build python on centos 7
#

set -e
set -x

hub=/rpm/packages

mkdir -p ${hub}

cd $hub

yum groupinstall "Development Tools" -y

#
# download and install python deps
#   with saving deb to keep folder
#
yumdownloader --downloadonly --resolve -y \
    curl openssl-devel make kernel-devel \
    cmake pkgconfig mesa-libGL \
    bzip2-devel libffi-devel xz-devel zlib-devel \
    ncurses-devel gdbm-devel nss-tools readline-devel sqlite-devel tk tk-devel glibc-devel

yum install -y *.rpm

#
# update gcc
#

yum install -y centos-release-scl
yum install -y devtoolset-9

source /opt/rh/devtoolset-9/enable

echo "Current GCC: $(gcc --version)"


cd /tmp

PYTHON='3.8.17'

mkdir .Python3.8
cd .Python3.8

curl -O https://www.python.org/ftp/python/${PYTHON}/Python-${PYTHON}.tar.xz
tar -xf Python-${PYTHON}.tar.xz

cd Python-${PYTHON}

./configure \
    --enable-optimizations \
    --enable-loadable-sqlite-extensions \
    --prefix=/usr/local \
    --enable-shared LDFLAGS="-Wl,-rpath /usr/local/lib"

# build
make -j 4
# make links
make altinstall

# check
python3.8 -V
© www.soinside.com 2019 - 2024. All rights reserved.