472,330 Members | 1,503 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,330 software developers and data experts.

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 1729
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: David Van Horn | last post by:
As a shell script, the following program works as expected. --------------------------------------- #! /usr/bin/sh echo '<html><? print 1...
2
by: Yvan | last post by:
Hi, I will first expose what I need to do, and then how I did it so far: I have a simple java application that is monitoring the status of...
11
by: tdi | last post by:
Ok, stupid question for the day. I'm reading the interview with Steve Moret and he says: "Once a lot of scripts started going in we knew there was...
4
by: draghuram | last post by:
Hi, I would like to have a python script which does some computations at the beginning and then changes to interactive mode (by displaying the...
4
by: joshw53 | last post by:
I'm sure this is a simple question. I just installed Visual Basic.NET and every time I begin running the program Norton anti-virus says that there...
1
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course...
11
by: Gregor Kovač | last post by:
Hi! I have an SQL script with bunch of CREATE TABLES, ALTER TABLES, ... I'd like it to run transactially. I have found that id I run a ALTER...
7
by: Peter S. | last post by:
I currently have created a C# program that automatically updates an SQL database once everyday. It's an application that I manually start. I want...
2
by: LayneMitch via WebmasterKB.com | last post by:
Hello. I posted this earlier with the full code of the JavaScript file I'm trying to open. I'm posting this again, because the file is not the...
0
by: tammygombez | last post by:
Hey fellow JavaFX developers, I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...

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.