12

Expect is a module used for spawning child applications and controlling them. I'm interested in Python and Ruby.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Geo
  • 93,257
  • 117
  • 344
  • 520

6 Answers6

17

There is WExpect for Python.

Notes in the wexpect.py file (typos unchanged and highlighting added)

Wexpect is a port of pexpext to Windows. Since python for Windows lacks the requisite modules (pty, tty, select, termios, fctnl, and resource) to run pexpect, it was necessary to create a back-end that implemented any functions that were used that relied on these modules. Wtty.py is this back-end. In the Windows world consoles are not homogeneous. They can use low level or high level input and output functions, and to correctly deal with both cases two child processes are created for instacne of Spawn, with an intermidate child that can continuously read from the console, and send that data over a pipe to an instance of wtty. Spawner.py is resposible from reading and piping data.

I've left as much code intact as I could and also tried to leave as many comments intact is possible (espicially for functions that have not been changed) so many of the comments will be misleading in their relationship to os specific functionality. Also, the functions sendcontrol and sendeof are unimplemnted at this time, as I could not find meaningful Windows versions of these functions.
additionally, consoles do not have associated fild descriptors on Windows, so the global variable child_fd will always be None.

Post169
  • 668
  • 8
  • 26
nik
  • 13,254
  • 3
  • 41
  • 57
  • @Symmetric: you've changed the link to point to [`winpexpect`](http://bitbucket.org/geertj/winpexpect). But [`wexpect`](http://sage.math.washington.edu/home/goreckc/sage/wexpect/) is a different module. – jfs Jul 24 '12 at 14:42
  • @J.F.Sebastian: good spot, those old links were broken so I wasn't able to find much about wexpect. Fixed. – Symmetric Jul 25 '12 at 19:09
6

winpexpect is a native port of pexpect to Windows. It can be found here:

https://github.com/geertj/winpexpect

Christopher Bottoms
  • 11,218
  • 8
  • 50
  • 99
Geert
  • 151
  • 2
  • 1
4

You can use the Windows CMD prompt.

You need to have Python installed in your Windows installation.

Open a cmd prompt and execute the below command:

C:\Users\xxx>pip install pexpect (if you have set the Python path in a system variable)

or

C:\Users\xxx>c:\python27\scripts\pip.exe install pexpect

Collecting pexpect
  Downloading pexpect-4.0.1.tar.gz (143kB)
    100% |################################| 147kB 1.2MB/s
Collecting ptyprocess>=0.5 (from pexpect)
  Downloading ptyprocess-0.5.1-py2.py3-none-any.whl
Building wheels for collected packages: pexpect
  Running setup.py bdist_wheel for pexpect
  Stored in directory: C:\Users\xxx\AppData\Local\pip\Cache\wheels\f2\65\89\09578bcd0efeabc7e2b0079cd62d3955c1477f2e55aa5031a4
Successfully built pexpect
Installing collected packages: ptyprocess, pexpect
Successfully installed pexpect-4.0.1 ptyprocess-0.5.1
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Aravind
  • 362
  • 5
  • 11
  • 1
    You can install it but note, that there is no native support for windows. There are issues around windows usage: https://pexpect.readthedocs.io/en/stable/overview.html#windows – betontalpfa Apr 18 '19 at 11:20
2

Use pexpect https://github.com/pexpect/pexpect

"Pexpect is pure Python" so it will run anywhere, without Cygwin too,

edit: pexpect depends on pty module which is currently available only for Linux, so as Nik suggested you should be using wexpect which is a port of pexpect

Christopher Bottoms
  • 11,218
  • 8
  • 50
  • 99
Anurag Uniyal
  • 85,954
  • 40
  • 175
  • 219
2

The latest working version of wexpect lives at http://sage.math.washington.edu/home/goreckc/sage/wexpect/

Hopefully it will be merged upstream soon.

ckg
  • 204
  • 3
  • 10
0

I've successfully used Pexpect under Cygwin. For now there is no other way due to POSIX compatibility problems under Windows.

Another thing: WExpect works like Pexpect, in fact it requires Cygwin! At this point, PExpect is a better choice.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Fabio T.
  • 1
  • 2