473,397 Members | 2,028 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Can I check if I'm running from the interpreter prompt?

Is there a reliable way (this is on Solaris if that matters) to tell if I'm
running in the interactive interpreter as opposed to in a script? I think
examining sys.argv works, but wanted to double check.

Thx,

Skip
Nov 14 '08 #1
4 1915
sk**@pobox.com wrote:
Is there a reliable way (this is on Solaris if that matters) to tell if I'm
running in the interactive interpreter as opposed to in a script? I think
examining sys.argv works, but wanted to double check.
import sys, traceback

try:
raise ValueError
except ValueError:
start = traceback.extract_tb(sys.exc_info()[2])[-1]

Start should show you where the program is being run from.
--Scott David Daniels
Sc***********@Acm.Org
Nov 14 '08 #2
sk**@pobox.com wrote:
Is there a reliable way (this is on Solaris if that matters) to tell if
I'm
running in the interactive interpreter as opposed to in a script? I think
examining sys.argv works, but wanted to double check.
hasattr(sys, "ps1")

http://www.python.org/doc/2.5.2/lib/....html#l2h-5167

"""
ps1
ps2
Strings specifying the primary and secondary prompt of the interpreter.
These are only defined if the interpreter is in interactive mode.
"""

Nov 14 '08 #3

Peterhasattr(sys, "ps1")

Thanks. I wasn't aware there was a documented way to check for
interactivity. It would have been more obvious if sys had an
"isinteractive" method or attribute.

--
Skip Montanaro - sk**@pobox.com - http://smontanaro.dyndns.org/
Nov 14 '08 #4
On Nov 15, 9:48*am, s...@pobox.com wrote:
Thanks. *I wasn't aware there was a documented way to check for
interactivity. *
That's more of a side-effect than the actual intent of those
attributes, which are there to hold the interpreter prompts. But it
does seem to be the only way.

It might be worth noting that this -doesn't- hold true for iPython:

IPython 0.9.1 -- An enhanced Interactive Python.
[...]
In [1]: import sys
In [2]: hasattr(sys, 'ps1')
Out[2]: False
It would have been more obvious if sys had an
"isinteractive" method or attribute.
You could add the following to your sitecustomize.py:

sys.isinteractive = hasattr(sys, 'ps1')

If you want to check for iPython as well:

sys.isinteractive = hasattr(sys, 'ps1') or hasattr(sys,
'ipcompleter')
Nov 15 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Florian Schulze | last post by:
Hi! I want to check from C that the Python Interpreter is still running and it's save to call things like PySys_GetObject, PyFile_WriteString etc. I need this in a C function which might be...
2
by: Eric Ching | last post by:
Can I run pythonw with a script that takes command line arguments then launches a GUI? I try pythonw myscript.pyw -option arg (etc.) and nothing happens. Nothing, as in I am immediately returned...
10
by: BaBS | last post by:
I've a problem whith my python script, i chmod'ed it to +x, addes the line : #!/usr/bin/python2.3 and i verified that /usr/bin/python2.3 exists but i've something like : bad interpreter: No such...
6
by: Hoang Do | last post by:
is there a facility to inspect the run-time of a python script? Essentially, it would execute a script to a set specific point and then drop into the interpreter. Something like a "Stop" or...
6
by: wooks | last post by:
Sorry if this is very basic. I have bought a Functional Programming book that uses the language Hope. I have managed to locate and extract Ross Pattersons hope interpreter from...
2
by: redefined.horizons | last post by:
I've written a few classes that I have been testing in IDLE. However, every time I make a change to the classes based on the testing I have to walk through all he steps of preparing the classes for...
51
by: Ojas | last post by:
Hi!, I just out of curiosity want to know how top detect the client side application under which the script is getting run. I mean to ask the how to know whether the script is running under...
1
by: John Boy | last post by:
First post and very much a newbie to Python. Have 2.5 on Linux (GG). I think I have set up PYTHONPATH correctly in that I can import a module apply_bp and it complains about line 20 in apply_bp...
0
by: Fredrik Lundh | last post by:
joey boggs wrote: I'm not sure I understand what you're doing, but maybe you'll find the following somewhat useful: .... def raw_input(self, prompt=None): .... # override input...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.