3

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.

Community
  • 1
  • 1
Michael Gmeiner
  • 133
  • 1
  • 4
  • 1
    This sounds weird to me. Maybe try deleting the space and rewriting it : it can be a thin space that is displayed as a space in fixed width fonts ; if you accidentally typed Alt+Space for example. (That assumes there is actually an error at the place python tells you ; I believe the missing parenthesis I hadn't seen before reloading the answers should start the syntax error at the `def` keyword) – Ekleog Apr 15 '15 at 23:57

3 Answers3

2

The line

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

is missing a closing parenthesis. You probably wanted

stockfish_cmd = 'C:\\Users\\Michael\\Downloads\\stockfish-6-win\\stockfish-6-win\\Windows\\stockfish'
engine = subprocess.Popen(
    stockfish_cmd, universal_newlines=True,
    stdin=subprocess.PIPE, stdout=subprocess.PIPE)

Note the doubling of backslashes as well, although I believe it just happens to be harmless in this case.

phihag
  • 278,196
  • 72
  • 453
  • 469
0

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')

Mike Vella
  • 10,187
  • 14
  • 59
  • 86
0

I put a script for returning the Stockfish position evaluation on github. Also a python wrapper here.

Peter Cotton
  • 1,671
  • 14
  • 17