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


Groups > comp.lang.python > #106338 > unrolled thread

Failed to update the os.environ with subprocess.Popen.

Started byHongyi Zhao <hongyi.zhao@gmail.com>
First post2016-04-03 03:28 +0000
Last post2016-04-03 04:00 +0000
Articles 3 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  Failed to update the os.environ with subprocess.Popen. Hongyi Zhao <hongyi.zhao@gmail.com> - 2016-04-03 03:28 +0000
    Get the output from a Popen instance (was: Failed to update the os.environ with subprocess.Popen.) Ben Finney <ben+python@benfinney.id.au> - 2016-04-03 13:37 +1000
      Re: Get the output from a Popen instance (was: Failed to update the os.environ with subprocess.Popen.) Hongyi Zhao <hongyi.zhao@gmail.com> - 2016-04-03 04:00 +0000

#106338 — Failed to update the os.environ with subprocess.Popen.

FromHongyi Zhao <hongyi.zhao@gmail.com>
Date2016-04-03 03:28 +0000
SubjectFailed to update the os.environ with subprocess.Popen.
Message-ID<ndq2kn$h77$1@aspen.stu.neva.ru>
Hi all,

I use the following code to update the os.environ with subprocess.Popen:

-------------
from subprocess import Popen

output = Popen("""
/bin/bash <<EOF
source ~/.profile.d/modules/modules.sh
export PATH=$MODULESHOME/../default/bin:$PATH
module add intel/parallel_studio_xe_2015
env -0
EOF
""", shell=True)

if "" in os.environ.data: del os.environ.data[""]
os.environ.clear()
os.environ.update(line.partition('=')[::2] for line in output.split('\0'))
-------------

But, I meet the following errors:

------------
Traceback (most recent call last):
  File "/home/werner/software/hpc/dft-to-study/jasp/jasp.git/jasp/bin/
runjasp.py", line 125, in <module>
    os.environ.update(line.partition('=')[::2] for line in output.split
('\0'))
AttributeError: 'Popen' object has no attribute 'split'
-------------------

How to solve this issue?
-- 
.: Hongyi Zhao [ hongyi.zhao AT gmail.com ] Free as in Freedom :.

[toc] | [next] | [standalone]


#106341 — Get the output from a Popen instance (was: Failed to update the os.environ with subprocess.Popen.)

FromBen Finney <ben+python@benfinney.id.au>
Date2016-04-03 13:37 +1000
SubjectGet the output from a Popen instance (was: Failed to update the os.environ with subprocess.Popen.)
Message-ID<mailman.388.1459654659.28225.python-list@python.org>
In reply to#106338
Hongyi Zhao <hongyi.zhao@gmail.com> writes:

> I use the following code to update the os.environ with
> subprocess.Popen

Again, it is quite misleading to describe what you are doing as “update
the os.environ with subprocess.Popen”.

The ‘subprocess.Popen’ call *cannot* upsdate the Python process's
‘os.environ’.

What you're doing is two quite separate steps:

* Obtain a collection of items (key → value pairs).

* Update a dictionary.

Please be clear that's what you're doing because “update ‘os.environ’
with ‘subprocess.Popen’ is *not* that.

> But, I meet the following errors:
>
> ------------
> Traceback (most recent call last):
>   File "/home/werner/software/hpc/dft-to-study/jasp/jasp.git/jasp/bin/
> runjasp.py", line 125, in <module>
>     os.environ.update(line.partition('=')[::2] for line in output.split
> ('\0'))
> AttributeError: 'Popen' object has no attribute 'split'
> -------------------

So your problem is nothing to do with “update ‘os.environ’”. I have
updated the Subject field accordingly.

The problem you're encountering is only to do with ‘subprocess.Popen’.
That should make it much easier to search for the documentation to
understand the problem.

<URL:https://docs.python.org/3/library/subprocess.html#subprocess.Popen.stdout>

-- 
 \     “When people believe that they have absolute knowledge, with no |
  `\     test in reality, this [the Auschwitz crematorium] is how they |
_o__)             behave.” —Jacob Bronowski, _The Ascent of Man_, 1973 |
Ben Finney

[toc] | [prev] | [next] | [standalone]


#106342 — Re: Get the output from a Popen instance (was: Failed to update the os.environ with subprocess.Popen.)

FromHongyi Zhao <hongyi.zhao@gmail.com>
Date2016-04-03 04:00 +0000
SubjectRe: Get the output from a Popen instance (was: Failed to update the os.environ with subprocess.Popen.)
Message-ID<ndq4hl$ia8$1@aspen.stu.neva.ru>
In reply to#106341
On Sun, 03 Apr 2016 13:37:24 +1000, Ben Finney wrote:

>> But, I meet the following errors:
>>
>> ------------
>> Traceback (most recent call last):
>>   File "/home/werner/software/hpc/dft-to-study/jasp/jasp.git/jasp/bin/
>> runjasp.py", line 125, in <module>
>>     os.environ.update(line.partition('=')[::2] for line in output.split
>> ('\0'))
>> AttributeError: 'Popen' object has no attribute 'split'
>> -------------------
> 
> So your problem is nothing to do with “update ‘os.environ’”. I have
> updated the Subject field accordingly.
> 
> The problem you're encountering is only to do with ‘subprocess.Popen’.
> That should make it much easier to search for the documentation to
> understand the problem.
> 
> <URL:https://docs.python.org/3/library/
subprocess.html#subprocess.Popen.stdout>

Thanks a lot, got it. Should be used as follows:

output = Popen("""
/bin/bash <<EOF
source ~/.profile.d/modules/modules.sh
export PATH=$MODULESHOME/../default/bin:$PATH
module add intel/parallel_studio_xe_2015
env -0
EOF
""", shell=True,  stdout=PIPE).communicate()[0]

Regards
-- 
.: Hongyi Zhao [ hongyi.zhao AT gmail.com ] Free as in Freedom :.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web