3

Currently if I use Run shell script and start python script there:

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

In case script execution fails due to exception happened, Automator returns the error, which says nothing:

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)?

LA_
  • 19,823
  • 58
  • 172
  • 308

2 Answers2

0

I believe your best way forward is to use the Python debugger with:

import pdb

pdb.set_trace()

inside your py script.

More information here:

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

tcsapunaru
  • 148
  • 8
0

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:

1 error on line 2

You need to catch all errors to prevent that from happening.

Use 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'",)

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'",)
('Line number: ', 5)

Community
  • 1
  • 1
Laurel
  • 5,965
  • 14
  • 31
  • 57