Intellij/Pycharm can't debug Python modules

Mariano Ruiz picture Mariano Ruiz · Mar 10, 2015 · Viewed 9k times · Source

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?

Answer

xcodebuild picture xcodebuild · Jul 28, 2015

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.