有没有办法让 python 在脚本中间变得交互式?
我想做一些类似的事情:
do lots of stuff to prepare a good environement
become_interactive
#wait for Ctrl-D
automatically clean up
用 python 可以吗?如果不能,你是否看到另一种做同样事情的方法?
I'd like to do something like:
do lots of stuff to prepare a good environement
become_interactive
#wait for Ctrl-D
automatically clean up
Is it possible with python?If not, do you see another way of doing the same thing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
启动 Python 时使用 -i 标志并设置要在清理时运行的 atexit 处理程序。
文件 script.py:
然后你只需使用 -i 标志启动 Python:
Use the -i flag when you start Python and set an atexit handler to run when cleaning up.
File script.py:
and then you just start Python with the -i flag:
code
模块将允许您启动 Python REPL。The
code
module will allow you to start a Python REPL.使用 IPython v1.0,您可以简单地使用
文档。
With IPython v1.0, you can simply use
with more options shown in the docs.
详细说明 IVA 的答案: embedding-a-shell,结合了
code
和Ipython。To elaborate on IVA's answer: embedding-a-shell, incoporating
code
and Ipython.不完全是您想要的东西,但 python
-i
将在执行脚本后启动交互式提示。Not exactly the thing you want but python
-i
will start interactive prompt after executing the script.你可以调用 python 本身:
You may call python itself: