sta*_*tti 16 python scripting interactive
我想做点什么:
do lots of stuff to prepare a good environement
become_interactive
#wait for Ctrl-D
automatically clean up
Run Code Online (Sandbox Code Playgroud)
是否有可能与python?如果没有,你看到另一种方式做同样的事情?
Dun*_*can 12
启动Python时使用-i标志并设置atexit处理程序以在清理时运行.
文件script.py:
import atexit
def cleanup():
print "Goodbye"
atexit.register(cleanup)
print "Hello"
Run Code Online (Sandbox Code Playgroud)
然后你用-i标志启动Python:
C:\temp>\python26\python -i script.py
Hello
>>> print "interactive"
interactive
>>> ^Z
Goodbye
Run Code Online (Sandbox Code Playgroud)
详细阐述IVA的答案: 嵌入式shell,codeincoporating和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()
Run Code Online (Sandbox Code Playgroud)
使用IPython v1.0,您可以简单地使用
from IPython import embed
embed()
Run Code Online (Sandbox Code Playgroud)
有更多选项显示在文档中.
不完全是你想要的东西,但python -i将在执行脚本后启动交互式提示.
-i:运行脚本后交互式检查,(也是PYTHONINSPECT = x)并强制提示,即使stdin似乎不是终端
$ python -i your-script.py
Python 2.5.4 (r254:67916, Jan 20 2010, 21:44:03)
...
>>>
Run Code Online (Sandbox Code Playgroud)