In this Stack Overflow question I asked about creating a Python program of mine which was meant to allow me to access the terminal from a Python shell. It did not work... at least when running the program through startx. But, interestingly enough, when I ran it through ssh (my Pi was running in console mode) the program ran like a charm. The code is below:

import subprocess
import socket
import sys
import platform
import os

print "WELCOME TO xTROL. \n"

pyVER = sys.version
OS = platform.system()

print pyVER, "\n"
print "Running on %s" % (OS)
while True:
    INPUT = raw_input(">>>")
    cmd_FORMAT = INPUT.split()

        output = subprocess.Popen(cmd_FORMAT, stdout=subprocess.PIPE).communicate()[0]
        print output
    except Exception:
        print "Something went wrong."

Why is this?


By did not work, I mean that the program did not work interactively. Writing Python in the startx version would return a blank line, in console mode I entered the interactive prompt.

I ran the startx Python program by double clicking on the item, opening idle, and then executing the program.

  • Terminal emulation is a complex affair. What you're trying can change a lot depending on which commands you run. Some require real terminals, others are happy with pipes, some change their behaviour depending on whether they detect a terminal or not.
    – KayEss
    Commented Apr 1, 2013 at 7:04
  • Note: by "did not work", I mean that the program was not interactive. (Typing Python opened an interactive shell over ssh, but did nothing on startx) Commented Apr 1, 2013 at 11:23
  • Can you elaborate what you meant by "running the program through startx"? Did you open a terminal first? Did you run it via .xinitrc?
    – Arne
    Commented Apr 1, 2013 at 14:20
  • @Arne I added the edits Commented Apr 1, 2013 at 16:30
  • 2
    Instead of running it from idle you should open a terminal (under X) and then run it from that in the same way that you did under ssh. You may well find that it is then interactive. I don't know if idle connects to a real terminal or not, but if not that would be the root cause of your problem -- you're not actually doing proper terminal emulation.
    – KayEss
    Commented Apr 2, 2013 at 2:52

1 Answer 1


A terminal emulator needs to deal with three streams: stdin for input, stdout for normal display and stderr for error output. Your program only captures stdout and relies on the terminal emulator you're running it from to handle the others for you.

What the right thing for you to do is dependant on what you're trying to achieve. If you want to write your own version of something like SSH then you should create a pseudo terminal and then connect the remote program to that with you sending commands to it. I.e. you write to the ptty which then turns up on stdin and you read from it to capture the program's stdout and stderr.

This page will tell you more than you ever wanted to know about this. If you can understand what it's telling you I think you can work out what strategy to pursue. http://www.linusakesson.net/programming/tty/index.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.