我想做点什么:
do lots of stuff to prepare a good environement
become_interactive
#wait for Ctrl-D
automatically clean up
是否可以使用python?如果没有,你是否看到了另一种做同样事情的方式?
答案 0 :(得分:12)
启动Python时使用-i标志并设置atexit处理程序以在清理时运行。
文件script.py:
import atexit
def cleanup():
print "Goodbye"
atexit.register(cleanup)
print "Hello"
然后你只需用-i标志启动Python:
C:\temp>\python26\python -i script.py
Hello
>>> print "interactive"
interactive
>>> ^Z
Goodbye
答案 1 :(得分:9)
code
模块将允许您启动Python REPL。
答案 2 :(得分:6)
详细阐述IVA的答案:embedding-a-shell,加入code
和Ipython。
def prompt(vars=None, message="welcome to the shell" ):
#prompt_message = "Welcome! Useful: G is the graph, DB, C"
prompt_message = message
try:
from IPython.Shell import IPShellEmbed
ipshell = IPShellEmbed(argv=[''],banner=prompt_message,exit_msg="Goodbye")
return ipshell
except ImportError:
if vars is None: vars=globals()
import code
import rlcompleter
import readline
readline.parse_and_bind("tab: complete")
# calling this with globals ensures we can see the environment
print prompt_message
shell = code.InteractiveConsole(vars)
return shell.interact
p = prompt()
p()
答案 3 :(得分:6)
答案 4 :(得分:5)
不完全是您想要的,但python -i
将在执行脚本后启动交互式提示。
-i
:在运行脚本后以交互方式检查(也是PYTHONINSPECT = x)并强制提示,即使stdin似乎不是终端
$ python -i your-script.py
Python 2.5.4 (r254:67916, Jan 20 2010, 21:44:03)
...
>>>
答案 5 :(得分:-1)
你可以调用python本身:
import subprocess
print "Hola"
subprocess.call(["python"],shell=True)
print "Adios"