开发者

Python: exec code object within special environment

开发者 https://www.devze.com 2023-03-08 16:50 出处:网络
here is a code that I wanna change LD_LIBRARY_PATH to exec a code:开发者_Go百科 #!/usr/bin/python

here is a code that I wanna change LD_LIBRARY_PATH to exec a code:开发者_Go百科

#!/usr/bin/python
import os

code='''
import os
print os.getenv("LD_LIBRARY_PATH");
import wrap
'''

os.environ['LD_LIBRARY_PATH'] = '/home/dma/python'
os.environ["PYTHONPATH"] = '/home/dma/python'

exec code

The output is:

/home/dma/python
Traceback (most recent call last):
  File "./test.py", line 13, in ?
    exec code
  File "<string>", line 4, in ?
ImportError: example.so: cannot open shared object file: No such file or directory

NOTE: test.py depend on wrap.so, wrap.so depend on example.so

Is there any way to execute the code object in the special environment?

Thanks Dma


It appears as though example.so isn't in /home/dma/python.


I use popen2 to resolve this problem. It works, anyone has a better way?

0

精彩评论

暂无评论...
验证码 换一张
取 消