How do I get the return value from a process run by subprocess.Popen?
Question: I am trying to assign the return value of `python --version` to a variable
using the code below.:

    
    
    #! /usr/bin/env python
    
    import os
    import sys
    import subprocess
    os.system('pwd')
    print ("check the version of python")
    output = subprocess.Popen(["python --version"], stdout=subprocess.PIPE, shell=True)
    (ver, error)= output.communicate()
    print "python is:",ver
    

The output I'm getting is :

    
    
    /home/mobaxterm/Desktop/mypy
    check the version of python
    Python 2.7.10
    python is:
    

Please let me know what I am missing.

Thanks in advance.


Answer: Python writes its version information to `stderr` (variable `error`, in your
code) - like most other products.

Nothing was written to `stdout`. You will notice that the variable `ver`
**was** printed, it was an empty string.

You also failed to direct `stderr`:

    
    
    output = subprocess.Popen(["python --version"], stdout=subprocess.PIPE, 
                 stderr=subprocess.PIPE, shell=True)
    

