LD_LIBRARY_PATH与sudo python

时间:2012-12-24 22:34:47

标签: python environment-variables sudo

我正在尝试使用较新的Python(2.7.3)和旧的CentOS。

我有一个将python安装到非标准位置的方法:

 ./configure --prefix=#{install_path} --with-threads --enable-shared --with-zlib=/usr/include
 make
 make install

我使用PATH设置LD_LIBRARY_PATHbin/python变量以查找.so/etc/profile.d/个文件。这似乎主要起作用。

对于非root用户,我得到了正确的Python:

[vagrant@localhost ~]$ python
Python 2.7.3 (default, Dec 24 2012, 15:18:59) 
[GCC 4.1.2 20080704 (Red Hat 4.1.2-50)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 

使用root用户,我得到了正确的Python:

[vagrant@localhost ~]$ sudo su
[root@localhost vagrant]# python
Python 2.7.3 (default, Dec 24 2012, 15:18:59) 
[GCC 4.1.2 20080704 (Red Hat 4.1.2-50)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 

然而,使用sudo时,$LD_LIBRARY_PATH黑客似乎有点不知所措:

[vagrant@localhost ~]$ sudo python
python: error while loading shared libraries: libpython2.7.so.1.0:
cannot open shared object file: No such file or directory

即使变量看起来正确:

[vagrant@localhost ~]$ sudo which python
/opt/Python-2.7.3/bin/python

Defaults env_keep += "LD_LIBRARY_PATH"添加到/etc/sudoers不起作用。

sudo -i python确实有用。 sudo -E python不起作用。

我很好奇我可以做些什么来让sudo在没有-i的情况下选择合适的Python?

相关:

sudo changes PATH - why?

https://stackoverflow.com/questions/12593336/odd-path-behaviour-on-centos-python

1 个答案:

答案 0 :(得分:1)

感谢this blog post。您可以通过在配置中与$LD_LIBRARY_PATH相关联来放弃使用LDFLAGS#{ldlibpath}#{install_path}/lib的地方:

./configure --prefix=#{install_path} --with-threads --enable-shared \
--with-zlib=/usr/include LDFLAGS="-Wl,-rpath #{ldlibpath}"

正如博客文章中所述,在运行configure之前,您需要mkdir这个ldlibpath。