Skip to main content

yet another winpty binding for python

Project description

yet another winpty binding for python

Build status LICENSE PyPI version Development status Download per month wheel Support python versions Codecov


pip install yawinpty

build from source

python 3.5+
install Visual C++ 2015 Build Tools, then use python build to build
older python
Visual C++ CPython version
10.0 3.3, 3.4
9.0 2.6, 2.7, 3.0, 3.1, 3.2

install both Visual C++ 2015 Build Tools and the matching version of Visual C++ Build Tools. open “Visual C++ 2015 Build Tools Command Prompt” with the same arch as python, then use python build to build

basic examples

get output from process

from yawinpty import *

# open a pty
with Pty() as pty:
    # spawn a process ``python -c "print('HelloWorld!')"``
    pty.spawn(SpawnConfig(SpawnConfig.flag.auto_shutdown, cmdline='python -c "print(\'HelloWorld!\')"'))
    # open the out pipe of console to read
    with open(pty.conout_name(), 'r') as f:
        # HelloWorld!

communicate with process

from yawinpty import *

with Pty() as pty:
    # spawn python repl
    pty.spawn(SpawnConfig(SpawnConfig.flag.auto_shutdown, cmdline='python'))
    # open the in pipe of console to write
    with open(pty.conin_name(), 'w') as f:
        f.write('1 + 2\n')
        # write EOF to exit python
    with open(pty.conout_name(), 'r') as f:

gui log of console program (navie)

from sys import argv
from threading import Thread
from subprocess import list2cmdline
from tkinter import *
from yawinpty import *

root = Tk()
con = Text(root)

def poll():
    # strip escape seq
    with Pty(Config(Config.flag.plain_output)) as pty:
        # run the cmdline passed in by ``sys.argv``
        pty.spawn(SpawnConfig(SpawnConfig.flag.auto_shutdown, cmdline=list2cmdline(argv[1:])))
        with open(pty.conout_name(), 'r') as f:
            while True:
                ln = f.readline()
                if not ln:
                # log to gui
                con.insert(END, ln)


using yawinpty

the common goal to use yawinpty is to open a pseudo terminal then spawn a process in it and send input to it’s stdin and get output from it’s stdout. yawinpty.Pty wrapper a pseudo-terminal and do the jobs

class yawinpty.Pty(config=yawinpty.Config())

yawinpty.Pty accept a instance of yawinpty.Config as its config

class yawinpty.Config(*flags)

for the flags to init a “config class” is commonly a set of Class.flag.*. example:

cfg = yawinpty.Config(yawinpty.Config.flag.plain_output)

help(yawinpty.Config.flag) for more supported flags

for yawinpty.SpawnConfig it’s similar

help(yawinpty.Config) for more methods

instances of the Pty class have the following methods:




get the name of console in/out/err pipe. the name could be passed to builtin open to open the pipe


get the process id of the agent process


set window size of the terminal


spawn a process in the pty. spawn_config is a instance of yawinpty.SpawnConfig. note that one Pty instance could only spawn once otherwise yawinpty.RespawnError would be raised

returns a tuple of process id, thread id of spawned process

class yawinpty.SpawnConfig(*spawnFlags, appname=None, cmdline=None, cwd=None, env=None)

the flags from yawinpty.SpawnConfig.flag
full path to executable file. can be None if cmdline is specified
command line passed to the spawned process
working directory for the spawned process
the environ for the spawned process, a dict like {'VAR1': 'VAL1', 'VAR2': 'VAL2'}

note that init a SpawnConfig does not spawn a process. a process is spawned only when calling Pty.spawn(). one SpawnConfig instance could be used multitimes

Pty.wait_agent(timeout = yawinpty.INFINITE)

Pty.wait_subprocess(timeout = yawinpty.INFINITE)

wait for agent/spawned process to exit. raise yawinpty.TimeoutExpired if out of timeout


kill processes not exited, close pty and release Windows resource


all winpty related exceptions are subclasses of yawinpty.YawinptyError. help(yawinpty) for more information

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

yawinpty-0.4.3.tar.gz (214.7 kB view hashes)

Uploaded source

Built Distributions

yawinpty-0.4.3-cp36-cp36m-win_amd64.whl (328.5 kB view hashes)

Uploaded cp36

yawinpty-0.4.3-cp36-cp36m-win32.whl (281.0 kB view hashes)

Uploaded cp36

yawinpty-0.4.3-cp35-cp35m-win_amd64.whl (326.3 kB view hashes)

Uploaded cp35

yawinpty-0.4.3-cp35-cp35m-win32.whl (279.7 kB view hashes)

Uploaded cp35

yawinpty-0.4.3-cp34-cp34m-win_amd64.whl (366.3 kB view hashes)

Uploaded cp34

yawinpty-0.4.3-cp34-cp34m-win32.whl (295.1 kB view hashes)

Uploaded cp34

yawinpty-0.4.3-cp27-cp27m-win_amd64.whl (342.4 kB view hashes)

Uploaded cp27

yawinpty-0.4.3-cp27-cp27m-win32.whl (299.2 kB view hashes)

Uploaded cp27

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page