Detect PythonWin?

Discussion in 'Python' started by Martin Bless, Aug 1, 2003.

  1. Martin Bless

    Martin Bless Guest

    IMHO PythonWin is great help in developing.

    How can I do something like:

    if detect("running on PythonWin"):
    pass
    # fake arguments ...
    else:
    "proceed normal"

    ?

    Martin
     
    Martin Bless, Aug 1, 2003
    #1
    1. Advertising

  2. Martin Bless

    Andrew Dalke Guest

    Martin Bless wants to know how to do:
    > if detect("running on PythonWin"):
    > pass
    > # fake arguments ...
    > else:
    > "proceed normal"


    To see if you're on Windows,

    >>> import sys
    >>> sys.platform

    'win32'
    >>>


    To see if the win32 extensions are installed

    try:
    import win32com # or another module in the extension
    except ImportError:
    print "Not installed"
    else:
    print "Installed!"

    Andrew
     
    Andrew Dalke, Aug 1, 2003
    #2
    1. Advertising

  3. Martin Bless

    Martin Bless Guest

    [Andrew Dalke]:

    >To see if you're on Windows,

    [...]
    >To see if the win32 extensions are installed


    Ok,
    but how can my script know it its running from inside PythonWin?

    I often have the situation that while developing with PythonWin I have
    to insert code like

    if 1 and "developing in PythonWin":
    sys.argv[1:] = ['fake', 'arguments']

    Later on or when running the same script from the commandline I need
    to disable this if statement. Nasty.

    I'm looking for a predicate functions that will let my script know if
    it's running from inside PythonWin. And the function shouldn't use
    much time or resources if we are not in PythonWin.

    Anybody knows?

    Martin
     
    Martin Bless, Aug 1, 2003
    #3
  4. Martin Bless

    Neil Hodgson Guest

    Martin Bless

    > but how can my script know it its running from inside PythonWin?


    I don't know if it is all that robust but performing 'dir()' from a
    PythonWin Interactive Window shows an interesting 'pywin' symbol, so:

    if 'pywin' in dir():
    print 'PythonWin'

    Seems to work.

    Neil
     
    Neil Hodgson, Aug 1, 2003
    #4
  5. Martin Bless wrote:

    >
    > I often have the situation that while developing with PythonWin I have
    > to insert code like
    >
    > if 1 and "developing in PythonWin":
    > sys.argv[1:] = ['fake', 'arguments']
    >


    Is there a subtle reason that you can't pass the arguments using the
    "Arguments" field on the PythonWin "Run Script" dialog?

    That's always worked fine for me...
     
    Brett g Porter, Aug 1, 2003
    #5
  6. Martin Bless <> wrote in message news:<>...
    > [Andrew Dalke]:
    >
    > >To see if you're on Windows,

    > [...]
    > >To see if the win32 extensions are installed

    >
    > Ok,
    > but how can my script know it its running from inside PythonWin?
    >
    > I often have the situation that while developing with PythonWin I have
    > to insert code like
    >
    > if 1 and "developing in PythonWin":
    > sys.argv[1:] = ['fake', 'arguments']
    >
    > Later on or when running the same script from the commandline I need
    > to disable this if statement. Nasty.
    >
    > I'm looking for a predicate functions that will let my script know if
    > it's running from inside PythonWin. And the function shouldn't use
    > much time or resources if we are not in PythonWin.
    >
    > Anybody knows?
    >
    > Martin


    >>> import sys
    >>> if sys.modules.has_key('pywin'):

    .... print "pythonwin running"
    .... else:
    .... print "pythonwin not running"
    ....
    pythonwin running
    >>>



    This shouldn't have any overhead. Writing a predicate function is
    left as an exercise to the reader.
     
    logistix at cathoderaymission.net, Aug 1, 2003
    #6
  7. Martin Bless

    Bob Gailer Guest


    >how can my script know it its running from inside PythonWin?


    I addressed this a few months ago:

    import sys
    if len(sys.modules) > 200: # running under PythonWin

    This is because PythonWin loads a lot of modules beyond what native Python
    does. (IDLE also loads some, but a lot less that PyrthonWin).

    Bob Gailer

    303 442 2625


    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.500 / Virus Database: 298 - Release Date: 7/10/2003
     
    Bob Gailer, Aug 1, 2003
    #7
  8. Martin Bless

    Roger Upole Guest

    Try this:
    >>> import win32ui
    >>> win32ui.GetCommandLine()

    '"C:\\Python23\\Pythonwin.exe" '
    hth
    Roger


    "Martin Bless" <> wrote in message
    news:...
    > IMHO PythonWin is great help in developing.
    >
    > How can I do something like:
    >
    > if detect("running on PythonWin"):
    > pass
    > # fake arguments ...
    > else:
    > "proceed normal"
    >
    > ?
    >
    > Martin
     
    Roger Upole, Aug 2, 2003
    #8
    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. Ilariu Raducan

    PythonWin dynamic toolbar

    Ilariu Raducan, Jul 7, 2003, in forum: Python
    Replies:
    0
    Views:
    332
    Ilariu Raducan
    Jul 7, 2003
  2. Richard Lee
    Replies:
    2
    Views:
    463
    Daniel Dittmar
    Jul 22, 2003
  3. Adam
    Replies:
    0
    Views:
    350
  4. stephan
    Replies:
    0
    Views:
    460
    stephan
    Jul 30, 2003
  5. Henrik Weber
    Replies:
    1
    Views:
    365
    Henrik Weber
    Aug 7, 2003
Loading...

Share This Page