Intellij/Pycharm can't debug Python modules

10,072

Solution 1

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.

Solution 2

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

enter image description here

Solution 3

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.

Share:
10,072

Related videos on Youtube

Mariano Ruiz
Author by

Mariano Ruiz

Software &amp; Web Developer | https://github.com/mrsarm

Updated on September 19, 2022

Comments

  • Mariano Ruiz
    Mariano Ruiz over 1 year

    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?

    • Mariano Ruiz
      Mariano Ruiz about 9 years
      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
      Mariano Ruiz about 9 years
      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
    Mariano Ruiz over 8 years
    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.
  • Reinderien
    Reinderien almost 8 years
    Further to that, the link mentioned here points to a ticket that has been reopened; so the problem isn't fixed.
  • toabi
    toabi almost 8 years
    This works pretty well, the sys.path modification are not even needed depending on your project layout and working directory setup.
  • slashdottir
    slashdottir over 7 years
    How and where do you configure PyCharm to run this script?
  • RyanLeiTaiwan
    RyanLeiTaiwan over 6 years
    [Run -> Edit Configurations] Script: The script above. Script parameters: All of your original parameters except "-m module_name".
  • Mariano Ruiz
    Mariano Ruiz about 5 years
    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
  • Mache
    Mache about 4 years
    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