简体   繁体   English

如何在 Pycharm 中使用 Popen 调试代码运行

[英]How to debug code run using Popen in Pycharm

I am running a codebase I have inherited from someone else which makes extensive use of user input.我正在运行一个从其他人那里继承的代码库,它广泛使用了用户输入。 For this reason I am running it using subprocess.Popen .出于这个原因,我使用subprocess.Popen运行它。 Here is an example.这是一个例子。 The following script ( caller.py ) calls the third-party code.以下脚本 ( caller.py ) 调用第三方代码。

from subprocess import Popen, PIPE, STDOUT
import sys
user_input = ['John', '555']

communicate_argument = '\n'.join(user_input)
p = Popen([sys.executable, 'example2.py'], stdout=PIPE, stdin=PIPE, stderr=STDOUT, encoding='utf-8')

stdout, stderr = p.communicate(communicate_argument)

print(stdout)

The following script ( example.py ) emulates behavior of the source code I was provided, by accepting a couple of input arguments from the user:以下脚本 ( example.py ) 通过接受来自用户的几个输入 arguments 来模拟我提供的源代码的行为:

name = input('What is your name\n')
age = input('What is your age\n')

print('You are {}, and you are {} years old'.format(name, age))

Running the code works fine, and I get the expected output.运行代码工作正常,我得到了预期的 output。

Debugging the code partially works, but partially doesn't.调试代码部分有效,但部分无效。 The debugger successfully attaches to the child process p such that any breakpoints placed in example.py will work.调试器成功附加到子进程p以便放置在example.py中的任何断点都可以工作。 However it seems that the debug console does not successfully attached to the child process.但是,调试控制台似乎没有成功附加到子进程。 When I try to enter some variables in the debug console, they do not print out, even if they appear as active variables in my debug session.当我尝试在调试控制台中输入一些变量时,即使它们在我的调试 session 中显示为活动变量,它们也不会打印出来。

EDIT编辑

It turns out this might be a bug.事实证明这可能是一个错误。 I have asked the same question in pycharm's official forum and they made an issue out of it:我在 pycharm 的官方论坛上问过同样的问题,他们提出了一个问题:

https://intellij-support.jetbrains.com/hc/en-us/community/posts/360009571680-Debugging-code-run-through-subprocess-Popen https://intellij-support.jetbrains.com/hc/en-us/community/posts/360009571680-Debugging-code-run-through-subprocess-Popen

So I guess what I'd be looking for is an effective workaround which would allow me to use a regular python interpreter in combination with the debugger to inspect and run operations on variables.所以我想我要寻找的是一种有效的解决方法,它允许我使用常规的 python 解释器和调试器来检查和运行变量操作。

I have had a similar issue with python and docker logs.我对 python 和 docker 日志有类似的问题。 What solved was running python with the -u flag as described here .解决的问题是使用 -u 标志运行 python ,如此所述。 Perhaps try changing your Popen call to use python and then include the -u flag.也许尝试更改您的 Popen 调用以使用 python 然后包含 -u 标志。 eg Popen(["python", "-u", "example2.py"], stdout=PIPE, stdin=PIPE, stderr=STDOUT, encoding='utf-8')例如Popen(["python", "-u", "example2.py"], stdout=PIPE, stdin=PIPE, stderr=STDOUT, encoding='utf-8')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM