Giter VIP home page Giter VIP logo

Comments (6)

scastria avatar scastria commented on August 15, 2024 1

Thank you!! I could swear I tried all combinations of the subprocess module and nothing worked. But sure enough, when I try subprocess.run now, it works on both Mac and Windows. On Windows Command Prompt, it works without any extra work. But when installing GIT on Windows, you also get the GIT bash terminal. When using the GIT bash terminal on Windows, it doesn't work out of the box. I have to prefix the command with winpty and then it works in GIT bash too.

I knew about winpty and tried it as well before, but now it seems to be working.

Thanks for helping me with this.

from pywinpty.

andfoy avatar andfoy commented on August 15, 2024

Hi @scastria, the purpose of pywinpty is to spawn a pseudoterminal whose I/O streams are hidden, which given your description makes it sound like the library it is not working for you, could you please provide a GIF and also a script snippet that describes the behaviour you are seeing?

from pywinpty.

scastria avatar scastria commented on August 15, 2024

Iā€™m not sure I can add any more than what I already posted. With the pty library on Linux, I can call pty.spawn and it will behave just like I ran that command in a real Linux shell. I believe if I call the spawn here in this library, the spawn call will immediately return control to the Python program and exit. I believe this library requires me to go into an infinite loop asking if the process is still alive, etc.

from pywinpty.

andfoy avatar andfoy commented on August 15, 2024

Sorry, I think I didn't understand you completely. So, just to be on the same page, you want to spawn an interactive session on top of your current terminal session and interact with it? If that's the case, then this library doesn't make much sense to you, pywinpty is intended to spawn and communicate with pseudoterminals that are detached from a calling console.

I would suggest you to just call your application using subprocess.Popen instead as it will inherit the same IO handles as the parent console, thus causing your program to "appear" interactively on top of your current session.

I'll close this issue, feel free to open it again if you have further questions!

from pywinpty.

scastria avatar scastria commented on August 15, 2024

Just so I am clear, I started off by writing this code:

import pty
import shlex

command = shlex.split("docker run -it --rm ubuntu bash")
pty.spawn(command)

and it worked beautifully on my Mac. The above python script worked just as if had run the docker command directly. I essentially get an interactive shell where I can type commands and get their output. However, the above python script does not work on Windows as I get the "ModuleNotFoundError: No module named 'termios'" error.

I then search the Internet and pywinpty comes up as a solution for windows. So then I write this code:

from winpty import PtyProcess

PtyProcess.spawn("docker run -it --rm ubuntu bash")

I don't get the same behavior. It seems I need to do more work like reading from stdin and relaying it to the PtyProcess, read from PtyProcess and relay to stdout, and do this in an infinite loop to prevent the python script from exiting until the PtyProcess has exited.

If this is correct, could someone help me with that? I don't see any docs on this library.

from pywinpty.

andfoy avatar andfoy commented on August 15, 2024

@scastria, thanks for describing your use case. Regarding pywinpty, it is intended to use as a "behind the scenes" pseudoterminal spawner in windows, which are used to spawn programs inside a terminal and then relay the input and output to another application or library that requires it. That's why it requires a fine control over the output and input, as well as the lifecycle of the process that is being run.

Given that you want to interact with the spawned process inside your current terminal session, it is best to use subprocess.run, which allows you to spawn a process and communicate with it. You can use it as follows:

import subprocess

subprocess.run(['docker', 'run', -'it', '--rm', 'ubuntu', 'bash'])

from pywinpty.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    šŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. šŸ“ŠšŸ“ˆšŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ā¤ļø Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.