18
votes

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?

4
What exactly are you trying to achieve by running your script using the -m parameter and not directly? As far as I understand the -m switch was designed for making it easy to run standard library modules; it brings no benefit for your own scripts. - yole
Because I am writing a distributable command line script, and it's installable in your system PATH as a executable command line tool, with setuptools and pip tools. When you write a real command line in Python, setuptools install it in your system environment with a new standalone script, and this script calls your original script as a module. This can be look like harmless when code is running, but isn't, because the "enviroment" changes, and some parts of your code can react different, specially modules import statements, or calls to sys.* package, etc. - Mariano Ruiz
So why don't you create a copy of a script that setuptools would create, and run that script from a PyCharm run configuration? - yole
Maybe works, but it's a basic requirement for any IDE the capability to run and debug programs without the need to change the way the programs run. Furthermore, add this capability to PyCharm looks so simple like change the execution parameters orders when it's debugging. Anyway thanks, I will test your “hack” soon. - Mariano Ruiz
There is indeed nothing difficult in fixing PyCharm. However, it's in any case faster to find a workaround that will let you continue your work than to release an updated version of PyCharm that contains the fix. - yole

4 Answers

22
votes

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.

4
votes

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

enter image description here

2
votes

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.

pycharm_debug_config

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.

-2
votes

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