gpt4 book ai didi

python - 脚本完成后如何返回到可以访问所有对象的提示符?

转载 作者:太空宇宙 更新时间:2023-11-04 01:21:00 25 4
gpt4 key购买 nike

我正在编写一个脚本,该脚本在运行时会生成大量对象和变量,我需要一种在进程终止之前在控制台中与这些对象和变量进行交互的方法。使用 input() 技巧是不够的,因为它只会等待按键而不是将其发送到提示符。在 eclipse 中运行我的脚本时,我发现没有办法让它在运行后保持打开状态。

def foo():
print('Hi')

def bar():
print('How are you?')


def main():
foo()
bar()

if __name__ == '__main__':
main()

最佳答案

你可以使用-i选项:

python -i /path/to/script.py

根据 Command line and environment - Miscellaneous options :

-i

When a script is passed as first argument or the -c option is used, enter interactive mode after executing the script or the command, even when sys.stdin does not appear to be a terminal. The PYTHONSTARTUP file is not read.

This can be useful to inspect global variables or a stack trace when a script raises an exception. See also PYTHONINSPECT.


另一种方式:如文档所述,也可以设置环境变量PYTHONINSPECT

...

if __name__ == '__main__':
import os
os.environ['PYTHONINSPECT'] = '1'
main()

顺便说一句,问题中的代码拼错了__main__(不是main)。

关于python - 脚本完成后如何返回到可以访问所有对象的提示符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21275927/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com