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

时间:2022-12-11 21:33:52

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?

是否有可能与python?如果没有,你看到另一种方式做同样的事情?

6 个解决方案

#1


6  

With IPython v1.0, you can simply use

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

from IPython import embed
embed()

with more options shown in the docs.

有更多选项显示在文档中。

#2


11  

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

在启动Python时使用-i标志并设置atexit处理程序以在清理时运行。

File script.py:

文件script.py:

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

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

然后你用-i标志启动Python:

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

Goodbye

#3


9  

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

代码模块将允许您启动Python REPL。

#4


6  

To elaborate on IVA's answer: embedding-a-shell, incoporating code and Ipython.

详细阐述IVA的答案:嵌入式shell,嵌入式代码和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()

#5


5  

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

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

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

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

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

#6


-1  

You may call python itself:

你可以调用python本身:

import subprocess

print "Hola"

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

print "Adios"

#1


6  

With IPython v1.0, you can simply use

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

from IPython import embed
embed()

with more options shown in the docs.

有更多选项显示在文档中。

#2


11  

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

在启动Python时使用-i标志并设置atexit处理程序以在清理时运行。

File script.py:

文件script.py:

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

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

然后你用-i标志启动Python:

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

Goodbye

#3


9  

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

代码模块将允许您启动Python REPL。

#4


6  

To elaborate on IVA's answer: embedding-a-shell, incoporating code and Ipython.

详细阐述IVA的答案:嵌入式shell,嵌入式代码和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()

#5


5  

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

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

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

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

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

#6


-1  

You may call python itself:

你可以调用python本身:

import subprocess

print "Hola"

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

print "Adios"