Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #105837
| From | Hongyi Zhao <hongyi.zhao@gmail.com> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Calling the source command from subprocess.popen to update the os.environ. |
| Date | 2016-03-27 13:24 +0000 |
| Organization | NPO RUSnet InterNetNews site |
| Message-ID | <nd8mtk$hhb$1@aspen.stu.neva.ru> (permalink) |
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 :.
Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll thread
Calling the source command from subprocess.popen to update the os.environ. Hongyi Zhao <hongyi.zhao@gmail.com> - 2016-03-27 13:24 +0000 Re: Calling the source command from subprocess.popen to update the os.environ. Ben Finney <ben+python@benfinney.id.au> - 2016-03-28 00:59 +1100 Re: Calling the source command from subprocess.popen to update the os.environ. Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2016-03-27 15:15 +0100 Re: Calling the source command from subprocess.popen to update the os.environ. Chris Angelico <rosuav@gmail.com> - 2016-03-28 01:34 +1100 Re: Calling the source command from subprocess.popen to update the os.environ. Hongyi Zhao <hongyi.zhao@gmail.com> - 2016-03-27 23:40 +0000
csiph-web