Starting a script interactively?

  • Thread starter David Klaffenbach
  • Start date
D

David Klaffenbach

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
 
A

Al Kabaila

David said:
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,
 
J

James Kew

Al Kabaila said:
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
 
S

Sami Juvonen

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
 
P

Peter Hansen

David said:
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
 
D

David Klaffenbach

Peter Hansen said:
import os
os.environ['PYTHONINSPECT'] = '1'

(untested)

Tested now! Just what I was looking for!

Thanks, Dave

replies to (e-mail address removed) will bounce; replace 'google' with the
name of your favorite programming language to get a good address.
 
L

Lonnie Princehouse

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()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top