Python - Print on stdout on a "terminal"

Go To


Before starting, I ask you all to apologize for the question. Maybe it is stupid, but I cannot find a solution. I am working on a remote machine, and have no idea what type.

My python code, that seems to work, is the one below. The problem is that I am trying to print some outputs on the screen but nothing happens. I have tried both print and raw_input but nothing happens ... Do you know any other way to do it ?

# Set up fields of reply message based on query
def prepareReply():
    global authorReply, authorReplyLen, localConvId, originConvId, blbContentAndUntUnz, linkName

    print "PLOP!"

    #print "="*10

Thanks !

2009-06-16 09:11
by wheisenberg
Print prints to standard out - the problem must be somewhere else. Which python version are you using - Joris Timmermans 2009-06-16 09:15
2.4 Is it possible that the stdout is redidercted somewhere else by default ? - wheisenberg 2009-06-16 09:19
It is possible to redirect stdout, but I don't think that's done by default. I've used redirecting for logging to file in circumstances where I didn't have access to stdout - Joris Timmermans 2009-06-16 09:20
So it can be possible to don't have access to stdout.. - wheisenberg 2009-06-16 09:22
did you tried to print anything in python interpreter - uolot 2009-06-16 09:23
with python interpreter the print is working fine ! I think that the shell script is redirecting the output somewhere else ... I solved the problem printing on files - wheisenberg 2009-06-16 09:45


To redirect stdout to something that you can read, a file in this case:

class PyLogger:

  def __init__(self, source):
    self.file_handle = open('Python_Log.txt', 'a')
    self.buf = []

  def write(self, data):
    if data.endswith('\n'):
      self.file_handle = open('Python_Log.txt', 'a')
      self.file_handle.write('\t' * indent_level)
      self.file_handle.write(self.source + "::" + ''.join(self.buf))
      self.buf = []

  def __del__(self):
    if self.buf != []:
      self.file_handle = open('Python_Log.txt', 'a')
      self.file_handle.write('\t' * indent_level)
      self.file_handle.write(self.source + "::" + ''.join(self.buf) + '\n')

import sys
sys.stdout = PyLogger('stdout')
sys.stderr = PyLogger('stderr')
2009-06-16 09:27
by Joris Timmermans
Looks like the first open() and close() in write() and del() are superfluous. Or did you want to do self.file_handle.flush()?

Plus, file_handle could really be a class attribute instead of an instance attribute, since it is shared by all instances - Eric O Lebigot 2009-06-16 12:37

It's a simple copy-paste of some "code that works" that I haven't thought about in ages, probably originally off the internet somewhere. You could well be right that much of the code is superfluous - Joris Timmermans 2009-06-16 12:59


import sys
print "Hi!"
2009-06-16 09:32
by grawity


This is a wild guess, but looking at the wording in your comments indicates that it might be a web server application (hint: more detail on your environment would be helpful). In this case, stdout is probably going somewhere else, at least not to the browser.

How are you actually running that code? Do you type "python" at a shell prompt, or do you hit Reload in your browser?

2009-06-16 09:22
by Greg Hewgill
the app is running on a server, but not a web server ! It's a particular server processing the transactions... To run the python program I have to run a shell script that runs my python application ! I have no idea ho the shell script is done .. - wheisenberg 2009-06-16 09:28
Well, it looks like you're already doing what can be expected from a Python point of view. Since your script is run through some other framework that none of us know anything about, stdout could be redirected anywhere. One thing you could try is: out = open("/dev/tty", "w"); print >>out, "hello world - Greg Hewgill 2009-06-16 09:34
If it runs by some framwework, maybe return "PLOP!" will work - uolot 2009-06-16 09:35