我正试图导出一个python-象棋游戏到一个pgn文件。文档建议-
import chess
.
.
chessBoard = chess.Board()
.
.
#Play the game and when over do below
game = chess.pgn.Game.from_board(chessBoard)
with open('output.pgn', 'a') as the_file:
print(game, file=the_file, end="\n\n")但是chess.pgn.Game.from_board(chessBoard)行会抛出以下错误-
AttributeError:模块“国际象棋”没有属性“pgn”
当我键入pgn时,chess.也会显示在intellisense中,因此编辑器也能够看到chess中有一个pgn。这是运行在windows 10上的VS2015中的python3.x。
是什么引起的?
发布于 2017-03-07 06:33:25
要使用pgn模块,还必须执行import chess.pgn
发布于 2020-05-03 15:16:21
对于那些没有让它与已接受的答案一起工作的人,请检查您是否将文件命名为chess.py (在这里您已经编写了import chess)。如果是这样的话,就把它改为其他的东西,比如pychess.py。
让您的文件名为chess.py不起作用的原因,我相信是因为它必须导入自己,当然也没有chess.Board()。
https://stackoverflow.com/questions/42641603
复制相似问题