Python: exec code object within special environment
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?
精彩评论