468,771 Members | 1,847 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,771 developers. It's quick & easy.

Starting a script interactively?

Is there a way from within a python script to cause the interpreter to
be in interactive mode after the script finishes?

so that if I run:

myscript.py

it will always execute as if I had run:

python23.exe -i myscript.py

I know I could use a batch file or shell script but can it be done
from within the script itself?

Thanks, Dave
Jul 18 '05 #1
6 1599
David Klaffenbach wrote:
Is there a way from within a python script to cause the interpreter to
be in interactive mode after the script finishes?

so that if I run:

myscript.py

it will always execute as if I had run:

python23.exe -i myscript.py


In Linux OS, add first line in myscript.py as follows:

#! /<path to the directory where python lives>/python

and make sure that myscript.py is executable. I don't know if this works
under dos, but there should be some equivalent.

Regards,
--
Al Kabaila, a_*******@yahoo.com.au
Jul 18 '05 #2
"Al Kabaila" <a_*******@yahoo.com.au> wrote:
David Klaffenbach wrote:
Is there a way from within a python script to cause the interpreter to
be in interactive mode after the script finishes?


In Linux OS, add first line in myscript.py as follows:
#! /<path to the directory where python lives>/python
and make sure that myscript.py is executable. I don't know if this works
under dos, but there should be some equivalent.


There is, for NT-class systems -- add .py to PATHEXT -- but I think the
Python installer does this automatically. (The ActiveState installer
certainly does.)

But that only answers the question "how do I make a script executable". The
OP wants to know how to have a script drop to the Python interpreter, rather
than back to the shell, when the script completes.

Under Windows, you could achieve this behaviour for _all_ Python scripts by
tweaking the "open" action of the .py file association to add the -i flag.
But I think the OP wants to do it only for _some_ scripts, and from _within_
the script -- nothing comes to mind for this.

James
Jul 18 '05 #3
On 2 Jan 2004 13:05:14 -0800,
David Klaffenbach <go****@klaff.org> blurted:
Is there a way from within a python script to cause the interpreter to
be in interactive mode after the script finishes?

so that if I run:

myscript.py

it will always execute as if I had run:

python23.exe -i myscript.py


In Windows, create another extension mapping for python, like
..pyi and set the program to open them to something like
c:\python23\python.exe -i "%1" %* (see .pyw and py for guidance).

Then rename your script to myscript.pyi
--
"...you want a .sig with that?"
Jul 18 '05 #4
David Klaffenbach wrote:

Is there a way from within a python script to cause the interpreter to
be in interactive mode after the script finishes?


Added in Python 2.3, according to http://www.python.org/2.3/highlights.html:

PYTHONINSPECT - A program can now set the environment variable $PYTHONINSPECT to
some string value in Python, and cause the interpreter to enter the interactive
prompt at program exit, as if Python had been invoked with the -i option.
In other words, this should work:

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

(untested)

-Peter
Jul 18 '05 #5
Peter Hansen <pe***@engcorp.com> wrote in message news:<3F***************@engcorp.com>...
<snip>
import os
os.environ['PYTHONINSPECT'] = '1'

(untested)


Tested now! Just what I was looking for!

Thanks, Dave

replies to go****@klaff.org will bounce; replace 'google' with the
name of your favorite programming language to get a good address.
Jul 18 '05 #6
Setting PYTHONINSPECT is probably the best way, but for completeness,
I offer this: Invoking a new interactive interpreter.

if __name__ == '__main__':
import code
interpreter = code.InteractiveConsole(globals())
interpreter.interact()

David Klaffenbach wrote in message news:<37*************************@posting.google.c om>...
Is there a way from within a python script to cause the interpreter to
be in interactive mode after the script finishes?

Jul 18 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by David Van Horn | last post: by
2 posts views Thread by Yvan | last post: by
11 posts views Thread by Gregor Kovač | last post: by
7 posts views Thread by Peter S. | last post: by
2 posts views Thread by LayneMitch via WebmasterKB.com | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.