使用PyCharm调试Popen子流程 [英] Debugging Popen subprocesses with PyCharm
问题描述
我正在尝试调试使用psutil.Popen对象的Python应用程序.当我启动一个子进程时,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>
最终会导致错误:
python.exe: Import by filename is not supported.
当我启动不带-m
选项的相同命令时,一切似乎都很好.有什么方法可以更改PyCharm的调试器启动命令?
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?
我已更新到PyCharm Community Edition 4.0.3,新的调试器命令如下:
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
其中,-c
和-f
是模块的命令行参数.调试器启动命令已更改,但是并不能解决问题.我仍然收到Import by filename is not supported
错误.
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.
此处位于Bitbucket.org 处提供了代码示例. Pycharm的运行配置应如下所示:
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%
推荐答案
如Piotr所述,PyCharm'在调试时自动附加到子进程'.如果子进程是Python进程,则PyCharm调试器会更改该进程的启动参数(请参见函数patch_args
,位于
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)
实际的启动命令如下:
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:
-
最简单的方法,如果您不需要调试子流程,只需在PyCharm设置中关闭调试时自动附加到子流程"
easiest way, if you don't need to debug subprocess, just turn off "Attach to subprocess automatically while debugging" inside PyCharm settings
将您的args更改为:
change your args to:
args = ['python', '__main__.py', 'worker']
缺点是您只能运行Python文件,而不能运行Python模块.
The disadvantage is you can only run a Python file, not a Python module.
我建议使用Python子进程的最后一个解决方案:
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()
这篇关于使用PyCharm调试Popen子流程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!