Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debugging Popen subprocesses with PyCharm

I'm trying to debug a Python application that uses psutil.Popen objects. When I start a subprocess, PyCharm replaces my command line with the following:

python -m pydevd.py --multiproc --client 127.0.0.1 --port 52581 --file <myapplication>

which ends up in an error:

python.exe: Import by filename is not supported.

When I launch the same command without -m option, everything seems to be fine. Is there a way I can change PyCharm's debugger launch command?

I've updated to PyCharm Community Edition 4.0.3 and the new debugger command looks like:

python.exe "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 4.0.3\helpers\pydev\pydevd.py" 
--multiproc --client 127.0.0.1 --port 62661 
--file __main__.py local -c local.yml -f input/11_12.xls

where -c and -f are my module's command line arguments. The debugger launch command has changed, but it didn't solve the issue; I still get the Import by filename is not supported error.

A code example is available here at Bitbucket.org. Pycharm's run configuration should look like:

Script:            __main__.py
Script parameters: server
Working directory: %path to the repository%
like image 854
Ivan Gromov Avatar asked Oct 20 '14 12:10

Ivan Gromov


People also ask

Can we Debug in PyCharm?

Starting a debugger session is very similar to running the program in normal mode. The debugger is attached behind the scenes, so you don't have to configure anything specific to start a debugger session. If you are able to run your program from PyCharm, you will also be able to debug it using the same configuration.

How do I create a run Debug configuration in PyCharm?

Add Run/Debug configurations to the Services windowSelect View | Tool Windows | Services from the main menu or press Alt+8 . In the Services tool window, click Add service, then select Run Configuration Type. Select a run/debug configuration type from the list to add all configurations of this type to the window.


1 Answers

As Piotr mentioned, PyCharm 'Attach to subprocess automatically while debugging'. If subprocess is a Python process, PyCharm debugger change the process's startup arguments (see function patch_args at source). When you start subprocess in this way:

args = ['python',
        '-m', 'pycharm-multiprocess-debug',
        'worker']
worker = subprocess.Popen(args)

The actual startup command is like:

python.exe -m "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 4.0.3\helpers\pydev\pydevd.py"
--multiproc --client 127.0.0.1 --port 62661
--file pycharm-multiprocess-debug

So it went wrong. There are several workarounds I can find:

  1. easiest way, if you don't need to debug subprocess, just turn off "Attach to subprocess automatically while debugging" inside PyCharm settings

  2. change your args to:

    args = ['python', '__main__.py', 'worker']
    

    The disadvantage is you can only run a Python file, not a Python module.

  3. I recommend the last solution for Python subprocess:

    from multiprocessing import Process
    
    def server():
        p = Process(target=worker)
        p.start()
        print 'worker pid: {}'.format(p.pid)
        p.join()
    
like image 72
ZZY Avatar answered Sep 17 '22 12:09

ZZY