1

I am using the python chess module in a google colab notebook. Specifically, I am running three commands related to this module:

  • chess.pgn.read_game(pgn_moves).mainline_moves()
  • chess.pgn.read_game(pgn_sides).headers["White"]
  • chess.pgn.read_game(pgn_variant).headers["Variant"]

Whereas chess.pgn.read_game.headers works like a charm, the first command throws an AttributeError: 'Game' object has no attribute 'mainline_moves'. Oddly enough, my script works perfectly well when run locally (I am using Spyder 5 in a conda python 3.8.10 environment).

Other questions on stackoverflow with this type of error seemed to have been connected to a user file being called chess.py, thus creating naming issues. However, it seems unlikely to me that this is happening in a google colab notebook...

For more information on my journey through annoying (yet unsolved issues) regarding GANs in a chess environment please check How to use GPU and CPU in tensorflow (keras)? and TF model doesn't predict anymore after switching to GPU.

My code:

import os
os.getcwd()

'''
The following section creates two lists based on a pgn file. One list includes
all moves made per game in the file, whereas the other lists the players names.
Various lists have to be introduced because of the incremental characteristic
of chess.pgn's read_game function. Additional code was necesary in order to 
eliminate non-standard variants (i.e. chess960 games) from the game list.
'''
import chess.pgn
file = "txt_files/games.txt"
pgn_file = open(file)
list_games = []
sides = []
variant = []
drop_list = []
length = 5000

game_list = []
for i in range(length):
    game_list.append(chess.pgn.read_game(pgn_file))
game_list = list(filter(None, game_list))
    
pgn_moves = open(file)
pgn_sides = open(file)
pgn_variant = open(file)

for i in range(len(game_list)):
    try:
        list_games.append(chess.pgn.read_game(pgn_moves).mainline_moves())
        sides.append(chess.pgn.read_game(pgn_sides).headers["White"])
        variant.append(chess.pgn.read_game(pgn_variant).headers["Variant"])
    except:
        print('Error')
        pass
    if variant[i] != 'Standard':
        #print('here here here', i, game_list[i])
        drop_list.append(i)
BLEB
  • 53
  • 6

1 Answers1

0

Uninstall python-chess then install new version of python chess.

!pip uninstall python-chess
!pip install chess

Test

for moves in list_games:
    print(moves)

Output

1. d4 { [%clk 0:15:00] } 1... d6 { [%clk 0:15:00] } 2. c4 { [%clk 0:14:54] } 2... Nf6 { [%clk 0:14:58] } ...
ferdy
  • 4,396
  • 2
  • 4
  • 16