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

Discussion in 'Python' started by Metalone, Oct 18, 2007.

  1. Metalone

    Metalone Guest

    In particular I want to know how to tell if reading and writing to the
    console can occur.
    Something like
    sys.isConsolePresent()
    Metalone, Oct 18, 2007
    #1
    1. Advertising

  2. On Oct 19, 7:56 am, Metalone <> wrote:
    > 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
    Graham Dumpleton, Oct 19, 2007
    #2
    1. Advertising

  3. Metalone

    Guest

    On Oct 18, 2:56 pm, Metalone <> wrote:
    > 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.
    , Oct 19, 2007
    #3
  4. Metalone

    BlueBird Guest

    On Oct 18, 11:56 pm, Metalone <> wrote:
    > 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()
    BlueBird, Oct 19, 2007
    #4
  5. Metalone

    Metalone Guest

    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.
    Metalone, Oct 19, 2007
    #5
  6. Re: Is there a way to tell if a script has been run by Pythonw.exeinstead of Python.exe?

    Metalone schrieb:
    > 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
    Thomas Heller, Oct 19, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Emile van Sebille

    python.exe vs pythonw.exe difference?

    Emile van Sebille, Mar 1, 2004, in forum: Python
    Replies:
    0
    Views:
    1,183
    Emile van Sebille
    Mar 1, 2004
  2. Tim Peters
    Replies:
    0
    Views:
    544
    Tim Peters
    Mar 2, 2004
  3. Thomas Heller

    Re: python.exe vs pythonw.exe difference?

    Thomas Heller, Mar 2, 2004, in forum: Python
    Replies:
    0
    Views:
    410
    Thomas Heller
    Mar 2, 2004
  4. Chaos
    Replies:
    1
    Views:
    403
  5. goldtech
    Replies:
    21
    Views:
    238
Loading...

Share This Page