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


Groups > comp.lang.python > #105849

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

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Subject Re: Calling the source command from subprocess.popen to update the os.environ.
Date Mon, 28 Mar 2016 01:34:06 +1100
Lines 46
Message-ID <mailman.87.1459090729.28225.python-list@python.org> (permalink)
References <nd8mtk$hhb$1@aspen.stu.neva.ru>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de 89odJm+DidcQUDjwHX1EFwWGQUQQiX/iDYHOjZ7CZxnw==
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'received:209.85.223': 0.03; 'skip:" 60': 0.05; 'subject:skip:s 10': 0.05; 'raises': 0.07; 'cc:addr:python-list': 0.09; '22,': 0.09; 'errno': 0.09; 'oserror': 0.09; 'script,': 0.09; 'subject:command': 0.09; 'will,': 0.09; 'bug': 0.10; 'python.': 0.11; 'argument': 0.15; 'properly': 0.15; 'things.': 0.15; '2016': 0.16; 'bash,': 0.16; 'did.': 0.16; 'does;': 0.16; 'env,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'oserror:': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'unset': 0.16; 'worst': 0.16; 'wrote:': 0.16; 'debugging': 0.18; 'skip:` 20': 0.18; 'try:': 0.18; 'variable': 0.18; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'skip:" 40': 0.20; '"",': 0.22; 'assuming': 0.22; 'exec': 0.22; 'am,': 0.23; '(most': 0.24; 'header:In-Reply-To:1': 0.24; 'mon,': 0.24; 'script': 0.25; 'message-id:@mail.gmail.com': 0.27; 'var': 0.27; 'subject:update': 0.29; 'environment': 0.29; "i'm": 0.30; 'code': 0.30; "i'd": 0.31; 'error.': 0.31; 'probably': 0.31; "can't": 0.32; 'maybe': 0.33; 'source': 0.33; 'environment,': 0.33; 'traceback': 0.33; 'case,': 0.34; 'file': 0.34; 'except': 0.34; 'received:google.com': 0.35; 'could': 0.35; 'clear': 0.35; 'replace': 0.35; 'problem.': 0.35; 'but': 0.36; 'skip:i 20': 0.36; 'should': 0.36; 'received:209.85': 0.36; 'possible': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'expect': 0.37; 'received:209': 0.38; 'stuff': 0.38; 'anything': 0.38; 'why': 0.39; 'sure': 0.39; 'whatever': 0.39; 'subject:from': 0.39; 'subject:the': 0.39; 'some': 0.40; 'your': 0.60; "you'll": 0.61; 'skip:u 10': 0.61; 'back': 0.62; 'more': 0.63; 'mar': 0.65; 'special': 0.73; 'case?': 0.84; 'chrisa': 0.84; 'first?': 0.84; 'subject:source': 0.84; 'to:none': 0.91; 'sourcing': 0.93
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc; bh=m3FgpMPVUJf69X+fhDfokSZ8elhxzekU8iV3MJdOo+I=; b=d/SyBIL+JRpUFTtN97pqswskKfi1EOsii2kP1DZxyn6Q6v5aSDuo5is0c273/yLvOv lS/oPz+4EQ4CoYt5QnqRgpjfoXd60Ik1/pBtrsZqu2UzUWoUvNyNfiKTay7xhdG/wOEI Aql7cqTmbFwXCaYcWqSge5Tr+zXCnHeiVCreywJA0v6qggeBWAoW3ANU8kh7fnRQ7HRQ g5FVxEQ80dBYiv8wFK/FjCKMYHgTFcjY/mF0RuL0wLYGh7AR/4S3B00the/B+07wCGGf RdT/pWeoHUGMZBnK5A2zDKNpoufN+m4X+PSks98Svc6WQu1xk3JZYWla6h70pAq2xMMy M3AA==
X-Google-DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:cc; bh=m3FgpMPVUJf69X+fhDfokSZ8elhxzekU8iV3MJdOo+I=; b=eQQBm0mep0qs1PswmV2ag3KQUTQeIzC4zh83WlPvV5atKz/cEU9hL46JjDWmMrJCEs ZKq29gv8yEJc2ga+jrepSWxoLzGh4vTG/sxipEWp2r2QJo+Y7aRP42JeUZRZo8Y7qj8Z DPHA99rozovOBBkGFx0Fa3WKqy7BMttvD8BS38myfbK2gzXFQopA90Ax72Ksn2W0Giek AeF1IRb7n1c6O4HNlSq0Q1fCcTjbtm/gWZYMMYBOs9zCaOa6n6XNRfECHz2r4IC1RlYL +bJvCECywqXDcdvOukWmFy4p/VRyeCm2HbFsseRmh3Dd4QoW7JTqlUWDN0SMhEO4ajD8 qKZA==
X-Gm-Message-State AD7BkJLPHGsA67L/W5541xvIa0cOmLjUKq6oyVDItMwWGxOcE+8koGrks0DuoB3rsuUWnXzkdSlQE1I+F4+1lA==
X-Received by 10.107.169.105 with SMTP id s102mr20449503ioe.19.1459089246241; Sun, 27 Mar 2016 07:34:06 -0700 (PDT)
In-Reply-To <nd8mtk$hhb$1@aspen.stu.neva.ru>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.21
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:105849

Show key headers only | View raw


On Mon, Mar 28, 2016 at 12:24 AM, Hongyi Zhao <hongyi.zhao@gmail.com> wrote:
> # 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.

Do you need to clear the environment first? Anything that's been
overwritten will replace stuff in os.environ, so the only reason to
clear it would be if you expect your env script to unset things. Is
that the case?

I'm not sure why the clear is a problem. Here's how I'd start debugging it:

for var in os.environ:
    try: del os.environ[var]
    except OSError: print(var)

Two possibilities: Either that raises OSError errno 22, same as
clear() does; or it doesn't. If it doesn't, you should have a clear
env, and all should be working (if a little odd) - and then you can
look into the possible bug with clear(). But more likely it will, and
then you'll be able to see exactly which key triggered the error.
Check out what that key is; maybe there's some kind of special
environment variable that can't be cleared??

Worst case, you can always do this "clear whatever you can", and then
do your environ.update(). Assuming you never need to clear one of the
unclearables, this'll give you the same env update as sourcing the
script did.

And worst worst case, you could probably exec to bash, source the
script, and exec back into Python. That should properly update the
environment, but it's a somewhat hamfisted way to go about it :)

ChrisA

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