使用 pythonbrew 编译 Python 3.2 和 2.7 时出现问题
我正在尝试使用 Pythonbrew 来构建多个版本的 Python,但遇到了一些测试失败的问题。这是在一个运行着 Ubuntu 8.04 32位 的虚拟机上进行的。
这个问题发生在我使用 --enable-shared
这个选项的时候。
下面是 build.log 文件中的错误信息:
Python 3.2
这是 Python 3.2 的测试失败情况:
LD_LIBRARY_PATH=/opt/standingcloud/stack/pythonbrew/build/Python-3.2: ./python -E -c 'import sys ; from sysconfig import get_platform ; print(get_platform()+"-"+sys.version[0:3])' >platform
find ./Lib -name '*.py[co]' -print | xargs rm -f
LD_LIBRARY_PATH=/opt/standingcloud/stack/pythonbrew/build/Python-3.2: ./python -Wd -E -bb ./Lib/test/regrtest.py -l
== CPython 3.2 (r32:88445, Jun 16 2011, 15:07:10) [GCC 4.2.4 (Ubuntu 4.2.4-1ubuntu4)]
== Linux-2.6.18.8-linode22-i686-with-debian-lenny-sid little-endian
== /opt/standingcloud/stack/pythonbrew/build/Python-3.2/build/test_python_18839
Testing with flags: sys.flags(debug=0, division_warning=0, inspect=0, interactive=0, optimize=0, dont_write_bytecode=0, no_user_site=0, no_site=0, ignore_environment=1, verbose=0, bytes_warning=2, quiet=0)
....SNIP....
[197/349] test_os
test test_os failed -- Traceback (most recent call last):
File "/opt/standingcloud/stack/pythonbrew/build/Python-3.2/Lib/test/test_os.py", line 646, in test_exist_ok_existing_directory
os.makedirs(path, mode=mode, exist_ok=True)
File "/opt/standingcloud/stack/pythonbrew/build/Python-3.2/Lib/os.py", line 152, in makedirs
mkdir(name, mode)
OSError: [Errno 17] File exists: '@test_18839_tmp/dir1'
....SNIP....
321 tests OK.
1 test failed:
test_os
27 tests skipped:
test_codecmaps_cn test_codecmaps_hk test_codecmaps_jp
test_codecmaps_kr test_codecmaps_tw test_curses test_dbm_gnu
test_dbm_ndbm test_gdb test_ioctl test_kqueue test_ossaudiodev
test_pep277 test_smtpnet test_socketserver test_sqlite
test_startfile test_timeout test_tk test_ttk_guionly
test_unicode_file test_urllib2net test_urllibnet test_winreg
test_winsound test_xmlrpc_net test_zipfile64
5 skips unexpected on linux2:
test_dbm_gnu test_dbm_ndbm test_ioctl test_tk test_ttk_guionly
sys:1: ResourceWarning: unclosed file <_io.TextIOWrapper name='/dev/null' mode='a' encoding='ANSI_X3.4-1968'>
make: *** [test] Error 1
Python 2.7.1
这是 Python 2.7.1 的测试失败情况:
./python -Wd -3 -E -tt ./Lib/test/regrtest.py -l
== CPython 2.7.1 (r271:86832, Jun 16 2011, 17:59:28) [GCC 4.2.4 (Ubuntu 4.2.4-1ubuntu4)]
== Linux-2.6.18.8-linode22-i686-with-debian-lenny-sid little-endian
== /opt/standingcloud/stack/pythonbrew/build/Python-2.7.1/build/test_python_19286
....SNIP....
test test_file2k failed -- Traceback (most recent call last):
File "/opt/standingcloud/stack/pythonbrew/build/Python-2.7.1/Lib/test/test_file2k.py", line 219, in testStdin
self.assertRaises(IOError, sys.stdin.seek, -1)
AssertionError: IOError not raised
....SNIP....
343 tests OK.
2 tests failed:
test_file test_file2k
41 tests skipped:
test_aepack test_al test_applesingle test_bsddb test_bsddb185
test_bsddb3 test_cd test_cl test_codecmaps_cn test_codecmaps_hk
test_codecmaps_jp test_codecmaps_kr test_codecmaps_tw test_curses
test_dbm test_gdb test_gdbm test_gl test_imgfile test_ioctl
test_kqueue test_linuxaudiodev test_macos test_macostools
test_ossaudiodev test_pep277 test_scriptpackages test_smtpnet
test_socketserver test_sqlite test_startfile test_sunaudiodev
test_timeout test_tk test_ttk_guionly test_unicode_file
test_urllib2net test_urllibnet test_winreg test_winsound
test_zipfile64
8 skips unexpected on linux2:
test_bsddb test_bsddb3 test_dbm test_gdb test_gdbm test_ioctl
test_tk test_ttk_guionly
make: *** [test] Error 1
Python 2.7
这是 Python 2.7 的测试失败情况:
LD_LIBRARY_PATH=/opt/standingcloud/stack/pythonbrew/build/Python-2.7: ./python -E -c 'import sys ; from sysconfig import get_platform ; print get_platform()+"-"+sys.version[0:3]' >platform
find ./Lib -name '*.py[co]' -print | xargs rm -f
LD_LIBRARY_PATH=/opt/standingcloud/stack/pythonbrew/build/Python-2.7: ./python -Wd -3 -E -tt ./Lib/test/regrtest.py -l
== CPython 2.7 (r27:82500, Jun 16 2011, 15:25:09) [GCC 4.2.4 (Ubuntu 4.2.4-1ubuntu4)]
== Linux-2.6.18.8-linode22-i686-with-debian-lenny-sid little-endian
== /opt/standingcloud/stack/pythonbrew/build/Python-2.7/build/test_python_13541
....SNIP....
test_distutils
/usr/bin/ld: cannot find -lpython2.7
collect2: ld returned 1 exit status
test test_distutils failed -- Traceback (most recent call last):
File "/opt/standingcloud/stack/pythonbrew/build/Python-2.7/Lib/distutils/tests/test_build_ext.py", line 269, in test_get_outputs
cmd.run()
File "/opt/standingcloud/stack/pythonbrew/build/Python-2.7/Lib/distutils/command/build_ext.py", line 340, in run
self.build_extensions()
File "/opt/standingcloud/stack/pythonbrew/build/Python-2.7/Lib/distutils/command/build_ext.py", line 449, in build_extensions
self.build_extension(ext)
File "/opt/standingcloud/stack/pythonbrew/build/Python-2.7/Lib/distutils/command/build_ext.py", line 531, in build_extension
target_lang=language)
File "/opt/standingcloud/stack/pythonbrew/build/Python-2.7/Lib/distutils/ccompiler.py", line 741, in link_shared_object
extra_preargs, extra_postargs, build_temp, target_lang)
File "/opt/standingcloud/stack/pythonbrew/build/Python-2.7/Lib/distutils/unixccompiler.py", line 258, in link
raise LinkError, msg
LinkError: command 'gcc' failed with exit status 1
....SNIP....
test test_file failed -- multiple errors occurred; run in verbose mode for details
test_file2k
test test_file2k failed -- Traceback (most recent call last):
File "/opt/standingcloud/stack/pythonbrew/build/Python-2.7/Lib/test/test_file2k.py", line 211, in testStdin
self.assertRaises(IOError, sys.stdin.seek, -1)
AssertionError: IOError not raised
....SNIP....
339 tests OK.
3 tests failed:
test_distutils test_file test_file2k
1 test altered the execution environment:
test_site
41 tests skipped:
test_aepack test_al test_applesingle test_bsddb test_bsddb185
test_bsddb3 test_cd test_cl test_codecmaps_cn test_codecmaps_hk
test_codecmaps_jp test_codecmaps_kr test_codecmaps_tw test_curses
test_dbm test_gdb test_gdbm test_gl test_imgfile test_ioctl
test_kqueue test_linuxaudiodev test_macos test_macostools
test_ossaudiodev test_pep277 test_scriptpackages test_smtpnet
test_socketserver test_sqlite test_startfile test_sunaudiodev
test_timeout test_tk test_ttk_guionly test_unicode_file
test_urllib2net test_urllibnet test_winreg test_winsound
test_zipfile64
8 skips unexpected on linux2:
test_bsddb test_bsddb3 test_dbm test_gdb test_gdbm test_ioctl
test_tk test_ttk_guionly
make: [test] Error 1 (ignored)
3 个回答
Ryan Thompson的回答挺不错的... LD_RUN_PATH
是解决这个问题的正确方法。
不过,与其写死一个绝对路径,不如使用:
LD_RUN_PATH='$ORIGIN/../lib'
... 这里的 $ORIGIN
是正在运行的程序(比如python)的当前工作目录(PWD)。
# objdump -x /home/arisinger/.pythonbrew/pythons/Python-2.7.3/bin/python|grep RPATH
RPATH $ORIGIN/../lib
# ldd .pythonbrew/pythons/Python-2.7.3/bin/python
[...]
libpython2.7.so.1.0 => /home/arisinger/.pythonbrew/pythons/Python-2.7.3/bin/../lib/libpython2.7.so.1.0 (0x00007f43994f6000)
[...]
... 上面的设置也能正确安装pip等工具。最后,从 man ld.so
中可以看到:
$ORIGIN and rpath
ld.so understands the string $ORIGIN (or equivalently ${ORIGIN}) in an
rpath specification (DT_RPATH or DT_RUNPATH) to mean the directory con‐
taining the application executable. Thus, an application located in
somedir/app could be compiled with gcc -Wl,-rpath,'$ORIGIN/../lib' so
that it finds an associated shared library in somedir/lib no matter
where somedir is located in the directory hierarchy. This facilitates
the creation of "turn-key" applications that do not need to be
installed into special directories, but can instead be unpacked into
any directory and still find their own shared libraries.
编辑:可惜的是,虚拟环境(venvs)会出现问题,因为virtualenv会复制 python
二进制文件(这会改变 $ORIGIN
),但并不会复制(或创建符号链接)库文件,所以在虚拟环境中的C语言或内置动态模块最终会链接到系统的python... 这可不好 :-( ...
... 我认为这是一个需要修复的bug(在virtualenv中?),但一个简单的解决办法是手动将 libpythonX.Y.so*
复制或创建符号链接到虚拟环境的lib/目录中... 或者做一些看起来不太好的事情,比如(注意X.Y.Z):
LD_RUN_PATH='$ORIGIN/../lib:$ORIGIN/../../../../pythons/Python-X.Y.Z/lib'
... 选择你觉得更合适的方式;这两种方法都能正常工作。
我想我找到了一种方法,可以让这个工作不需要设置 LD_LIBRARY_PATH
。
我发现有一个环境变量叫 LD_RUN_PATH
,它和 LD_LIBRARY_PATH
很像,只不过你是在编译程序的时候设置它,而不是在运行的时候。这样一来,每次运行程序时,它都会记住这个路径(所以你就不需要在运行时再设置 LD_LIBRARY_PATH
)。
所以,如果你在使用 pythonbrew 安装 Python 的时候,适当地设置 LD_RUN_PATH
,那么每次运行它的时候,它就能找到自己的共享库。例如,下面这个命令对我来说是成功的,包括安装 distutils 和 pip,这样模块安装就可以直接使用,不需要再做其他设置。
LD_RUN_PATH=$HOME/.pythonbrew/pythons/Python-2.7.3/lib pythonbrew install -j2 -C '--enable-shared' --force -v 2.7.3
然后我可以用 pythonbrew use 2.7.3
来激活它,它就能正常工作,不需要在你的 shell 初始化文件里设置任何环境变量。
当然,你需要提前知道 pythonbrew 会把你的 Python 安装在哪里,这样才能正确设置 LD_RUN_PATH
。一般来说,它会在 $HOME/.pythonbrew/pythons/Python-$VERSION/lib
,但如果这个路径不行,你可以先安装一次(不加 --enable-shared
),然后找到 lib
目录。
我相信错误发生的原因是,在使用 --enable-shared
安装后,你安装的 Python 找到了系统 Python 的共享库并使用了它,如果这两个 Python 的构建方式有任何不匹配,就会导致问题。使用 LD_LIBRARY_PATH
或 LD_RUN_PATH
的解决方案可以强制你的 Python 使用它自己的共享库。
目前,我成功让pythonbrew在2.7.2版本上运行,步骤如下:
a) 首先,安装编译python所需的所有依赖项。
'curl' # not for build, but for steps after
'build-essential',
'libbz2-dev',
'libsqlite3-dev',
'zlib1g-dev',
'libxml2-dev',
'libxslt1-dev',
'libreadline5', # lenny
'libreadline5-dev', # lenny
'libgdbm-dev',
'libgdb-dev',
'libxml2',
'libssl-dev',
'tk-dev',
'libgdbm-dev',
'libexpat1-dev',
'libncursesw5-dev'
b) 接下来安装2.7.2版本:
pythonbrew install --configure="--with-threads --enable-shared" -j2 -v 2.7.2
这个安装过程是可以完成的,但会遇到一个错误:
/home/python-deploy/.pythonbrew/pythons/Python-2.7.2/bin/python: 错误 加载共享库时出错: libpython2.7.so.1.0: 无法打开 共享对象文件: 没有那个文件或目录
*为了避免在构建后安装setuptools时出现错误,可以使用 --no-setuptools 选项,并且在某些平台上可能需要使用 --force,因为gdb测试由于测试本身的错误而失败。
pythonbrew install --configure="--with-threads--enable-shared" \
--force \
--no-setuptools \
--jobs=2 \
--verbose 2.7.2
c) 最简单的解决方法是这样做(记得替换成你pythonbrew安装的正确路径):
pythonbrew use 2.7.2
export LD_LIBRARY_PATH=$HOME/.pythonbrew/pythons/Python-2.7.2/lib
或者你可以永久添加这个路径,把pythonbrew的库路径放在一个文件里,放在 /etc/ld.so.conf.d 目录下,这样在启动时会自动添加,或者你也可以立即运行 ldconfig 来添加:
sudo echo /home/user/.pythonbrew/pythons/Python-2.7.2/lib >> /etc/ld.so.conf.d/pythonbrew.conf
sudo ldconfig
d) 手动安装setuptools或distribute,也就是:
curl -O http://python-distribute.org/distribute_setup.py
python distribute_setup.py && easy_install pip
这样你就可以开始使用了。这个过程只在vagrant debian lenny虚拟机上测试过2.7.2版本。