Search code examples
pythonpython-chess

Errno 8 Exec Format Error - Stockfish/python chess


I am trying to access Stockfish evaluations through python chess, however, whenever I try to run the code, I am met with "[Errno 8] Exec format error". I tried running the some code off the documentation and am met with the same error. I read some articles and they talked about adding a shebang to the executable code but I don't see anyone else needing to do this, or any reference of it in the documentation. Am I just really dumb? Sorry, I am still new to coding. The documentation code is below:

import chess
import chess.engine

engine = chess.engine.SimpleEngine.popen_uci("/content/drive/MyDrive/stockfish_14.1_win_x64_avx2/stockfish_14.1_win_x64_avx2.exe")

board = chess.Board()
info = engine.analyse(board, chess.engine.Limit(time=0.1))
print("Score:", info["score"])
# Score: PovScore(Cp(+20), WHITE)

board = chess.Board("r1bqkbnr/p1pp1ppp/1pn5/4p3/2B1P3/5Q2/PPPP1PPP/RNB1K1NR w KQkq - 2 4")
info = engine.analyse(board, chess.engine.Limit(depth=20))
print("Score:", info["score"])
# Score: PovScore(Mate(+1), WHITE)

engine.quit()
---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
<ipython-input-9-9affa6765785> in <module>()
      2 import chess.engine
      3 
----> 4 engine = chess.engine.SimpleEngine.popen_uci("/content/drive/MyDrive/stockfish_14.1_win_x64_avx2/stockfish_14.1_win_x64_avx2.exe")
      5 
      6 board = chess.Board()

15 frames
/usr/lib/python3.7/subprocess.py in _execute_child(self, args, executable, preexec_fn, close_fds, pass_fds, cwd, env, startupinfo, creationflags, shell, p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite, restore_signals, start_new_session)
   1549                         if errno_num == errno.ENOENT:
   1550                             err_msg += ': ' + repr(err_filename)
-> 1551                     raise child_exception_type(errno_num, err_msg, err_filename)
   1552                 raise child_exception_type(err_msg)
   1553 

OSError: [Errno 8] Exec format error: '/content/drive/MyDrive/stockfish_14.1_win_x64_avx2/stockfish_14.1_win_x64_avx2.exe'

Solution

  • Use a stockfish that is compiled for linux as the machine is not run on windows.

    If you encounter permission issue use the command for example:

    chmod 777 "/content/drive.../stockfish_linux"
    or
    !chmod 777 "/content/drive.../stockfish_linux"
    for google colab
    

    where stockfish_linux is your stockfish engine file.