简体   繁体   English

python程序终止后,如何保持控制台窗口打开以查看产生的任何错误?

[英]How do I keep a console window open after my python program terminates to see any errors produced?

I am using python 3 on Win10 and running my code by opening a Command Prompt window and typing the file location. 我在Win10上使用python 3,并通过打开命令提示符窗口并键入文件位置来运行代码。 However, the window closes as soon as the program terminates, and before I can read any errors. 但是,该窗口将在程序终止后以及在我读取任何错误之前关闭。 Edit: This happens whether or not the program has errors. 编辑:无论程序是否有错误,都会发生这种情况。 Thank you. 谢谢。

Solution 1: 解决方案1:

I just saw your comment: 我刚刚看到您的评论:

When I do that, I recieve the error 'python' is not recognized as an internal or external command, operable program or batch file 当我这样做时,我收到错误“ python”未被识别为内部或外部命令,可操作程序或批处理文件

It looks like you haven't specified the path to the python executable: you need to add the python executable path to your Window's PATH variable. 似乎您尚未指定python可执行文件的路径:您需要将python可执行文件路径添加到Window的PATH变量中。 You can see how to do that here: Add Python to the PATH Environmental Variable ('python' is not recognized as an internal or external command) 您可以在此处查看操作方法: 将Python添加到PATH环境变量中(“ python”未被识别为内部或外部命令)

Solution 2 : 解决方案2

You can use input("enter to exit") at the end of your python code to keep the program alive. 您可以在python代码的末尾使用input("enter to exit")来使程序保持活动状态。 It would exit once you press enter. 一旦按回车,它将退出。

You could also surround your code in a try except statement and place thr input() in the except to prevent the program from exiting when there are errors, but like @Kevin mentioned in the comments, this would catch run time errors but not syntax errors. 你也可以围绕你的代码在try except声明,并把THR input()except ,以防止程序时出现错误退出,但像@Kevin在评论中提到的,这会赶上运行时错误而不是语法错误。

Solution 3 : 解决方案3

You can write errors or anything information you want to a file such as log.txt for example, and then read that log file once the code finishes running eg how to write to a file in Python 您可以将错误或所需的任何信息写入文件(例如log.txt ,然后在代码运行完毕后读取该日志文件,例如, 如何在Python中写入文件

FWIW, I have several Python versions on my Windows system, so I don't want to add any Python directories to my path permanently. FWIW,我的Windows系统上有多个Python版本,因此我不想将任何Python目录永久添加到我的路径中。

Code for each version is in a separate folder (eg 'py37'), with a subfolder for each project eg 'myProject'. 每个版本的代码位于单独的文件夹(例如,“ py37”)中,每个项目都有一个子文件夹,例如,“ myProject”。 In py37, there's a batch file called pyEnv.bat with this content: 在py37中,有一个名为pyEnv.bat的批处理文件,其内容如下:

@echo off
path=%path%;C:\Python37\;C:\Python37\Scripts\
cd.
cmd

In Windows explorer, I head over to the project folder I want to work in, click in the address bar and type ..\\pyEnv . 在Windows资源管理器中,我转到要使用的项目文件夹,在地址栏中单击,然后键入..\\pyEnv That launches a DOS-box, in which I now can do python myproject.py . 这将启动一个DOS盒,我现在可以在其中执行python myproject.py You can see print() output, errors, and so on. 您可以看到print()输出,错误等。

You can up-arrow to try different modules, having typed them first. 您可以向上箭头尝试不同的模块,只需先输入它们即可。

Once you quit the DOS-box, your path is back to normal again. 退出DOS框后,您的路径又恢复了正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM