[Tutor] Apparent incosistency with Python interperter in IDLE

Luke Paireepinart rabidpoobear at gmail.com
Thu Jun 18 08:16:48 CEST 2009

Karen Palen wrote:
> Which appears to be merely the return code, not the stdout!
> It looks like I need to set some variable in IDLE, but I can't figure out exactly what is needed here.
> Can anyone point me to an answer?

Well, Karen, according to my IDLE (admittedly it's 2.6 not 3.0 so it may 
not agree), call only returns the return code.
You can check on your system by doing the following:

IDLE 2.6.2     
 >>> import subprocess
 >>> help(subprocess.call)

which will print out the docstring for the method:

Help on function call in module subprocess:

call(*popenargs, **kwargs)
    Run command with arguments.  Wait for command to complete, then
    return the returncode attribute.
    The arguments are the same as for the Popen constructor.  Example:
    retcode = call(["ls", "-l"])

I believe what's happening is that you are confused about the presence 

In a normal console window, when you call a program (such as "ls"), 
stdout is directed at the screen (the console window.)
So what's happening in the first example (the non-idle one) is:

open subprocess and execute an "ls" call
    - the subprocess prints out the result of the "ls" call
    -subprocess finishes
process finishes

Now the key here is that in this case, both of these have the same 
stdout (the screen), which is the default stdout.

However, when you run from an IDLE prompt, your stdout is not going to 
be the default.
check it out if you want:

 >>> import sys
 >>> print sys.stdout
<idlelib.rpc.RPCProxy object at 0x01198F90>

Notice that this is not a standard stdout.
In a normal Python window,

 >>> import sys
 >>> print sys.stdout
<open file '<stdout>', mode 'w' at 0x00A43070>

it's not the same stdout as IDLE has.

So what's happening when you run the command from IDLE:
open subprocess and execute an "ls" call
    -subprocess starts, executes "ls", prints result to _its_ stdout 
(which is not IDLE's stdout, remember!)
    -subprocess executes, output goes bye bye
your process ends as well

So the problem is that the stdout of the "ls" command is appearing in 
some location that you cannot see.
As for ways to remedy this - I don't know.  The idea here, though, is 
that even though the regular Python version has the side-effect that it 
outputs it in the console, that's not necessarily what you want it to 
do.  The reason is that you have no way to access that data.

Also you mentioned that IDLE prints out a 0.  This is only something 
that IDLE's interpreter does.
For example
 >>> x = "hello"
 >>> x

IDLE echoes the value of x.  However, if you had the code
x = "hello"

and you ran it (not in IDLE's interpreter) you would not get any output.

What you should learn from this is
1- IDLE is weird in many ways
2 - that subprocess.call() is not meant to get data back from a call 
(look at subprocess.Popen and its communicate() method for that)
3 - sometimes things that appear to be outputs are 
side-effects/artifacts of the environment you're running them in and 
can't be depended upon.


More information about the Tutor mailing list