Python Chess

I’m delighted to give you this guest post by Niklas Fiekas, the creator of Python Chess. You may think Python Chess is just another chess engine. It isn’t. It’s a library of routines which can manipulate and analyze chess data using Python.  After I learnt about Python Chess I immediately went to Code Academy and took their course on Python. I really see this set of tools becoming a key part in any testing or development frame-work.

Over to Niklas…

Python-chess by example: Automating the Bratko-Kopec Test

Decades ago (1982) Kopec and Bratko published a systematic test for assessing the strength chess-playing programs (pdf). Despite its age it can still be fairly challenging, even for modern chess engines. Of course 24 test positions are not going to be enough for a definite result, especially given that most positions are of a specific type: fairly closed and the solution often involves some kind of sacrifice.

For each of the 24 positions a unique best move is known. The engine is given 120 seconds to solve each position. 1 point is awared for the correct best move.

The positions are given as EPDs. In this article we are going to automate the process of testing an UCI engine, introducing python-chess along the way.


Why Python (and python-chess)?

When it comes to chess programming, C++ often is the language of choice. Performance is critical. Not nescessarily so, in this case. All intensive calculations will be done by the engine. We just want to convert the given EPD lines, send them to the engine and handle its response. Python is a very good tool for high-level stuff like this (or making a mini chess computer, or making a website to probe tablebases or creating a cross platform chess GUI). We will use python-chess to deal with the chess rules and the involved formats: EPDs, FENs and the UCI protocol. python-chess can also read and write PGNs, read Polyglot opening books and probe Syzygy tablebases.

Chess positions

FEN: 1k1r4/pp1b1R2/3q2pp/4p3/2B5/4Q3/PPP2B2/2K5 b – – 0 1

BK.01 is one of the easier positions: Black to move and mate in 3

In python-chess a position is represented by a chess.Bitboard(). To create a position from a FEN:

You can then check if a specific move is legal:

Or to get the shorter SAN notation of the move:

Or to make a move and see if it is a check (and much more):

Here we are just going to parse an EPD line to setup a position and get the related info:

Communicating with an UCI engine

Next we will communicate via UCI with a chess engine like Maverick. First start the engine process:

The engine expects an initial uci command. Then it will send information about its identity and options.

Now setup the position:

And calculate for two minutes. The result is the best move (according to the engine) and an optional ponder move (the expected reply the engine wants to ponder on).

Or in fact comparing it with the expected best move from the EPD:


Putting it all together

Congratulations Maverick 0.51 x64! On my machine you score 18/24, which is almost on a par with Stockfish 6 64’s 19/24.



The original Brato-Kopec test has one more rule: Sometimes the engine changes its mind. If it had the solution at the 30, 60 or 90 second mark but then discarded it, 1/4, 1/3 or 1/2 points are awarded. In order to do this we need to look at the engines principal variations while it is thinking. This is included in the full code of this example.

You can find out more about Python Chess at

  • max

    Naming Openings in Polyglot
    I am re-writing my wooden chess GUI, ( using python chess and trying to get my head round the Polyglot opening book. Is there a way of looking up the name of an opening using perhaps the Zobrist hash. I understand that a particular position could result from ultiple opening, but then you could use the first occurrence or highest weight. Does such a lookup table exist?

    • Niklas Fiekas

      Nice, that looks like an intresting project.

      Unfortunately all a polyglot opening book can tell are follow-up moves (weighted by an integer value) + one more integer value (learn) that often goes unused.

      I’ve got a data set of opening names here: Perhaps you can adapt that to make such a lookup table.

      • max

        Niklas, That is really helpful. I have been able to use that file to write a routine that chooses a book move and displays the opening name (and a ponder which can be used as a hint).
        By removing the fields I did not need I got your file size down to 400k and the response time is sub 0,04 secs, plenty fast enough for playing computer vs human.
        There remains the problem of which book move to choose for any given position.

        So what’s the advantage of the polyglot binary file approach? is it just speed?

        • Niklas Fiekas

          Yes, and speed (and space). The bigger the opening book, the faster polyglot will be in comparison. (The difference would be even more apparent with a lower-level implementation in some systems programming language.)

          One warning about my ECO data set: Not all named openings are considered good. I presume some openings in there are outright losing.