【问题标题】:LD_LIBRARY_PATH with sudo pythonLD_LIBRARY_PATH 与 sudo python
【发布时间】:2012-12-24 22:34:47
【问题描述】:

我正在尝试将更新的 Python (2.7.3) 与旧的 CentOS 一起使用。

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

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

我设置了PATHLD_LIBRARY_PATH 变量以使用/etc/profile.d/ 查找bin/python.so 文件。这似乎主要工作。

对于非 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.
>>> 

然而,$LD_LIBRARY_PATH hack 在使用 sudo 时似乎有点奇怪:

[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 不起作用。

我很好奇在没有-i 的情况下如何让sudo 选择正确的Python?

相关:

sudo changes PATH - why?

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

【问题讨论】:

    标签: python environment-variables sudo


    【解决方案1】:

    感谢this blog post。您可以通过在配置中与LDFLAGS 链接来放弃使用$LD_LIBRARY_PATH。其中#{ldlibpath}#{install_path}/lib

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

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

    【讨论】:

      猜你喜欢
      • 2011-05-14
      • 2013-08-16
      • 1970-01-01
      • 1970-01-01
      • 2011-09-26
      • 1970-01-01
      • 2020-12-06
      • 1970-01-01
      相关资源
      最近更新 更多