【问题标题】:python chess module doesn't work properly on google colabpython 国际象棋模块在google colab上无法正常工作
【发布时间】:2021-06-07 14:56:25
【问题描述】:

我在 google colab 笔记本中使用 python 国际象棋模块。具体来说,我正在运行与此模块相关的三个命令:

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

而 chess.pgn.read_game.headers 的工作原理就像一个魅力,第一个命令抛出一个 AttributeError: 'Game' object has no attribute 'mainline_moves'。奇怪的是,我的脚本在本地运行时运行良好(我在 conda python 3.8.10 环境中使用 Spyder 5)。

stackoverflow 上出现此类错误的其他问题似乎与名为 chess.py 的用户文件有关,因此产生了命名问题。但是,在我看来,这似乎不太可能发生在 google colab 笔记本中......

有关我在国际象棋环境中解决有关 GAN 的烦人(尚未解决的问题)的更多信息,请查看 How to use GPU and CPU in tensorflow (keras)?TF model doesn't predict anymore after switching to GPU

我的代码:

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)

【问题讨论】:

    标签: python google-colaboratory chess python-chess


    【解决方案1】:

    卸载python-chess,然后安装新版本的python chess。

    !pip uninstall python-chess
    
    !pip install chess
    

    测试

    for moves in list_games:
        print(moves)
    

    输出

    1. d4 { [%clk 0:15:00] } 1... d6 { [%clk 0:15:00] } 2. c4 { [%clk 0:14:54] } 2... Nf6 { [%clk 0:14:58] } ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-24
      • 2022-06-17
      • 2018-03-25
      • 1970-01-01
      • 2015-05-02
      • 1970-01-01
      • 2020-04-26
      相关资源
      最近更新 更多