有没有办法让 python 在脚本中间变得交互式?

发布于 2024-08-29 00:55:25 字数 195 浏览 2 评论 0原文

我想做一些类似的事情:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

我还不会笑 2024-09-05 00:55:25

启动 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

Use the -i flag when you start Python and set an atexit handler to run when cleaning up.

File script.py:

import atexit
def cleanup():
    print "Goodbye"
atexit.register(cleanup)
print "Hello"

and then you just start Python with the -i flag:

C:\temp>\python26\python -i script.py
Hello
>>> print "interactive"
interactive
>>> ^Z

Goodbye
独行侠 2024-09-05 00:55:25

code 模块将允许您启动 Python REPL。

The code module will allow you to start a Python REPL.

怪异←思 2024-09-05 00:55:25

使用 IPython v1.0,您可以简单地使用

from IPython import embed
embed()

文档

With IPython v1.0, you can simply use

from IPython import embed
embed()

with more options shown in the docs.

烟花肆意 2024-09-05 00:55:25

详细说明 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()

To elaborate on IVA's answer: embedding-a-shell, incoporating code and 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()
信愁 2024-09-05 00:55:25

不完全是您想要的东西,但 python -i 将在执行脚本后启动交互式提示。

-i :运行脚本后交互式检查(也为 PYTHONINSPECT=x)并强制提示,即使 stdin 看起来不是终端

$ python -i your-script.py
Python 2.5.4 (r254:67916, Jan 20 2010, 21:44:03) 
...
>>> 

Not exactly the thing you want but python -i will start interactive prompt after executing the script.

-i : inspect interactively after running script, (also PYTHONINSPECT=x) and force prompts, even if stdin does not appear to be a terminal

$ python -i your-script.py
Python 2.5.4 (r254:67916, Jan 20 2010, 21:44:03) 
...
>>> 
娇纵 2024-09-05 00:55:25

你可以调用 python 本身:

import subprocess

print "Hola"

subprocess.call(["python"],shell=True)

print "Adios"

You may call python itself:

import subprocess

print "Hola"

subprocess.call(["python"],shell=True)

print "Adios"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文