首页 > 解决方案 > Pythonwin.exe 不指出运行时错误的代码行

问题描述

这是一个棘手的问题,因为它处理的是 Pythonwin.exe 用户界面本身,而不是我的代码。

当我运行一个包含以下代码的文件时,我希望如果它出错,它会在回溯输出中显示它失败的行

我实际上只是创建了一个未受污染的 32 位 python 3.5 环境,安装了 pywin32,然后打开了我的 testfile.py 来运行它,结果如下。 截屏

如果你看到这行说

File "C:\testfile.py", line 2, in <module>
  foo = 2 #this line works fine, but is identified as the traceback line

我本来希望这指向第 5 行而不是第 2 行。在更大的脚本中,这会产生更大的问题,因为我需要根据上下文猜测是哪一行导致了错误。

在基于 anaconda 的环境中复制我的情况: 1. 打开 Anaconda 命令提示符 2. 运行以下行来生成我们的环境

set CONDA_FORCE_32BIT=1
conda create -n test python=3.5
activate test
conda install -y pywin32
  1. 用文本保存“testfile.py”文件:
#comment
foo = 2 #this line works fine, but is identified as the traceback line
a=2
a=3
a=b #this line causes the error, but isn't identified
  1. 打开 PythonWin.exe,打开“testfile.py”文件并按 F5 或单击运行

这不仅仅是 anaconda 安装环境中的问题。如果有人知道如何让它报告错误的代码行,或者它为什么会这样,请告诉我。谢谢!

标签: runtime-errorpywin32traceback

解决方案


推荐阅读