Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Cameron Simpson Newsgroups: comp.lang.python Subject: Re: Failed to update the os.environ with subprocess.Popen. Date: Sun, 3 Apr 2016 18:20:31 +1000 Lines: 28 Message-ID: References: <57009af2$0$1607$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed X-Trace: news.uni-berlin.de 8kbwysFxN7mQhHCza0URRQeSb2AE+UZitQaoOh0+P/xA== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:skip:s 10': 0.05; 'expression:': 0.09; 'method:': 0.09; 'os.environ': 0.09; 'skip:> 40': 0.09; 'output': 0.13; '2016': 0.16; '>on': 0.16; '>to': 0.16; 'from:addr:cs': 0.16; 'from:addr:zip.com.au': 0.16; 'from:name:cameron simpson': 0.16; 'message-id:@cskk.homeip.net': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'simpson': 0.16; 'wrote:': 0.16; 'attribute': 0.18; 'written,': 0.18; 'object.': 0.22; 'cheers,': 0.22; 'trying': 0.22; 'sets': 0.23; 'header:In-Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; 'updating': 0.27; 'object,': 0.27; 'subject:update': 0.29; 'objects': 0.29; 'code': 0.30; 'error.': 0.31; 'regardless': 0.31; 'problem': 0.33; 'url:python': 0.33; "d'aprano": 0.33; 'particular,': 0.33; 'steven': 0.33; 'skip:> 10': 0.35; 'but': 0.36; 'url:org': 0.36; 'url:library': 0.36; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'charset:us-ascii': 0.37; 'received:localdomain': 0.38; 'subject:the': 0.39; 'to:addr:python.org': 0.40; 'subject:with': 0.40; 'url:3': 0.60; 'your': 0.60; 'here.': 0.62; 'cameron': 0.66 Content-Disposition: inline In-Reply-To: <57009af2$0$1607$c3e8da3$5496439d@news.astraweb.com> User-Agent: Mutt/1.5.24 (2015-08-30) 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:106356 On 03Apr2016 14:24, Steven D'Aprano wrote: >On Sun, 3 Apr 2016 01:29 pm, Hongyi Zhao wrote: >> I use the following code to update the os.environ with subprocess.Popen: > >I don't understand what you are trying to do here. But regardless of your >intention, the problem you have is nothing to do with updating os.environ. >Proof: change the last line from this complicated expression: > >os.environ.update(line.partition('=')[::2] for line in output.split('\0')) >to this simple expression: >output.split('\0') > >and you will get the same error. os.environ is irrelevant. Your problem is >only with Popen. > >What makes you think that Popen objects have a split() method? They are not >documented as having this method: > >https://docs.python.org/2/library/subprocess.html >https://docs.python.org/3/library/subprocess.html >https://pymotw.com/2/subprocess/ In particular, you want the subprocess' output. As written, your code sets "output" to the Popen object. You actually want to set it to the .stdout attribute of that object, which is the output from the subcommand. Cheers, Cameron Simpson