run an executable using wexpect

6.6k Views Asked by At

I have an executable (evapo.exe) which has to be called with an input file (inputs.prj), usually I do it using windows command prompt by just typing c:\myfiles\evapo inputs.prj (of course both the executable and input file located in myfiles folder)

Now I want to be able to do the same thing using python. Other similar questions here on SO suggested to use wexpect since other methods such as subprocess do not work when the executable askes for other informations to run (for my case the information being the input file). i tried:

import wexpect

wexpect.run(r'c:\myfiles\evapo.exe')

and python hangs..., please help me if anyone has an idea how i can combine the exe and input file as I do it using cmd.

3

There are 3 best solutions below

0
jfs On BEST ANSWER

You don't need wexpect if all information you want to pass is a file name:

from subprocess import check_call

check_call(r'c:\myfiles\evapo.exe inputs.prj')
3
pyfunc On

I guess wexpect is a python port of pexpect and behaves similarly but works on windows.

I checked on the following: http://www.jjoseph.org/files/led_calibration/wexpect.py

If that is the case then any of the examples for pexpect should work.

run() command should be used when you just want to collect output. This does not work when you want to interact and provide inputs.

if you want to send the inputs, you will need to use spawn() function.

See the example in the code. Here the command asks for an input which is sent across by wexpect

child = wexpect.spawn('some command')
child.expect ('Password:')
child.sendline (mypassword)

Just go through following answers on pexpect and substitute it with wexpect.

And also for windows following port has been suggested as working one:

0
Prasad Nurani On

I was able to make it work. You need to download wexpect from github and follow the instruction on wiki on how to build it.

https://github.com/raczben/wexpect/wiki/Wexpect-with-pyinstaller#how-to-use-wexpect-with-pyinstaller

once you are done with building of wexpect, to create one exe click the link below and follow the instruction

https://github.com/raczben/wexpect/issues/12#issuecomment-605390122