首页 > 解决方案 > 如何从 PyCharm 中的输出控制台跳转到源代码?

问题描述

寻找一种能够通过单击控制台中的某些输出行(类似于 eclipse 功能)来跳转到代码行的方法我当前的输出如下所示:

C:\anaconda3\python.exe C:/test/TestUnit.py 
2018-06-01 10:43:26,610 TestUnit.py              <module>() 8    INFO     TestUnit started, checking input params ...
2018-06-01 10:43:26,610 TestUnit.py              <module>() 27   INFO     Provided video file: [D:\input\video.mp4]
2018-06-01 10:43:26,613 Video.py                getFrames() 4    INFO     Extracting frames from input video D:\input\video.mp4

是否可以点击 getFrames() 并让 IDE 跳转到文件 video.py => getFrames() 函数中的相关代码行?

看起来像这样:

class Video(object):
 def getFrames(video,log):
   log.info("Extracting frames from input video " + video) <<====== jump to this

提前致谢 !

标签: pythonidepycharm

解决方案


为每个有同样问题的人回答这个老问题。

日志输出需要包含这部分:

File "/path/to/file.py", line 140

使用 Python 的日志记录模块,您可以通过

format="File \"%(pathname)s\", line %(lineno)d"

这是日志配置的完整示例

logging.basicConfig(level=10, format="[%(asctime)s] %(levelname)s [File \"%(pathname)s\", line %(lineno)d] %(message)s", datefmt="%H:%M:%S", stream=sys.stdout)

推荐阅读