Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #105841

Re: Calling the source command from subprocess.popen to update the os.environ.

From Ben Finney <ben+python@benfinney.id.au>
Newsgroups comp.lang.python
Subject Re: Calling the source command from subprocess.popen to update the os.environ.
Date 2016-03-28 00:59 +1100
Message-ID <mailman.82.1459087223.28225.python-list@python.org> (permalink)
References <nd8mtk$hhb$1@aspen.stu.neva.ru>

Show all headers | View raw


Hongyi Zhao <hongyi.zhao@gmail.com> writes:

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

That will start a new process (running ‘/bin/bash’), then execute some
commands from a script file in that process.

When that new process ends, any changes in its environment also
disappear.

At no point do changes to that new process's environment have any effect
on the Python process.

A Unix process is *completely unable* to change the environment
variables of its parent. It can change its own variables, and optionally
those of its child processes.

This is by design. It's a good thing.

So if you want the Python process's environment to change, then it needs
to be:

* Inherited from an *already* changed environment when it starts.

Or:

* Changed within the Python process, by the Python API for that purpose
  (‘os.environ’).

You will not be able to change a Python process environment by starting
new processes.

-- 
 \       “Faith, n. Belief without evidence in what is told by one who |
  `\   speaks without knowledge, of things without parallel.” —Ambrose |
_o__)                           Bierce, _The Devil's Dictionary_, 1906 |
Ben Finney

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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