Calling the source command from subprocess.popen to update the os.environ.
Hongyi Zhao
hongyi.zhao at gmail.com
Sun Mar 27 09:24:05 EDT 2016
Hi all,
Based on the methods here:
http://stackoverflow.com/questions/7040592/calling-the-source-command-
from-subprocess-popen/18897007#comment30954741_12708396
I use the following code the update the os.environ:
import os
from subprocess import check_output
# POSIX: name shall not contain '=', value doesn't contain '\0'
output = check_output("source /home/werner/env-intel-toolchains.sh;
env -0", shell=True, executable="/bin/bash")
# replace env
os.environ.clear()
os.environ.update(line.partition('=')[::2] for line in output.split('\0'))
In the above code, the file /home/werner/env-intel-toolchains.sh will be
sourced and its settings should be combined into the current os.environ
of python.
But, I find that the above code will fail on the `os.environ.clear()'
command as follows:
In[24]: import os
from subprocess import check_output
# POSIX: name shall not contain '=', value doesn't contain '\0'
output = check_output("source /home/werner/env-intel-toolchains.sh; env
-0", shell=True,
executable="/bin/bash")
# replace env
os.environ.clear()
os.environ.update(line.partition('=')[::2] for line in output.split('\0'))
Traceback (most recent call last):
File "/home/werner/anaconda2/lib/python2.7/site-packages/IPython/core/
interactiveshell.py", line 3066, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-24-816c472b62f2>", line 10, in <module>
os.environ.clear()
File "/home/werner/anaconda2/lib/python2.7/os.py", line 501, in clear
unsetenv(key)
OSError: [Errno 22] Invalid argument
If I don't use the `os.environ.clear()' code line, it will work smoothly.
Any hints?
Regards
--
.: Hongyi Zhao [ hongyi.zhao AT gmail.com ] Free as in Freedom :.
More information about the Python-list
mailing list