简体   繁体   English

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

[英]Keep Windows Console open after a Python Error

File associations on my machine (winxp home) are such that a python script is directly opened with the python interpreter. 我的机器上的文件关联(winxp home)是这样的,python脚本直接用python解释器打开。 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. 如果我双击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.py but I am sure that there is a more convenient (ie "double click based") solution. 当然,他们可以手动打开控制台窗口并通过键入python myscript.py来执行脚本,但我确信有一个更方便(即“双击”)解决方案。

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. 将其用作文件关联而不是直接使用python.exe。 This will only cause the PAUSE statement to execute if python.exe returns an error 如果python.exe返回错误,这只会导致执行PAUSE语句

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

cmd /k "your command"

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. 我在rossipedia关于类似情况的回答中留下了评论,但我怀疑它会在8岁的帖子中被注意到。 So, I've experimented myself. 所以,我已经尝试过自己。

I have a python script that processes a folder that's dropped on it. 我有一个python脚本来处理放在它上面的文件夹。 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. 为了在错误后保持窗口打开,我不得不修改rossipedia的代码。 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. 我需要一种方法来传递一个路径到bat文件(在其上放一个文件夹)并让bat传递给python脚本的路径。 The following does that: 以下是这样的:

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.bat to 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 . 当我使用debug.bat时,这不起作用。 "modules" is a folder that's in the same folder with the python script. “modules”是与python脚本位于同一文件夹中的文件夹。 When the script is called with debug.bat, the current working directory changes to the dropped file's directory. 使用debug.bat调用脚本时,当前工作目录将更改为已删除文件的目录。 So, to get the path to "modules", I had to use absolute path: 因此,要获得“模块”的路径,我必须使用绝对路径:

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

我这样做的方法是我右键单击我从记事本保存到.py文件的脚本,然后我点击编辑IDLE,这是一个编辑的东西,但你也可以从它运行模块

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

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