Comments (6)
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.
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.
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.
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.
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.
@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)
- Release v2.0.3
- ConPTY is not fully working in CI HOT 4
- Build failure on windows py3.6 HOT 1
- Release v2.0.4
- Release v2.0.5
- Async read?
- Release v2.0.6
- Release v2.0.7
- Garbled output on Telnet Client HOT 10
- Release v2.0.8
- Failed to install with python 3.11 HOT 2
- 'Pty is closed.' BUG
- Release v2.0.10
- ssh connection establish failed.
- [Question] Messed up display buffer after resize? HOT 2
- Question: Does `PTY.write()` take in unicode string or bytes?
- PtyProcess.spawn cwd parameter not working HOT 4
- Freeze on process.iseof() HOT 7
- Problem calling an executable with full path HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
š Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ā¤ļø Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pywinpty.