简体   繁体   English

Intellij / Pycharm无法调试Python模块

[英]Intellij/Pycharm can't debug Python modules

I use PyCharm / IntelliJ community editions from a wile to write and debug Python scripts, but now I'm trying to debug a Python module , and PyCharm does a wrong command line instruction parsing, causing an execution error, or maybe I'm making a bad configuration. 我使用PyCharm / IntelliJ社区版本来编写和调试Python脚本,但现在我正在尝试调试Python模块 ,而PyCharm执行错误的命令行指令解析,导致执行错误,或者我正在制作配置错误。

This is my run/debug configuration: 这是我的运行/调试配置:

IntelliJ运行/调试Python模块配置

And this is executed when I run the module (no problems here): 这是在我运行模块时执行的(这里没有问题):

/usr/bin/python3.4 -m histraw

But when I debug, this is the output in the IntelliJ console: 但是当我调试时,这是IntelliJ控制台中的输出:

/usr/bin/python3.4 -m /opt/apps/pycharm/helpers/pydev/pydevd.py --multiproc --client 127.0.0.1 --port 57851 --file histraw
/usr/bin/python3.4: Error while finding spec for '/opt/apps/pycharm/helpers/pydev/pydevd.py' (<class 'ImportError'>: No module named '/opt/apps/pycharm/helpers/pydev/pydevd')

Process finished with exit code 1

As you can see, the parameters are wrong parsed, and after -m option a IntelliJ debug script is passed before the module name. 如您所见,参数分析错误,在-m选项之后,IntelliJ调试脚本在模块名称之前传递。

I also tried just put -m histraw in the Script field, but doesn't work, that field is only to put Python script paths, not modules. 我也试过把-m histraw放在Script字段中,但不起作用,该字段只是放置Python脚本路径,而不是模块。

Any ideas? 有任何想法吗?

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 . 还有另一种方法可以使它工作。你可以编写一个python脚本来运行你的模块。然后只需配置PyCharm来运行这个脚本

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. 我发现使用以下内容创建引导程序文件(debuglaunch.py​​)最简单。

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: 例如,要在pycharm调试器中启动locustio,我创建了debuglaunch.py​​,如下所示:

from locust import main

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

And configured pycharm as follows. 并配置pycharm如下。

pycharm_debug_config

NOTE: I found I was not able to break into the debugger unless I added a breakpoint on main.main() . 注意:除非我在main.main()上添加了断点,否则我发现无法进入调试器。 That may be specific to locustio, however. 然而,这可能是特定于locustio。

In PyCharm 2019.1 (professional), I'm able to select run as module option under configurations, as below 在PyCharm 2019.1(专业)中,我可以在配置下选择run as module option,如下所示

在此输入图像描述

The problem is already fixed since PyCharm 4.5.2. 自PyCharm 4.5.2以来,这个问题已经解决了。 See corresponding issue in PyCharm tracker: https://youtrack.jetbrains.com/issue/PY-15230 请参阅PyCharm跟踪器中的相应问题: https ://youtrack.jetbrains.com/issue/PY-15230

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

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