Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #106415
| From | Cameron Simpson <cs@zip.com.au> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: Failed to update the os.environ with subprocess.Popen. |
| Date | 2016-04-04 07:49 +1000 |
| Message-ID | <mailman.417.1459720185.28225.python-list@python.org> (permalink) |
| References | <ndquho$62b$1@aspen.stu.neva.ru> |
On 03Apr2016 11:24, Hongyi Zhao <hongyi.zhao@gmail.com> wrote:
>On Sun, 03 Apr 2016 18:20:31 +1000, Cameron Simpson wrote:
>
>> 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.
>
>Based on your above hints, I try to use the following codes:
>
>output = Popen("""
>/bin/bash <<EOF
>source ~/.profile.d/environment-module-systems/modules.sh
>export PATH=$MODULESHOME/../default/bin:$PATH
>module add intel/parallel_studio_xe_2015
>env -0
>EOF
>""", shell=True).stdout
>
>But I meet the following error:
>
>Traceback (most recent call last):
> File "/home/werner/software/hpc/dft-to-study/jasp/jasp.git/jasp/bin/
>runjasp.py", line 138, in <module>
> os.environ.update(line.partition('=')[::2] for line in output.split
>('\0'))
>AttributeError: 'NoneType' object has no attribute 'split'
Please have a close read of the subprocess docs as Steven recommended.
You're calling:
output.split('\0')
The message above indicates that "output" is None. This is probably because you
have not asked to attach to the command's output in your Popen() call, and
therefore stdout is not attached to a pipe, so it is None.
Also note that even when it is attached to a pipe, .stdout is a _file_. So you
need to read from it to get a string to split.
>While the following code will work smoothly:
>
>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]
>
>Any hints on this issue?
This is because (a) you specified stdout=PIPE here, which you did not do
earlier and (b) because .communicate reads the output for you and gives you a
string.
Cheers,
Cameron Simpson <cs@zip.com.au>
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread
Failed to update the os.environ with subprocess.Popen. Hongyi Zhao <hongyi.zhao@gmail.com> - 2016-04-03 03:29 +0000
Re: Failed to update the os.environ with subprocess.Popen. Steven D'Aprano <steve@pearwood.info> - 2016-04-03 14:24 +1000
Re: Failed to update the os.environ with subprocess.Popen. Cameron Simpson <cs@zip.com.au> - 2016-04-03 18:20 +1000
Re: Failed to update the os.environ with subprocess.Popen. Hongyi Zhao <hongyi.zhao@gmail.com> - 2016-04-03 11:24 +0000
Re: Failed to update the os.environ with subprocess.Popen. Cameron Simpson <cs@zip.com.au> - 2016-04-04 07:49 +1000
csiph-web