更改当前流程环境的LD_LIBRARY_PATH [英] Change current process environment's LD_LIBRARY_PATH
问题描述
是否可以更改当前进程的环境变量?
更具体地说,在python脚本中,我想更改 LD_LIBRARY_PATH
,以便在导入一个模块'x'时取决于一些 xyz.so
, xyz.so
取自LD_LIBRARY_PATH中的给定路径
是否有任何其他方式动态更改加载库的路径?
编辑:我认为我需要提到,我已经尝试过像
os.environ [LD_LIBRARY_PATH] = mypath
os.putenv('LD_LIBRARY_PATH',mypath)
但这些修改了env。对于生成的子进程,不是当前进程,模块加载不考虑新的LD_LIBRARY_PATH
Edit2 ,所以问题是可以改变环境或图书馆装载机看到的东西,从那里加载?
原因
os.environ [LD_LIBRARY_PATH] = ...
不工作很简单:这个环境变量控制Linux上的动态加载器( ld-linux.so.2
)的行为, ld.so.1
在Solaris),但加载程序只在进程启动时查看 LD_LIBRARY_PATH
。在之后的当前进程中更改 LD_LIBRARY_PATH
的值不起作用(就像这个问题说)。
你有一些选择:
A。如果你知道你需要从 / some / path
需要 xyz.so
,并控制python的执行脚本从一开始,然后简单地设置 LD_LIBRARY_PATH
为您的喜好(在检查它尚未设置后),并重新执行自己。这就是 Java
。
B。您可以在导入 x.so $ c之前,通过绝对路径导入
/some/path/xyz.so
$ C>。当您导入 x.so
时,加载程序将发现它已经加载了 xyz.so
,并将使用已经加载的模块,而不是再次搜索。
C。如果您自己构建 x.so
,可以在其链接中添加 -Wl,-rpath = / some / path
行,然后导入 x.so
将导致加载程序在 / some / path
中查找依赖模块。 / p>
Is it possible to change environment variables of current process?
More specifically in a python script I want to change LD_LIBRARY_PATH
so that on import of a module 'x' which depends on some xyz.so
, xyz.so
is taken from my given path in LD_LIBRARY_PATH
is there any other way to dynamically change path from where library is loaded?
Edit: I think I need to mention that I have already tried thing like os.environ["LD_LIBRARY_PATH"] = mypath os.putenv('LD_LIBRARY_PATH', mypath)
but these modify the env. for spawned sub-process, not the current process, and module loading doesn't consider the new LD_LIBRARY_PATH
Edit2, so question is can we change environment or something so the library loader sees it and loads from there?
The reason
os.environ["LD_LIBRARY_PATH"] = ...
doesn't work is simple: this environment variable controls behavior of the dynamic loader (ld-linux.so.2
on Linux, ld.so.1
on Solaris), but the loader only looks at LD_LIBRARY_PATH
once at process startup. Changing the value of LD_LIBRARY_PATH
in the current process after that point has no effect (just as the answer to this question says).
You do have some options:
A. If you know that you are going to need xyz.so
from /some/path
, and control the execution of python script from the start, then simply set LD_LIBRARY_PATH
to your liking (after checking that it is not already so set), and re-execute yourself. This is what Java
does.
B. You can import /some/path/xyz.so
via its absolute path before importing x.so
. When you then import x.so
, the loader will discover that it has already loaded xyz.so
, and will use the already loaded module instead of searching for it again.
C. If you build x.so
yourself, you can add -Wl,-rpath=/some/path
to its link line, and then importing x.so
will cause the loader to look for dependent modules in /some/path
.
这篇关于更改当前流程环境的LD_LIBRARY_PATH的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!