1

I am trying to get my python app to start a command prompt session and run commands in it. I have been successful in getting it to start a cmd session, but I cannot make it run commands in the newly created window (nor any other open window). It only runs like in this picture: enter image description here

1 Answers1

1

I am trying to get my python app to start a command prompt session and run commands in it.

If I understand you correct, you're trying to send commands to a shell like it was typed on the keyboard.

To achieve that, you need

  • start the shell process
  • determine input and output streams of the shell process
  • write and read some data to that streams

I'm not familiar with CMD shell and Windows, but here's the example how it could be done on linux with ZSH:

In [48]: import subprocess, time
    ...:
    ...:
    ...: def main():
    ...:     with subprocess.Popen(['/usr/bin/zsh'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, bufsize=1) as proc:
    ...:         proc.encoding = 'utf-8'
    ...:         proc.text_mode = True
    ...:         proc.universal_newlines = True
    ...:         COMMAND = 'echo "Hello From Python! Current shell is \"$SHELL\"!"\r\n'
    ...:
    ...:         print('Sending command:', proc.pid, '\n<<<', repr(COMMAND))
    ...:
    ...:         proc.stdin.writelines([COMMAND.encode()])
    ...:         proc.stdin.flush()
    ...:
    ...:         print('Receiving outputs:', proc.pid, '\n>>>', end=' ')
    ...:         raw = proc.stdout.readline()
    ...:         res = raw.decode()
    ...:         print(repr(res))
    ...:
    ...:         proc.terminate()
    ...:
    ...: main()
Sending command: 1250722
<<< 'echo "Hello From Python! Current shell is "$SHELL"!"\r\n'
Receiving outputs: 1250722
>>> 'Hello From Python! Current shell is /usr/bin/zsh!\r\n'

If you need to go deeper, please take a look at this answer: Understanding Popen.communicate

madbird
  • 1,326
  • 7
  • 11
  • Hi, I got most of it to work, using the solution I found here: https://stackoverflow.com/questions/39716557/how-to-use-subprocess-popen-with-built-in-command-on-windows#39717215 It shows output but it doesn't allow the user to type anything and I want to allow that. Edit: It has to do with the fact that I am using a kivy app so the problem resides there. Thanks for your help! – GetTwoBirdsStonedAtOnce Apr 26 '21 at 08:59
  • Oh, I got it. @GetTwoBirdsStonedAtOnce, if you need to start cmd with initial setup, and setup script is not seems to be heavily dependent on python variables, I could suggest using BAT file for starting CMD and setting up colors there, rather than doing that from python though. And to bind the started process directly to a parent's terminal, just remove `stdin=PIPE` and `stdout=PIPE`. – madbird Apr 26 '21 at 09:18
  • I will try it, but the thing I am trying to accomplish is to let the user write in the cmd and if necessary click on a button in the app and make it run the command that is bound to the button in the window that user is writing in. Hope I explained it clearly. – GetTwoBirdsStonedAtOnce Apr 26 '21 at 09:39