Detect PythonWin?

M

Martin Bless

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
 
A

Andrew Dalke

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,

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
(e-mail address removed)
 
M

Martin Bless

[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
 
N

Neil Hodgson

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
 
B

Brett g Porter

Martin said:
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...
 
L

logistix at cathoderaymission.net

Martin Bless said:
[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
.... 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.
 
B

Bob Gailer

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
(e-mail address removed)
303 442 2625
 

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,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top