473,387 Members | 2,436 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 1806
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 ?></html>' | php...
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 several services, the configuration for this monitoring...
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 no way we could back out of using Python." I'm...
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 prompt). How can I do this? Thanks in advance,...
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 is a malicious script file that it suggest I stop. ...
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 tutors are going to be teaching us JAVA wth bluej...
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 STATEMENT inside a transaction and then roll it back...
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 make it so that if the (Windows 2003) server...
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 problem. I seem to be having a 'windows' problem...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...

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.