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

后端 未结 6 979
执笔经年
执笔经年 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 07:59

    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
    

提交回复
热议问题