出于可读性考虑,我想使用回溯(nir0s/backtrace)代替进入PyCharm的标准堆栈跟踪。
即像这样的代码:
def func1(a):
return func2(a+1)
def func2(b):
return func3(b+1)
def func3(c):
return c/0
print(func1(1))
生成此堆栈跟踪(行号不匹配,这只是一个示例):
Traceback (most recent call last):
File "/home/alessandro/git-files/python/new_calc/app/new_calc.py", line 51, in <module>
print(func1(1))
File "/home/alessandro/git-files/python/new_calc/app/new_calc.py", line 43, in func1
return func2(a+1)
File "/home/alessandro/git-files/python/new_calc/app/new_calc.py", line 46, in func2
return func3(b+1)
File "/home/alessandro/git-files/python/new_calc/app/new_calc.py", line 49, in func3
return c/0
ZeroDivisionError: integer division or modulo by zero
Process finished with exit code 1
在python中配置backtrace
import backtrace
backtrace.hook( # this is to print the backtrace in a more practical way (doesn't work in pycharm, alas)
reverse=False,
align=True,
strip_path=False,
enable_on_envvar_only=False,
on_tty=False,
conservative=False,
styles={})
它读起来更好,恕我直言:
Traceback (Most recent call last):
49 /home/alessandro/git-files/python/new_calc/app/new_calc.py <module> --> print(func1(1))
41 /home/alessandro/git-files/python/new_calc/app/new_calc.py func1 --> return func2(a+1)
44 /home/alessandro/git-files/python/new_calc/app/new_calc.py func2 --> return func3(b+1)
47 /home/alessandro/git-files/python/new_calc/app/new_calc.py func3 --> return c/0
ZeroDivisionError: integer division or modulo by zero
Process finished with exit code 1
如何配置PyCharm以解码回溯跟踪的文件名和行号?否则,无法通过单击跟踪线来跳转到代码。