python-chess - AttributeError: module 'chess' has no attribute 'pgn'

python-chess - AttributeError: module 'chess' has no attribute 'pgn'

我正在尝试将 python-chess 游戏导出到 pgn 文件。 documentation 推荐 -

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: module 'chess' has no attribute 'pgn'

当我键入 chess. 时,

pgn 也会出现在智能感知中,因此编辑器也能够看到 chess 中有一个 pgn。这是 python 3.x 运行 windows 10 上的 VS2015。

可能是什么原因造成的?

要使用 pgn 模块,您还必须执行 import chess.pgn

对于那些无法使用已接受的答案的人,请检查您是否已将文件命名为 chess.py(您在其中编写了 import chess)。如果是这种情况,请将其更改为其他内容,例如 pychess.py

将您的文件命名为 chess.py 不起作用的原因是,我相信,它实际上是在导入自身,当然那里没有 chess.Board()