Search code examples
pythonchess

Stockfish and Python


I'm trying to write a script using python to feed chess positions into stockfish and get evaluations.

My question is based on this, How to Communicate with a Chess engine in Python?

The issue is with subprocess.pipe.

import subprocess, time
import os

os.chdir('C:\Users\Michael\Downloads\stockfish-6-win\stockfish-6-win\Windows'


engine = subprocess.Popen('stockfish', 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:
        text = engine.stdout.readline().strip()
        if text == 'readyok':
            break
        if text !='':
            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()

I'm getting an invalid syntax error on the comma after stdin=subprocess.PIPE

Any help fixing that or trying another method is appreciated.


Solution

  • You have a missing ) in your third line:

    os.chdir('C:\Users\Michael\Downloads\stockfish-6-win\stockfish-6-win\Windows'

    should be os.chdir('C:\Users\Michael\Downloads\stockfish-6-win\stockfish-6-win\Windows')