473,729 Members | 2,309 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

python -i (interactive environment)

Joe
When you run "python -i scriptname.py" after the script completes you left
at the interactive command prompt.

Is there a way to have this occur from a running program?

In other words can I just run scriptname.py (NOT python -i scriptname.py)
and inside of scriptname.py I decide that I want to fall back to the
interactive prompt?

I've searched and so far the only thing I've come up with is to use pdb, but
that is not exactly the same as the interactive prompt.

Is there any way to do it that I have missed?

Thanks.
Jul 18 '05 #1
20 2445
Very simple is you're on UNIX ...

You juste have to put at the beginnin of your file :

#!/usr/bin/python -i

And it juste does what you want :)

Pierre

Joe a écrit :
When you run "python -i scriptname.py" after the script completes you left
at the interactive command prompt.

Is there a way to have this occur from a running program?

In other words can I just run scriptname.py (NOT python -i scriptname.py)
and inside of scriptname.py I decide that I want to fall back to the
interactive prompt?

I've searched and so far the only thing I've come up with is to use pdb, but
that is not exactly the same as the interactive prompt.

Is there any way to do it that I have missed?

Thanks.

Jul 18 '05 #2
Joe
Hi Pierre,

Thanks for the reply, but I am not on Unix and it even if I was that
solution it does not achieve the desired results.

I want the script to decide whether to fall back to the interactive prompt.
You solution makes it ALWAYS fall back to the interactive prompt.

I want to do something like:

try:
# execute code
except MyExceptionOccu rred, except_msg:
# fall to interactive prompt.

In other words I want to have my program to determine whether the
interactive prompt to be displayed or not.

Thanks!
"Pierre Barbier de Reuille" <pi************ @cirad.fr> wrote in message
news:42******** *************** @news.free.fr.. .
Very simple is you're on UNIX ...

You juste have to put at the beginnin of your file :

#!/usr/bin/python -i

And it juste does what you want :)

Pierre

Joe a écrit :
When you run "python -i scriptname.py" after the script completes you
left at the interactive command prompt.

Is there a way to have this occur from a running program?

In other words can I just run scriptname.py (NOT python -i scriptname.py)
and inside of scriptname.py I decide that I want to fall back to the
interactive prompt?

I've searched and so far the only thing I've come up with is to use pdb,
but that is not exactly the same as the interactive prompt.

Is there any way to do it that I have missed?

Thanks.

Jul 18 '05 #3
Joe wrote:
I want the script to decide whether to fall back to the interactive prompt.
You solution makes it ALWAYS fall back to the interactive prompt.


Actually, using sys.exit() means the program can exit even if python -i
is used.

You can use:

import code
code.interact()

which emulates the interactive prompt.
--
Michael Hoffman
Jul 18 '05 #4
I posted the following a while back. I think this is what you are
looking for.

This can be done fairly easily by creating a module (lets call it
interactive) with the following code in it.
-----------
import sys,os

def debug_exception (type, value, traceback):
# Restore redirected standard I/O
sys.stdin = sys.__stdin__
sys.stdout = sys.__stdout__
sys.stderr = sys.__stderr__

# Kick the interpreter into interactive mode and call the original
# exception handler.
os.environ['PYTHONINSPECT'] = '1'
sys.__excepthoo k__(type, value, traceback)

sys.excepthook = debug_exception
-----------

Now if you import this module and raise an unhandled exception, you will
be in interactive mode. In other words, I think the following script
does what you are asking for.

-----------
import interactive

raise RuntimeError('I nteractive Mode')
-----------

This also has the advantage that if there are no unhandled exceptions in
your script, the script runs and terminates normally.

Enjoy,
Ray Buvel

Joe wrote:
Hi Pierre,

Thanks for the reply, but I am not on Unix and it even if I was that
solution it does not achieve the desired results.

I want the script to decide whether to fall back to the interactive prompt.
You solution makes it ALWAYS fall back to the interactive prompt.

I want to do something like:

try:
# execute code
except MyExceptionOccu rred, except_msg:
# fall to interactive prompt.

In other words I want to have my program to determine whether the
interactive prompt to be displayed or not.

Thanks!
"Pierre Barbier de Reuille" <pi************ @cirad.fr> wrote in message
news:42******** *************** @news.free.fr.. .
Very simple is you're on UNIX ...

You juste have to put at the beginnin of your file :

#!/usr/bin/python -i

And it juste does what you want :)

Pierre

Joe a écrit :
When you run "python -i scriptname.py" after the script completes you
left at the interactive command prompt.

Is there a way to have this occur from a running program?

In other words can I just run scriptname.py (NOT python -i scriptname.py)
and inside of scriptname.py I decide that I want to fall back to the
interactiv e prompt?

I've searched and so far the only thing I've come up with is to use pdb,
but that is not exactly the same as the interactive prompt.

Is there any way to do it that I have missed?

Thanks.


Jul 18 '05 #5
Joe wrote:
When you run "python -i scriptname.py" after the script completes you left
at the interactive command prompt.

Is there a way to have this occur from a running program?

In other words can I just run scriptname.py (NOT python -i scriptname.py)
and inside of scriptname.py I decide that I want to fall back to the
interactive prompt?

I've searched and so far the only thing I've come up with is to use pdb, but
that is not exactly the same as the interactive prompt.

Is there any way to do it that I have missed?

Thanks.


Yes you can set the PYTHONINSPECT environment variable to something
other than an empty string from within your program.

MikeG
Jul 18 '05 #6
Joe
Thanks Michael, that's what I was looking for.

Are there any differences between that and the actual interactve prompt that
I should be aware of?

Would you consider that a better method than using (thanks to those who
suggested this other option):

import os
os.environ['PYTHONINSPECT'] = '1'
"Michael Hoffman" <ca*******@mh39 1.invalid> wrote in message
news:d0******** **@gemini.csx.c am.ac.uk...
Joe wrote:
I want the script to decide whether to fall back to the interactive
prompt. You solution makes it ALWAYS fall back to the interactive prompt.


Actually, using sys.exit() means the program can exit even if python -i
is used.

You can use:

import code
code.interact()

which emulates the interactive prompt.
--
Michael Hoffman

Jul 18 '05 #7
Joe wrote:
Are there any differences between that and the actual interactve prompt that
I should be aware of?
I don't know, unfortunately.
Would you consider that a better method than using (thanks to those who
suggested this other option):

import os
os.environ['PYTHONINSPECT'] = '1'


Actually I would do that everyone else suggested for your use case. I thought
about trying this method but couldn't imagine that it would actually work!
--
Michael Hoffman
Jul 18 '05 #8
Joe
Funny you said that, because I didn't even bother trying it because I didn't
think it would work either. I figured that python did something internally
when it saw the -i switch. Looks like it just checks for it when it's done
with your code.

One difference between the two methods that I see is that when you set
PYTHONINSPECT you don't really have to do anything else and unless
sys.exit() or raise SystemExit is used you will get the interactive prompt.

Using code.interact you would need to wrap everything in a try/except block
and call code.interact from there, or you could invoke it whenever you
wanted too. This has the advantage that you can even trap SystemExit if you
want too.

Thanks again to everyone!
"Michael Hoffman" <ca*******@mh39 1.invalid> wrote in message
news:d0******** **@gemini.csx.c am.ac.uk...
Joe wrote:

Actually I would do that everyone else suggested for your use case. I
thought
about trying this method but couldn't imagine that it would actually work!
--
Michael Hoffman

Jul 18 '05 #9
Michael Hoffman wrote:
Joe wrote:
Are there any differences between that and the actual interactve prompt that
I should be aware of?


I don't know, unfortunately.
Would you consider that a better method than using (thanks to those who
suggested this other option):

import os
os.environ['PYTHONINSPECT'] = '1'


Actually I would do that everyone else suggested for your use case. I thought
about trying this method but couldn't imagine that it would actually work!


Actually, using this behavior is encouraged by the developers. See this
comment in Modules/main.c:

/* Check this environment variable at the end, to give programs the
* opportunity to set it from Python.
*/

Reinhold
Jul 18 '05 #10

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

Similar topics

220
19101
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have any preconceived ideas about it. I have noticed, however, that every programmer I talk to who's aware of Python is also talking about Ruby. So it seems that Ruby has the potential to compete with and displace Python. I'm curious on what basis it...
20
2355
by: Mediocre Person | last post by:
Well, after years of teaching grade 12 students c++, I've decided to make a switch to Python. Why? * interactive mode for learning * less fussing with edit - compile - link - run - debug - edit - compile - link - run -..... * lots of modules * I was getting tired of teaching c++! Bored teacher = bad instruction.
0
1915
by: Fuzzyman | last post by:
Movable Python has just been released on sourceforge. Movable Python is a frozen distribution of Python. It will run python scripts without needing to be installed. http://sourceforge.net/projects/movpy http://www.voidspace.org.uk/python/movpy Fancy trying out Python 2.4 without installing it ? Want a distribution of python you can carry around on a flash card ? Need to test scripts with several versions of python ? Movable Python may...
17
3872
by: Paul Rubin | last post by:
Dumb question from a Windows ignoramus: I find myself needing to write a Python app (call it myapp.py) that uses tkinter, which as it happens has to be used under (ugh) Windows. That's Windows XP if it makes any difference. I put a shortcut to myapp.py on the desktop and it shows up as a little green snake icon, which is really cool and Pythonic. When I double click the icon, the app launches just fine and the tkinter interface does...
3
1699
by: qwweeeit | last post by:
Hi all, is it possible to enter an interactive session and automatically do some initialization? I explain better: I want that when I start interactive Python on a console (I use Linux) two command lines be executed automatically: Python 2.3.4 (#2, Aug 19 2004, 15:49:40) on linux2 Type "help", "copyright", "credits" or "license" for more information.
30
2911
by: bblais | last post by:
Hello, Let me start by saying that I am coming from a background using Matlab (or Octave), and C++. I am going to outline the basic nuts-and-bolts of how I work in these languages, and ask for some help to find out how the same thing is done in Python. I am not sure what the standard is. In C++, I open up an editor in one window, a Unix shell in another. I write the code in the editor, then switch to the shell window for compile...
118
6719
by: 63q2o4i02 | last post by:
Hi, I've been thinking about Python vs. Lisp. I've been learning Python the past few months and like it very much. A few years ago I had an AI class where we had to use Lisp, and I absolutely hated it, having learned C++ a few years prior. They didn't teach Lisp at all and instead expected us to learn on our own. I wasn't aware I had to uproot my thought process to "get" it and wound up feeling like a moron. In learning Python I've...
13
3356
by: dmh2000 | last post by:
I am experimenting with the interactive interpreter environments of Python and Ruby and I ran into what seems to be a fundamental difference. However I may be doing something wrong in Python. Please comment and correct me if I am wrong In both languages, you can start up the interactive interpreter ('python' and 'irb'), load source files and do stuff, like create objects and call their methods. When you want to change something, you can...
4
4766
by: fuffens | last post by:
Hi! Does anyone here have experience with Python in Eclipse environment? I have downloaded the Pydev plug-in for Eclipse. I like the editor and I find it very useful to be able to add files in a project and easily navigate between the files. I can start a Python interactive window. However, the Python interactive window does not keep varibles in memory from the Python scripts that I run. Also, when I debug I want to be able to set a...
0
8761
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9426
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9281
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8148
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6722
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6022
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3238
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2680
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2163
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.