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)JetBrainsPyCharm Community Edition 4.0.3helperspydevpydevd.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%

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)JetBrainsPyCharm Community Edition 4.0.3helperspydevpydevd.py"
--multiproc --client 127.0.0.1 --port 62661
--file pycharm-multiprocess-debug

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

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

  • change your args to:

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

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

  • 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()
    
  • 链接地址: http://www.djcxy.com/p/44948.html

    上一篇: PyCharm:在Django模板中调试Javascript

    下一篇: 用PyCharm调试Popen子进程