Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

This is my run/debug configuration:

IntelliJ run/debug Python module configuration

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:

/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.

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.

Any ideas?

like image 548
Mariano Ruiz Avatar asked Mar 10 '15 03:03

Mariano Ruiz


People also ask

Why my debugger is not working in IntelliJ?

If the code is outdated, or the versions (the source code and the compiled class) mismatch in any way, it can happen that the debugged is giving the IDE information to show a certain line, but that information is not correct giving the current source code, which might cause what appears to be the debugged "jumping" ...

Why does PyCharm not Debug?

Faced the same issue and even tried different PyCharm versions without success. In the end the reason for debugging console not working was that somewhere in the repo sys. out and sys. stderr were being overwritten.

Can IntelliJ Debug Python?

Python Debugger SettingsUltimateIf this checkbox is selected, IntelliJ IDEA will automatically attach all subprocesses of the process being debugged. Thus, if the parent process has subprocesses, their breakpoints will always work.


2 Answers

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.

like image 76
xcodebuild Avatar answered Sep 21 '22 05:09

xcodebuild


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

enter image description here

like image 28
Yuri Feldman Avatar answered Sep 22 '22 05:09

Yuri Feldman