By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,156 Members | 962 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,156 IT Pros & Developers. It's quick & easy.

Detect PythonWin?

P: n/a
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
Jul 18 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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
da***@dalkescientific.com
Jul 18 '05 #2

P: n/a
[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


Jul 18 '05 #3

P: n/a
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
Jul 18 '05 #4

P: n/a
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...

Jul 18 '05 #5

P: n/a
Martin Bless <m.*****@gmx.de> wrote in message news:<ej********************************@4ax.com>. ..
[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.
Jul 18 '05 #6

P: n/a
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
bg*****@alum.rpi.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

Jul 18 '05 #7

P: n/a
Try this:
import win32ui
win32ui.GetCommandLine()
'"C:\\Python23\\Pythonwin.exe" '
hth
Roger
"Martin Bless" <mb@muenster.de> wrote in message
news:3f*************@news.muenster.de... 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

Jul 18 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.