1

I am working on a project were I open a .exe file. The file will open a terminal and I would like to input commands into it and save the output. The code I am working with right now is returning an error that a bytes-like object is required not 'str' for the ActiveEngine2.stdin.write("go depth 5").

import subprocess

SFPath = "stockfish_14.1_win_x64_avx2\stockfish_14.1_win_x64_avx2.exe"

ActiveEngine = subprocess.run([SFPath, "position startpos move e2e4 e7e5"], capture_output=True)
print(ActiveEngine.stdout.decode())
ActiveEngine2 = subprocess.Popen(SFPath, stdin=subprocess.PIPE, stdout=subprocess.PIPE)

ActiveEngine2.stdin.write("go depth 5")
print(ActiveEngine2.stdout.readline().strip())

I am able to open the file, but I can't figure out how to put input into it and, for that matter, get output from it. There is another post on Stack Overflow (Stockfish and Python) about this type of problem I have tried the code they use and it doesn't work for some reason. To be fair I did change the path, of course, and maybe one or two other things (after trying the original) but it doesn't work and doesn't complete the while loop (or, from what I can tell, iterate it at all.)

import subprocess, time
import os


#The path to StockFish

SFPath = "stockfish_14.1_win_x64_avx2\stockfish_14.1_win_x64_avx2.exe"


engine = subprocess.Popen(SFPath, universal_newlines=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE)

def put(command):
    print('\nyou:\n\t'+command)
    engine.stdin.write(command+'\n')

def get():
    # using the 'isready' command (eng has to answer 'readyok')
    # to indicate current last line of stdout
    engine.stdin.write('isready\n')
    print('\nengine:')
    while True:
        print("This will print each time it iterates")
        text = engine.stdout.readline().strip()
        if text == 'readyok':
            print("this is printed when get the program has completed the answer")
            break
        if text != 'readyok':
            #note I have tried '' like the original code and it doesn't work either
            print("This will print whenever the engine output doesn't equal readyok")
            print('\t'+text)

put('go depth 15') 
get()
put('eval')
get() 
put('position fen rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1') 
get()

For reference the output is

you:
    go depth 15

engine:
This will print each time it iterates
This will print whenever the engine output doesn't equal readyok
    Stockfish 14.1 by the Stockfish developers (see AUTHORS file)
This will print each time it iterates

Another tidbit that might be able to help people is that is I manually close the Stock Fish Terminal that opens it will repeat for forever, for as much as I can tell

This will print each time it iterates
This will print whenever the engine output doesn't equal readyok

I cannot understand how subprocess works and how to give/get input/output. I am using Windows 10, Python 3.10.0 and StockFish 14.1.

halfer
  • 19,824
  • 17
  • 99
  • 186
Hidden
  • 11
  • 2

0 Answers0