Is there a way to make python become interactive in the middle of a script?

后端 未结 6 987
执笔经年
执笔经年 2021-02-01 07:03

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 i

6条回答
  •  滥情空心
    2021-02-01 08:06

    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()
    

提交回复
热议问题