更改当前进程环境的LD_LIBRARY_PATH

时间:2021-08-17 11:41:51

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

更具体地说,在python脚本中,我想更改LD_LIBRARY_PATH,以便在导入模块'x'时依赖于某些xyz.so,xyz.so取自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)

编辑:我想我需要提一下,我已经尝试过像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

但这些修改了env。对于生成的子进程,而不是当前进程,并且模块加载不考虑新的LD_LIBRARY_PATH

Edit2, so question is can we change environment or something so the library loader sees it and loads from there?

Edit2,所以问题是我们可以改变环境或其他东西,以便库加载器看到它并从那里加载吗?

4 个解决方案

#1


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).

不起作用很简单:这个环境变量控制动态加载器的行为(Linux上的ld-linux.so.2,Solaris上的ld.so.1),但加载程序在进程启动时只查看LD_LIBRARY_PATH一次。在该点之后更改当前进程中LD_LIBRARY_PATH的值无效(正如此问题的答案所示)。

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.

A.如果您知道从/ some / path需要xyz.so,并从头开始控制python脚本的执行,那么只需将LD_LIBRARY_PATH设置为您喜欢的(在检查它之后是否已设置),并重新执行自己。这就是Java所做的。

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.

B.在导入x.so之前,您可以通过其绝对路径导入/some/path/xyz.so.然后当你导入x.so时,加载器会发现它已经加载了xyz.so,并将使用已加载的模块而不是再次搜索它。

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.

C.如果你自己构建x.so,你可以在其链接行添加-Wl,-rpath = / some / path,然后导入x.so将使加载器在/ some / path中查找依赖模块。

#2


Based on the answer from Employed Russian, this is what works for me

基于Employed Russian的答案,这对我有用

oracle_libs = os.environ['ORACLE_HOME']+"/lib/"
rerun = True

if not 'LD_LIBRARY_PATH' in os.environ:
  os.environ['LD_LIBRARY_PATH'] = ":"+oracle_libs
elif not oracle_libs in os.environ.get('LD_LIBRARY_PATH'):
  os.environ['LD_LIBRARY_PATH'] += ":"+oracle_libs
else:
  rerun = False

if rerun:
  os.execve(os.path.realpath(__file__), sys.argv, os.environ)

#3


In my experience trying to change the way the loader works for a running Python is very tricky; probably OS/version dependent; may not work. One work-around that might help in some circumstances is to launch a sub-process that changes the environment parameter using a shell script and then launch a new Python using the shell.

根据我的经验,尝试更改加载程序为正在运行的Python工作的方式非常棘手;可能是OS /版本依赖;可能行不通。在某些情况下可能有用的一种解决方法是启动一个子进程,该子进程使用shell脚本更改环境参数,然后使用shell启动新的Python。

#4


well, the environment variables are stored in the dictionary os.environ, so if you want to change , you can do

好吧,环境变量存储在字典os.environ中,所以如果你想改变,你可以这样做

os.environ["PATH"] = "/usr/bin"

#1


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).

不起作用很简单:这个环境变量控制动态加载器的行为(Linux上的ld-linux.so.2,Solaris上的ld.so.1),但加载程序在进程启动时只查看LD_LIBRARY_PATH一次。在该点之后更改当前进程中LD_LIBRARY_PATH的值无效(正如此问题的答案所示)。

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.

A.如果您知道从/ some / path需要xyz.so,并从头开始控制python脚本的执行,那么只需将LD_LIBRARY_PATH设置为您喜欢的(在检查它之后是否已设置),并重新执行自己。这就是Java所做的。

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.

B.在导入x.so之前,您可以通过其绝对路径导入/some/path/xyz.so.然后当你导入x.so时,加载器会发现它已经加载了xyz.so,并将使用已加载的模块而不是再次搜索它。

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.

C.如果你自己构建x.so,你可以在其链接行添加-Wl,-rpath = / some / path,然后导入x.so将使加载器在/ some / path中查找依赖模块。

#2


Based on the answer from Employed Russian, this is what works for me

基于Employed Russian的答案,这对我有用

oracle_libs = os.environ['ORACLE_HOME']+"/lib/"
rerun = True

if not 'LD_LIBRARY_PATH' in os.environ:
  os.environ['LD_LIBRARY_PATH'] = ":"+oracle_libs
elif not oracle_libs in os.environ.get('LD_LIBRARY_PATH'):
  os.environ['LD_LIBRARY_PATH'] += ":"+oracle_libs
else:
  rerun = False

if rerun:
  os.execve(os.path.realpath(__file__), sys.argv, os.environ)

#3


In my experience trying to change the way the loader works for a running Python is very tricky; probably OS/version dependent; may not work. One work-around that might help in some circumstances is to launch a sub-process that changes the environment parameter using a shell script and then launch a new Python using the shell.

根据我的经验,尝试更改加载程序为正在运行的Python工作的方式非常棘手;可能是OS /版本依赖;可能行不通。在某些情况下可能有用的一种解决方法是启动一个子进程,该子进程使用shell脚本更改环境参数,然后使用shell启动新的Python。

#4


well, the environment variables are stored in the dictionary os.environ, so if you want to change , you can do

好吧,环境变量存储在字典os.environ中,所以如果你想改变,你可以这样做

os.environ["PATH"] = "/usr/bin"