Is there a way to tell if a script has been run by Pythonw.exe instead of Python.exe?

M

Metalone

In particular I want to know how to tell if reading and writing to the
console can occur.
Something like
sys.isConsolePresent()
 
G

Graham Dumpleton

In particular I want to know how to tell if reading and writing to the
console can occur.
Something like
sys.isConsolePresent()

Have you tried:

sys.stdin.isatty()
sys.stdout.isatty()

Graham
 
M

michaelvmata

In particular I want to know how to tell if reading and writing to the
console can occur.
Something like
sys.isConsolePresent()

Look at sys.executable to find the name of the binary for the Python
interpreter.
 
B

BlueBird

In particular I want to know how to tell if reading and writing to the
console can occur.
Something like
sys.isConsolePresent()

For a different problem, I have the following code. It might help:

def isrealfile(file):
"""
Test if file is on the os filesystem. This is necessary on windows,
when
starting python with pythonw.exe because in that case, the
stdout and stderr
are not real file and will create IOError when being flushed or when
more
than 4096 bytes are written.
"""
if not hasattr(file, 'fileno'): return False
try: tmp = os.dup(file.fileno())
except: return False
else: os.close(tmp); return True

class NullStream:
"""
A file like class that writes nothing
"""
def close(self): pass
def flush(self): pass
def write(self, str): pass
def writelines(self, sequence): pass

if not isrealfile(sys.stdout):
sys.stdout = NullStream()

if not isrealfile(sys.stderr):
sys.stderr = NullStream()
 
M

Metalone

Thanks to all, I learned something in each post.
When using py2exe to build an executable sys.executable does not
provide the name of the python interpreter but the name of the
executable generated by py2exe.
 
T

Thomas Heller

Metalone said:
Thanks to all, I learned something in each post.
When using py2exe to build an executable sys.executable does not
provide the name of the python interpreter but the name of the
executable generated by py2exe.

When running the executable built with py2exe you might be interested
in the variable sys.frozen; they are set to the string 'console' or 'windows', IIRC.

Thomas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top