Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Ben Finney Newsgroups: comp.lang.python Subject: Re: Calling the source command from subprocess.popen to update the os.environ. Date: Mon, 28 Mar 2016 00:59:58 +1100 Lines: 45 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Trace: news.uni-berlin.de IdutznbmrYCubcokkjVrWQWu/rnh3mE02V51ZTa1H94g== Cancel-Lock: sha1:GybMRw0VmUKIegox9ylTog7d27s= Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'subject:skip:s 10': 0.05; 'api': 0.09; 'inherited': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:command': 0.09; 'variables,': 0.09; 'python': 0.10; 'output': 0.13; 'variables': 0.15; "'=',": 0.16; "'\\0'": 0.16; '8bit%:32': 0.16; 'ends,': 0.16; 'optionally': 0.16; 'processes.': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'subprocess': 0.16; 'changes': 0.20; 'import': 0.24; 'unix': 0.24; 'script': 0.25; 'header:User- Agent:1': 0.26; "doesn't": 0.26; 'header:X-Complaints-To:1': 0.26; 'change,': 0.27; 'be:': 0.29; 'subject:update': 0.29; 'environment': 0.29; 'code': 0.30; 'point': 0.33; 'changed': 0.33; 'file': 0.34; 'child': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'starting': 0.37; 'things': 0.38; 'subject:from': 0.39; 'subject:the': 0.39; 'skip:e 20': 0.39; 'to:addr:python.org': 0.40; 'some': 0.40; 'within': 0.64; 'skip:\xe2 10': 0.70; 'design.': 0.72; '_o__)': 0.84; 'or:': 0.84; 'received:125': 0.84; 'skip:/ 30': 0.84; 'subject:source': 0.84; '(running': 0.91; 'belief': 0.91; 'speaks': 0.91 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: jigong.madmonks.org X-Public-Key-ID: 0xAC128405 X-Public-Key-Fingerprint: 517C F14B B2F3 98B0 CB35 4855 B8B2 4C06 AC12 8405 X-Public-Key-URL: http://www.benfinney.id.au/contact/bfinney-pubkey.asc X-Post-From: Ben Finney User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.4 (gnu/linux) X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:105841 Hongyi Zhao 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