在出现 Python 错误后保持 Windows 控制台打开

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2843545/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 14:28:07  来源:igfitidea点击:

Keep Windows Console open after a Python Error

pythonwindowsconsole

提问by cknoll

File associations on my machine (winxp home) are such that a python script is directly opened with the python interpreter. If I double click on a python script a console window runs and every thing is fine - as long as there is no syntax error in the script.

我的机器(winxp home)上的文件关联是这样的,一个python脚本直接用python解释器打开。如果我双击 python 脚本,控制台窗口就会运行,一切都很好——只要脚本中没有语法错误。

In that case the console window opens up for a moment but it is closed immediately. Too fast to read the error message.

在这种情况下,控制台窗口会打开一会儿,但会立即关闭。读取错误消息的速度太快。

Of course their would be the possibility to manually open a console window and to execute the script by typing python myscript.pybut I am sure that there is a more convenient (i.e. "double click based") solution.

当然,他们可以手动打开控制台窗口并通过键入来执行脚本,python myscript.py但我确信有更方便(即“基于双击”)的解决方案。

回答by rossipedia

Make a batch file:

制作一个批处理文件:

C:\Python26\python.exe %1
IF %ERRORLEVEL% NEQ 0 PAUSE

Use that as your file association instead of python.exe directly. This will only cause the PAUSE statement to execute if python.exe returns an error

将其用作您的文件关联,而不是直接使用 python.exe。如果 python.exe 返回错误,这只会导致 PAUSE 语句执行

回答by tzot

The canonical way to run a command in a command prompt window that doesn't close is

在不关闭的命令提示符窗口中运行命令的规范方法是

cmd /k "your command"

回答by akinuri

I've left a comment at rossipedia's answer about a similar situation, but I doubt it's gonna get noticed in a 8 year old post. So, I've experimented myself.

我在 rossipedia's answer 上就类似情况发表了评论,但我怀疑它会在 8 年前的帖子中引起注意。所以,我自己做了实验。

I have a python script that processes a folder that's dropped on it. As I add new lines, sometimes I get syntax errors, and I can't view them because the command prompt window closes too quickly. In order to keep the window open after an error, I had to modify rossipedia's code. I needed a way to pass a path to the bat file (drop a folder on it) and make the bat pass that path to the python script. The following does that:

我有一个 python 脚本来处理一个放在它上面的文件夹。当我添加新行时,有时会出现语法错误,而且我无法查看它们,因为命令提示符窗口关闭得太快。为了在出错后保持窗口打开,我不得不修改rossipedia的代码。我需要一种方法来将路径传递给 bat 文件(在其上放置一个文件夹)并使 bat 将该路径传递给 python 脚本。以下是这样做的:

debug.bat:

debug.bat

@echo off

REM debug.bat and myscript.py should be in the same dir, otherwise use absolute path for the python script

C:\Users\%username%\AppData\Local\Programs\Python\Python36-32\python.exe "%~dp0\myscript.py" %1
IF %ERRORLEVEL% NEQ 0 PAUSE

myscript.py:

myscript.py

import sys
print("printing dropped file path:")
print(sys.argv[1])
input()

If I get an error, I just use the debug.batto view the error, and it works the same (drop a folder on it).

如果出现错误,我只使用debug.bat来查看错误,它的工作原理相同(在其上放置一个文件夹)。

Update:

更新:

In my actual python script, I have this line:

在我的实际 python 脚本中,我有这一行:

sys.path.append("modules")

This doesn't work when I use debug.bat. "modules" is a folder that's in the same folder with the python script. When the script is called with debug.bat, the current working directory changes to the dropped file's directory. So, to get the path to "modules", I had to use absolute path:

当我使用debug.bat. “modules”是与python脚本位于同一文件夹中的文件夹。当使用 debug.bat 调用脚本时,当前工作目录将更改为放置文件的目录。因此,要获得“模块”的路径,我必须使用绝对路径:

sys.path.append(os.path.dirname(os.path.abspath(__file__)) + "\modules")

回答by Joey Rarick

The way that I do it is i right click the script that I saved from notepad to a .py file, then i click edit with IDLE, This is an editing thingy, but you can also run modules from it

我这样做的方法是右键单击我从记事本保存到 .py 文件的脚本,然后单击使用 IDLE 进行编辑,这是一个编辑内容,但您也可以从中运行模块