Wexpect in a Python Virtual Environment

299 Views Asked by At

When run in cmd window, the spawn command works fine, but if in a virtual environment, it hangs forever.

This works

C:\Users\csullivan\Documents\Projects\netlog>python
Python 3.9.5 (tags/v3.9.5:0a7dcbd, May  3 2021, 17:27:52) [MSC v.1928 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from wexpect import spawn
>>> spawn("cmd.exe")
<wexpect.host.SpawnPipe object at 0x0000019F7040DBB0>
>>>

but this does not return from spawn.

(env) C:\Users\csullivan\Documents\Projects\netlog>python
Python 3.9.5 (tags/v3.9.5:0a7dcbd, May  3 2021, 17:27:52) [MSC v.1928 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from wexpect import spawn
>>> spawn("cmd.exe")

I expected or at least hoped for identical behaviour in a Python Virtual Environment if software versions are the same, which they are.

Environment

  • Microsoft Windows 10 Pro 10.0.19043 Build 19043
  • Dell Latitude 7490
  • Python 3.9.5
  • wexpect 4.0.0

Is there anything special about running from a virtualenv that could be relevant? I started this exercise for testing an interactive C program with unittest using subprocess, and it worked until I needed to actually interact with it. Then I tried pexpect. That at least did not hang on Popen_spawn but did later despite using timeout=1 on reads.

If this isn't going to work, is there perhaps a C API that has similar functionality or enough to provide read/write functions to a command window or directly with the application?

1

There are 1 best solutions below

0
Deepstop On

This is a known issue with wexpect. Initially reported only with venv, some changes to virtualenv to align it with venv have triggered the problem, although the behaviour is not identical. Although there is a suggest patch, I've been able to achieve my objective by using subprocess directly and combining it with threading, despite some annoying quirks.