How do I tell if I'm running in the PyWin interpreter?

Discussion in 'Python' started by Charles Krug, Jan 28, 2006.

  1. Charles Krug

    Charles Krug Guest

    Here's the deal:

    I've a dead-simple command-line program I'm using to test things that I
    can't (for various reasons) test in the IDE.

    Here's a do-nothing subset that shows the idea:

    # insanely simply command interpreter
    import Commands
    import sys

    myPrompt = '$> '

    # Raw Input doesn't QUITE do what I want in Python Win.
    while True:
    try:
    args = raw_input(myPrompt).strip().split()
    except EOFError:
    break

    cmd = args[0]
    print '>%s<' % cmd
    print args

    As the comment says, when I run this under Python Win, I get an (pretty
    sure) Tkinter interface, not a command line, and I don't get my
    EOFError when I expect to.

    This is something I occasionally need in my Swiss Army Knife. Not
    often, but when I need something like this, I need something like THIS
    pretty badly, and sometimes I need to run it under PyWin (and under
    Linux, Unix, Solaris, and anything else you might name and a few things
    I bet you couldn't).

    Is there a way to detect that I'm running the the PyWin interpreter so
    that I can bypass its raw_input behavior?

    Is there a simpler way to do this?

    I recall some sample code that did something very much like this (define
    a small set of callbacks and execute them from a command-like interface)
    but I can't seem to lay my hands on the example.

    Thanx


    Charles
    Charles Krug, Jan 28, 2006
    #1
    1. Advertising

  2. On Fri, 27 Jan 2006 21:05:43 -0600, Charles Krug wrote:

    > Here's the deal:
    >
    > I've a dead-simple command-line program I'm using to test things that I
    > can't (for various reasons) test in the IDE.
    >
    > Here's a do-nothing subset that shows the idea:
    >
    > # insanely simply command interpreter
    > import Commands
    > import sys
    >
    > myPrompt = '$> '
    >
    > # Raw Input doesn't QUITE do what I want in Python Win.
    > while True:
    > try:
    > args = raw_input(myPrompt).strip().split()
    > except EOFError:
    > break
    >
    > cmd = args[0]
    > print '>%s<' % cmd
    > print args
    >
    > As the comment says, when I run this under Python Win, I get an (pretty
    > sure) Tkinter interface, not a command line, and I don't get my
    > EOFError when I expect to.


    When do you expect to get an EOFError? The only way I get an EOFError is
    if I explicitly hit Ctrl-D while raw_input is running. When do you expect
    to get it? Have you tried Ctrl-Z under Windows?



    --
    Steven.
    Steven D'Aprano, Jan 28, 2006
    #2
    1. Advertising

  3. Charles Krug

    Charles Krug Guest

    On 2006-01-28, Steven D'Aprano <> wrote:
    >>
    >> As the comment says, when I run this under Python Win, I get an (pretty
    >> sure) Tkinter interface, not a command line, and I don't get my
    >> EOFError when I expect to.

    >
    > When do you expect to get an EOFError? The only way I get an EOFError is
    > if I explicitly hit Ctrl-D while raw_input is running. When do you expect
    > to get it? Have you tried Ctrl-Z under Windows?
    >


    That's exactly how I use it everywhere else. Type until you're done
    then hit <Ctrl-D>

    The problem is only when running under the PyWin IDE . . I'd been using
    this for months under Idle and every place else I needed it.

    The problem is that PyWin doesn't give you a raw command line with in
    response to raw_input, but gives you a text entry box and a nice
    OK-Cancel yada yada interface that silently eats my EOF.

    I'd like to have a single tool I can use everywhere. So far as I can
    tell, that means I have to detect the PyWin IDE and handle it
    separately on initialization so I get a real raw input and not the
    redefined Tkinter version.
    Charles Krug, Jan 28, 2006
    #3
  4. Charles Krug

    Peter Otten Guest

    Charles Krug wrote:

    > Is there a way to detect that I'm running the the PyWin interpreter so
    > that I can bypass its raw_input behavior?


    You could test

    if pywin_specific_module in sys.modules:
    # use workaraound

    Or maybe you can get away with always using sys.stdin.readline() instead of
    raw_input()? Look into cmd.py for an example.

    Peter
    Peter Otten, Jan 28, 2006
    #4
  5. "Charles Krug" <> schrieb im Newsbeitrag
    news:...
    | On 2006-01-28, Steven D'Aprano <> wrote:
    | >>
    | >> As the comment says, when I run this under Python Win, I get an (pretty
    | >> sure) Tkinter interface, not a command line, and I don't get my
    | >> EOFError when I expect to.
    | >
    | > When do you expect to get an EOFError? The only way I get an EOFError is
    | > if I explicitly hit Ctrl-D while raw_input is running. When do you
    expect
    | > to get it? Have you tried Ctrl-Z under Windows?
    | >
    |
    | That's exactly how I use it everywhere else. Type until you're done
    | then hit <Ctrl-D>
    |
    | The problem is only when running under the PyWin IDE . . I'd been using
    | this for months under Idle and every place else I needed it.
    |
    | The problem is that PyWin doesn't give you a raw command line with in
    | response to raw_input, but gives you a text entry box and a nice
    | OK-Cancel yada yada interface that silently eats my EOF.
    |
    | I'd like to have a single tool I can use everywhere. So far as I can
    | tell, that means I have to detect the PyWin IDE and handle it
    | separately on initialization so I get a real raw input and not the
    | redefined Tkinter version.
    |

    import sys
    import os
    if os.path.basename(sys.executable) == 'Pythonwin.exe':
    #Pythonwin specific initialization
    else:
    #Other

    HTH,

    Vincent Wehren
    vincent wehren, Jan 28, 2006
    #5
  6. Charles Krug

    Charles Krug Guest

    On 2006-01-28, Peter Otten <> wrote:
    > Charles Krug wrote:
    >
    >> Is there a way to detect that I'm running the the PyWin interpreter so
    >> that I can bypass its raw_input behavior?

    >
    > You could test
    >
    > if pywin_specific_module in sys.modules:
    > # use workaraound
    >
    > Or maybe you can get away with always using sys.stdin.readline() instead of
    > raw_input()? Look into cmd.py for an example.
    >
    > Peter
    >


    cmd.py is the "battery included" I was thinking of last night.
    Unfortunately it uses something that PyWin replaces.

    However I did note that PyWin's version raises KeyboardInterrupt out of
    its dialog box.

    That's not ideal, but at least it gives me an idea what I need to trap
    to exit.
    Charles Krug, Jan 28, 2006
    #6
  7. Charles Krug

    Charles Krug Guest

    On 2006-01-28, Charles Krug <> wrote:
    > On 2006-01-28, Peter Otten <> wrote:
    >> Charles Krug wrote:
    >>
    >>> Is there a way to detect that I'm running the the PyWin interpreter so
    >>> that I can bypass its raw_input behavior?

    >>
    >> You could test
    >>
    >> if pywin_specific_module in sys.modules:
    >> # use workaraound
    >>
    >> Or maybe you can get away with always using sys.stdin.readline() instead of
    >> raw_input()? Look into cmd.py for an example.
    >>
    >> Peter
    >>

    >
    > cmd.py is the "battery included" I was thinking of last night.
    > Unfortunately it uses something that PyWin replaces.
    >
    > However I did note that PyWin's version raises KeyboardInterrupt out of
    > its dialog box.
    >
    > That's not ideal, but at least it gives me an idea what I need to trap
    > to exit.
    >


    Okay, I poked around a bit more and found the initialization code that
    does this:

    sys.modules['__builtin__'].raw_input=Win32RawInput

    Which is the substituted function.

    Is there a way to access the original function that I want to use, or do
    I need to come up with some other way to do console input?

    Thanks
    Charles Krug, Jan 29, 2006
    #7
    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. Helge Stenstrom

    Hello World with pywin

    Helge Stenstrom, Feb 17, 2004, in forum: Python
    Replies:
    2
    Views:
    388
    Thomas Heller
    Feb 17, 2004
  2. Miranda Evans

    pywin.mfc, win32ui, Windows common dialogs

    Miranda Evans, May 23, 2004, in forum: Python
    Replies:
    0
    Views:
    577
    Miranda Evans
    May 23, 2004
  3. The Jetman
    Replies:
    0
    Views:
    417
    The Jetman
    Jul 27, 2004
  4. Martin DeMello

    Newbie: pywin problem

    Martin DeMello, Aug 2, 2004, in forum: Python
    Replies:
    5
    Views:
    449
    Martin Bless
    Aug 3, 2004
  5. alex23
    Replies:
    4
    Views:
    225
    Asun Friere
    Jul 16, 2009
Loading...

Share This Page