1

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 following script (caller.py) calls the third-party 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 following script (example.py) emulates behavior of the source code I was provided, by accepting a couple of input arguments from the user:

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.

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. 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.

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:

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.

user32882
  • 5,094
  • 5
  • 43
  • 82
  • Note that we prefer a technical style of writing here. We gently discourage greetings, hope-you-can-helps, thanks, advance thanks, notes of appreciation, regards, kind regards, signatures, please-can-you-helps, chatty material and abbreviated txtspk, pleading, how long you've been stuck, voting advice, meta commentary, etc. Just explain your problem, and show what you've tried, what you expected, and what actually happened. – halfer Aug 25 '20 at 18:12
  • Hi user32882. Please note that I have this conversation a few times a week - to the best of my knowledge, technical writing is the preferred format here, and (importantly) moderators tend to side with that view. For what it's worth, I tend to agree with it, as I think it makes posts succinct and easier to read as future reference material. I can point you to some _Meta_ posts where it has been discussed, if you like? – halfer Aug 25 '20 at 18:21
  • 1
    Sure id be curious to see what was discussed – user32882 Aug 25 '20 at 19:23
  • Great! See [here](https://meta.stackoverflow.com/q/260776) and [here](https://meta.stackoverflow.com/q/266525), both of which are on the Stack Overflow Meta. There is some general [editing advice here](https://meta.stackoverflow.com/a/303220), including advice about removing "noise". On the overall Stack Exchange Meta, the canonical is probably [here](https://meta.stackexchange.com/q/2950). Some advice on [mutual editing is here](https://stackoverflow.com/help/editing). – halfer Aug 25 '20 at 20:03

1 Answers1

-1

I have had a similar issue with python and docker logs. What solved was running python with the -u flag as described here. Perhaps try changing your Popen call to use python and then include the -u flag. e.g. Popen(["python", "-u", "example2.py"], stdout=PIPE, stdin=PIPE, stderr=STDOUT, encoding='utf-8')

ekmcd
  • 172
  • 8