Detect PythonWin?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Martin Bless

    Detect PythonWin?

    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
  • Andrew Dalke

    #2
    Re: Detect PythonWin?

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

    To see if you're on Windows,
    [color=blue][color=green][color=darkred]
    >>> import sys
    >>> sys.platform[/color][/color][/color]
    'win32'[color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]

    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
    dalke@dalkescie ntific.com


    Comment

    • Martin Bless

      #3
      Re: Detect PythonWin?

      [Andrew Dalke]:
      [color=blue]
      >To see if you're on Windows,[/color]
      [...][color=blue]
      >To see if the win32 extensions are installed[/color]

      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




      Comment

      • Neil Hodgson

        #4
        Re: Detect PythonWin?

        Martin Bless
        [color=blue]
        > but how can my script know it its running from inside PythonWin?[/color]

        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


        Comment

        • Brett g Porter

          #5
          Re: Detect PythonWin?

          Martin Bless wrote:
          [color=blue]
          >
          > 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']
          >[/color]

          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...

          Comment

          • logistix at cathoderaymission.net

            #6
            Re: Detect PythonWin?

            Martin Bless <[email protected] > wrote in message news:<ejfkivo0a h8me65ge5o7pptj 3v6dcq1sfo@4ax. com>...[color=blue]
            > [Andrew Dalke]:
            >[color=green]
            > >To see if you're on Windows,[/color]
            > [...][color=green]
            > >To see if the win32 extensions are installed[/color]
            >
            > 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[/color]
            [color=blue][color=green][color=darkred]
            >>> import sys
            >>> if sys.modules.has _key('pywin'):[/color][/color][/color]
            .... print "pythonwin running"
            .... else:
            .... print "pythonwin not running"
            ....
            pythonwin running[color=blue][color=green][color=darkred]
            >>>[/color][/color][/color]


            This shouldn't have any overhead. Writing a predicate function is
            left as an exercise to the reader.

            Comment

            • Bob Gailer

              #7
              Re: Detect PythonWin?

              [color=blue]
              >how can my script know it its running from inside PythonWin?[/color]

              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
              [email protected] i.edu
              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

              Comment

              • Roger Upole

                #8
                Re: Detect PythonWin?

                Try this:[color=blue][color=green][color=darkred]
                >>> import win32ui
                >>> win32ui.GetComm andLine()[/color][/color][/color]
                '"C:\\Python23\ \Pythonwin.exe" '
                hth
                Roger


                "Martin Bless" <[email protected] > wrote in message
                news:3f29fe44.1 [email protected] ster.de...[color=blue]
                > 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[/color]


                Comment

                Working...