Debugging code run through subprocess.Popen
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`. Here is an example. The script `caller.py` contains the following code:
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 script example.py contains the following code
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))
When I try to debug, pycharm successfully stops at any breakpoints I set in example.py and I can see the variables and their values shown in the debugger.
Unfortunately, this is not the case for the debug console. When I try to inspect any of the variables in example.py I get no value.
I have tried the following:
- deactivating the "Attach to subprocess automatically while debugging" under Build/Execution/Deployment in the Python Debugger
- activating "Run with Python Console" in my Run/Debug Configurations
Neither of these solutions has allowed me to inspect the values in the debug console. How can I achieve this?
Thanks in advance for your help.
请先登录再写评论。
Would anyone please help? I have no idea how to solve this problem.
Hello,
Thank you for reporting this, I could easily reproduce and report it as a bug https://youtrack.jetbrains.com/issue/PY-44170 , please vote for it in order to be updated on the resolution.
Apologies for the inconvenience.
The bug report mentions `main.py` and `example.py` but these are not attached anywhere.
Is there a workaround for this? I use the debug console quite a lot to inspect and operate on variables while I'm debugging. It's one of the main reasons I really like to work with PyCharm.
I just renamed it for my convenience.
At this moment, there is no workaround, please follow the ticket for any updated from the assigned developer.
Unfortunately that means I will have to switch to VSCode until this is resolved...