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

Cameron Simpson cs at zip.com.au
Sun Apr 3 17:49:41 EDT 2016


On 03Apr2016 11:24, Hongyi Zhao <hongyi.zhao at 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 at zip.com.au>



More information about the Python-list mailing list