18

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?

5
  • 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. Commented Mar 11, 2015 at 20:50
  • 1
    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. Commented Mar 12, 2015 at 3:07
  • So why don't you create a copy of a script that setuptools would create, and run that script from a PyCharm run configuration? Commented Mar 12, 2015 at 8:09
  • 1
    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. Commented Mar 12, 2015 at 12:05
  • 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. Commented Mar 12, 2015 at 16:13

4 Answers 4

22

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.

Sign up to request clarification or add additional context in comments.

4 Comments

This works pretty well, the sys.path modification are not even needed depending on your project layout and working directory setup.
How and where do you configure PyCharm to run this script?
[Run -> Edit Configurations] Script: The script above. Script parameters: All of your original parameters except "-m module_name".
this should be marked as the correct response; it's simple, it works I've tested it with Robot module and it's the first time in quite a while when I was able to debug
6

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

enter image description here

1 Comment

Yes, the company claims to have solved it since PyCharm 4.5.2 (without explaning how, just marking the issue as solved), but in reality I saw the problem solved since PyCharm 2018, and it's available in both versions: Community and Professional
2

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.

Comments

-2

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

2 Comments

If you don't comment or document how to use a new feature, it's like that feature doesn't exist. This is the case of that fix, I tried again with PayCharm 4.5.3, and I had the same issue, but I not sure if it was because the issue wasn't solved, or because there is a new special feature to debug Python modules, but that feature isn't documented to know how to use it.
Further to that, the link mentioned here points to a ticket that has been reopened; so the problem isn't fixed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.