The `stockfish` library provides a Python wrapper for the powerful open-source Stockfish chess engine, enabling easy integration into Python applications. It allows users to control the engine, set board positions, get best moves, and retrieve evaluations. Currently at version 4.0.8, the library is actively maintained with frequent patch releases, typically releasing multiple updates within a month when new features or fixes are introduced.
pip install stockfishVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to initialize the `Stockfish` class, set a FEN position, retrieve the best move, make a move, and get the engine's evaluation. Remember to replace `STOCKFISH_EXECUTABLE_PATH` with the actual path to your downloaded Stockfish engine binary. It's recommended to manage the Stockfish executable's path via an environment variable for easier deployment.
Download the appropriate Stockfish engine executable from the official Stockfish website (https://stockfishchess.org/download/) and provide its full path to the `Stockfish` class constructor (e.g., `Stockfish(path='/path/to/stockfish_engine')`).
Validate all FEN strings and move inputs before passing them to the `Stockfish` object. The `is_fen_valid()` method can help, though it has some known limitations (e.g., for checkmates/stalemates).
Experiment with different combinations of `Skill Level`, `depth`, and `MultiPV` parameters. For significantly weaker play, consider using a lower `depth` and increasing `MultiPV` to encourage the engine to explore more options, potentially picking weaker ones.
If explicit control over the Stockfish engine's lifecycle is critical, call `stockfish.send_quit_command()` directly when you are finished using the engine instance to ensure the process is terminated cleanly.
pip install stockfish
Download the appropriate Stockfish engine executable for your operating system and provide its absolute path to the `Stockfish` constructor, e.g., `Stockfish(path="/path/to/stockfish_executable")`.
Ensure the FEN string is correctly formatted according to the standard and represents a valid chess board state.
Call `set_skill_level()` with an integer argument between 0 and 20 (inclusive).