I am trying to get Stockfish to work in Python 3. Just to get started on my project, I tried to have Stockfish think until a depth of 20. However, the program instantly returns that Stockfish recommends 1.a3 in the starting position. After browsing these forums, I found out that the problem was that there was a missing '\n'. However, after adding it, the program still doesn't work. Can someone tell me what I did wrong?

Code: enter image description here

Message: enter image description here


Even though the best answer is to use a pre-existing module instead of reinventing the wheel, just for illustrative purposes here's a version that works using only pure Python Popen(). The key is the use of "isready" and waiting for an answer:

import time
from subprocess import Popen, PIPE

def put(p, msg):

def get(p, verbose=False):
    put(p, 'isready')
    lines = []
    while True:
        s = p.stdout.readline().strip()
        if s == 'readyok':
        if verbose:
    return lines

def putget(p, msg, verbose=False):
    put(p, msg)
    get(p, verbose)

def go(p, depth=None, t=1.0, dt=0.05, verbose=False):
    depth = 'infinite' if depth is None else 'depth %d' % depth
    put(p, "go %s" % depth)
    t0 = time.time()
    stopped = False
    while True:
        lines = get(p, verbose=verbose)
        for line in lines:
            if line.startswith('bestmove'):
                return line
        if not stopped and time.time() - t0 > t:
            put(p, "stop")
            stopped = True

p = Popen("stockfish-8-64", stdout=PIPE, stdin=PIPE, universal_newlines=True)
get(p, verbose=True)
putget(p, 'uci')
putget(p, 'position startpos')
bestmove = go(p, depth=20, t=3.0)
| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.