简体   繁体   English

如何使用Automator运行python脚本以查看调试/错误消息?

[英]How to run python script with Automator to see debug/error messages?

Currently if I use Run shell script and start python script there: 目前,如果我使用Run shell script并启动python脚本:

/usr/bin/python /Users/myuser/script.py "$1"

In case script execution fails due to exception happened, Automator returns the error, which says nothing: 如果由于异常发生脚本执行失败,Automator会返回错误,该错误没有任何说明:

Run Shell Script failed - 1 error
Traceback (most recent call last):

Is there any way to run the shell script to see all debug message (or, to run Terminal and run python script there)? 有没有办法运行shell脚本来查看所有调试消息(或者,运行终端并在那里运行python脚本)?

I believe your best way forward is to use the Python debugger with: 我相信你最好的方法是使用Python调试器:

import pdb

pdb.set_trace()

inside your py script. 你的py脚本里面。

More information here: 更多信息:

https://docs.python.org/2/library/pdb.html https://docs.python.org/2/library/pdb.html

When an error occurs, program execution stops and output is overwritten with an abbreviated version of the error (which provides no useful information). 发生错误时,程序执行停止,输出被错误的缩写版本覆盖(没有提供有用的信息)。

For example, running the buggy code from here will only show you this: 例如,从这里运行有缺陷的代码只会向您显示:

第2行有1个错误

You need to catch all errors to prevent that from happening. 您需要捕获所有错误以防止这种情况发生。

Use try: ... except : 使用try: ... except

try:
    l = [[1, 2, 3], [4, 5, 6], [7], [8, 9]]
    reduce(lambda x, y: x.extend(y), l)
except Exception as error:
    print(repr(error))

You will get this printed out: 你会得到这个打印出来的:

AttributeError("'NoneType' object has no attribute 'extend'",) AttributeError(“'NoneType'对象没有属性'extend'”,)

If you want more information, you can just tweak the code: 如果您想了解更多信息,可以调整代码:

from sys import exc_info
from traceback import extract_tb
try:
    l = [[1, 2, 3], [4, 5, 6], [7], [8, 9]]
    reduce(lambda x, y: x.extend(y), l)
except Exception as error:
    print(repr(error))
    print("Line number: ", extract_tb(exc_info()[2])[0][1])

Prints: 打印:

AttributeError("'NoneType' object has no attribute 'extend'",) AttributeError(“'NoneType'对象没有属性'extend'”,)
('Line number: ', 5) ('行号:',5)

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

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