如何保持 Python 脚本输出窗口打开? [英] How to keep a Python script output window open?

查看:47
本文介绍了如何保持 Python 脚本输出窗口打开?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用 Python.当我在 Windows 上执行 python 脚本文件时,输出窗口出现但立即消失.我需要它留在那里,以便我可以分析我的输出.我怎样才能让它保持打开状态?

I have just started with Python. When I execute a python script file on Windows, the output window appears but instantaneously goes away. I need it to stay there so I can analyze my output. How can I keep it open?

推荐答案

您有几个选择:

  1. 从已经打开的终端运行程序.打开命令提示符并键入:

  1. Run the program from an already-open terminal. Open a command prompt and type:

python myscript.py

为此,您需要路径中的 python 可执行文件.只需查看 如何在 Windows 上编辑环境变量,并添加 C:\PYTHON26(或您安装 python 的任何目录).

For that to work you need the python executable in your path. Just check on how to edit environment variables on Windows, and add C:\PYTHON26 (or whatever directory you installed python to).

当程序结束时,它会将您返回到 cmd 提示符而不是关闭窗口.

When the program ends, it'll drop you back to the cmd prompt instead of closing the window.

添加代码以在脚本结束时等待.对于 Python2,添加 ...

Add code to wait at the end of your script. For Python2, adding ...

raw_input()

... 在脚本的末尾使其等待 Enter 键.这种方法很烦人,因为您必须修改脚本,并且必须记住在完成后将其删除.在测试其他人的脚本时特别烦人.对于 Python3,使用 input().

... at the end of the script makes it wait for the Enter key. That method is annoying because you have to modify the script, and have to remember removing it when you're done. Specially annoying when testing other people's scripts. For Python3, use input().

使用为您暂停的编辑器.一些为python准备的编辑器在执行后会自动为你暂停.其他编辑器允许您配置用于运行程序的命令行.我发现在运行时将其配置为python -i myscript.py"特别有用.这会在程序结束后将您带到一个 python shell,加载程序环境,因此您可以进一步使用变量并调用函数和方法.

Use an editor that pauses for you. Some editors prepared for python will automatically pause for you after execution. Other editors allow you to configure the command line it uses to run your program. I find it particularly useful to configure it as "python -i myscript.py" when running. That drops you to a python shell after the end of the program, with the program environment loaded, so you may further play with the variables and call functions and methods.

这篇关于如何保持 Python 脚本输出窗口打开?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆