473,757 Members | 6,899 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
20 2450
Joe
Reinhold,

Interesting.

A key difference between the two is that PYTHONINSPECT will allow you access
to the prompt at the end of your program (assuming no sys.exit or raise
SystemExit) but code.interact() allows you to jump into the program at any
point.
"Reinhold Birkenfeld" <re************ ************@wo lke7.net> wrote in
message news:39******** *****@individua l.net...
/* Check this environment variable at the end, to give programs the
* opportunity to set it from Python.
*/

Reinhold

Jul 18 '05 #11
Michael Hoffman wrote:
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.


Unfortunately it does so in an entirely new namespace, thereby losing
the advantage of -i - namely, that you can investigate the program's
namespace after it's terminated.

regards
Steve

Jul 18 '05 #12
In article <ma************ *************** *********@pytho n.org>,
Steve Holden <st***@holdenwe b.com> wrote:
Michael Hoffman wrote:
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.


Unfortunately it does so in an entirely new namespace, thereby losing
the advantage of -i - namely, that you can investigate the program's
namespace after it's terminated.


code.interact() has a namespace argument ('local'), so it really easy to
have it use the namespace you want.

Just
Jul 18 '05 #13
When I'm using pyunit and want to stop in a point during the test
(skipping all the framework initialization) , I just start the debugger:

import pdb
pdb.set_trace()

You'll get the debugger prompt.

Jul 18 '05 #14
Joe
Found that out :-(

You can use the local=locals() option so at least you have access to the
local variables, which in the case of debugging, is exactly what I needed.

Since -i gives you control at the end of the program the locals are already
gone.

Seems like both approaches have their advantages depending on the situation.

"Steve Holden" <st***@holdenwe b.com> wrote in message
news:ma******** *************** *************@p ython.org...
Unfortunately it does so in an entirely new namespace, thereby losing the
advantage of -i - namely, that you can investigate the program's namespace
after it's terminated.

Jul 18 '05 #15
Joe
Right, but only one namespace. Would be nice if there was a way to give it
both the global and the local namespaces. In my case though the local
namespace was sufficient.

"Just" <ju**@xs4all.nl > wrote in message
news:ju******** *************** *@news1.news.xs 4all.nl...
code.interact() has a namespace argument ('local'), so it really easy to
have it use the namespace you want.

Just

Jul 18 '05 #16
Joe
Isn't this a bug?

Here's the test program:

import code

def test_func():
lv = 1
print '\n\nBEFORE lv: %s\n' % (lv)
code.interact(l ocal=locals())
print '\n\nAFTER lv: %s\n' % (lv)
return

test_func()

gv = 1
print '\n\nBEFORE gv: %s\n' % (gv)
code.interact(l ocal=locals())
print '\n\nAFTER gv: %s\n' % (gv)

Here's the output and interactive session:

BEFORE lv: 1

Python 2.4 (#60, Nov 30 2004, 11:49:19) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright" , "credits" or "license" for more information.
(InteractiveCon sole)
lv = 2
print lv 2 ^Z
AFTER lv: 1

BEFORE gv: 1

Python 2.4 (#60, Nov 30 2004, 11:49:19) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright" , "credits" or "license" for more information.
(InteractiveCon sole) gv = 2
print gv 2 ^Z

AFTER gv: 2

In the case of code.interact being called inside the function, the locals
were available to the interactive environment but changes did not stick when
you returned back the function. (started as lv=1 changed to lv=2 in
interactive session, back to lv=1 in function) Since you are still in the
function and since code.interact used the same local environment why didn't
the change stick until testfunc ended?

When code.interact was called from the outside the function (globals() ==
locals()) the changes stuck. (started as gv=1, changed to gv=2 in
interactive session, stuck as gv=2 back in main).

"Steve Holden" <st***@holdenwe b.com> wrote in message
news:ma******** *************** *************@p ython.org... Michael Hoffman wrote:
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.


Unfortunately it does so in an entirely new namespace, thereby losing the
advantage of -i - namely, that you can investigate the program's namespace
after it's terminated.

regards
Steve

Jul 18 '05 #17
Joe wrote:
Isn't this a bug?

Here's the test program:

import code

def test_func():
lv = 1
print '\n\nBEFORE lv: %s\n' % (lv)
code.interact(l ocal=locals())
print '\n\nAFTER lv: %s\n' % (lv)
return


Check the documentation for locals() [1]:

"Update and return a dictionary representing the current local symbol
table. Warning: The contents of this dictionary should not be modified;
changes may not affect the values of local variables used by the
interpreter."

So if you change things in the dictionary returned by locals() you won't
actually change the local variables.

STeVe

[1] http://docs.python.org/lib/built-in-funcs.html#l2h-45
Jul 18 '05 #18
Joe
Steve,

Thanks, I knew about that but my question is why is it not working
consistently?

Joe
"Steven Bethard" <st************ @gmail.com> wrote in message
news:09******** ************@co mcast.com...
Joe wrote:
Isn't this a bug?

Here's the test program:

import code

def test_func():
lv = 1
print '\n\nBEFORE lv: %s\n' % (lv)
code.interact(l ocal=locals())
print '\n\nAFTER lv: %s\n' % (lv)
return


Check the documentation for locals() [1]:

"Update and return a dictionary representing the current local symbol
table. Warning: The contents of this dictionary should not be modified;
changes may not affect the values of local variables used by the
interpreter."

So if you change things in the dictionary returned by locals() you won't
actually change the local variables.

STeVe

[1] http://docs.python.org/lib/built-in-funcs.html#l2h-45

Jul 18 '05 #19
Joe wrote:
Thanks, I knew about that but my question is why is it not working
consistently?


At the module level, locals() is globals():

py> locals() is globals()
True

And the globals() dict is modifiable.

HTH,

STeVe
Jul 18 '05 #20

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

Similar topics

220
19136
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
2358
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
1918
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
3875
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
1700
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
2914
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
6728
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
3357
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
4768
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
9489
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10072
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
9906
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
9737
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7286
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
6562
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();...
0
5172
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3829
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

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.