Intellij/Pycharm can't debug Python modules

强颜欢笑 提交于 2019-11-30 06:42:41
codefalling

There is another way to make it work.You can write a python script to run your module.Then just configure PyCharm to run this script.

import sys
import os
import runpy
path = os.path.dirname(sys.modules[__name__].__file__)
path = os.path.join(path, '..')
sys.path.insert(0, path)
runpy.run_module('<your module name>', run_name="__main__",alter_sys=True)

Then the debugger works.

I found it easiest to create a bootstrap file (debuglaunch.py) with the following contents.

from {package} import {file with __main__}

if __name__ == '__main__':
    {file with __main__}.main()

For example, to launch locustio in the pycharm debugger, I created debuglaunch.py like this:

from locust import main

if __name__ == '__main__':
    main.main()

And configured pycharm as follows.

NOTE: I found I was not able to break into the debugger unless I added a breakpoint on main.main() . That may be specific to locustio, however.

In PyCharm 2019.1 (professional), I'm able to select run as module option under configurations, as below

The problem is already fixed since PyCharm 4.5.2. See corresponding issue in PyCharm tracker: https://youtrack.jetbrains.com/issue/PY-15230

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!