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

Starting a script interactively?

P: n/a
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
Share this Question
Share on Google+
6 Replies


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

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

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

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

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

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