【发布时间】:2019-02-03 07:26:53
【问题描述】:
我有一个来自本书deep learning and go 的python 文件,如下所示。如果我执行 python.exe bot_v_bot.py,程序就会运行。
如果我从 eclipse/pydev 运行 bot_v_bot.py,那么它可以工作。
.ipnb 文件与 bot_v_bot.py 位于同一文件夹中。
如果我说:
from bot_v_bot import main
main()
进入 .ipnb 文件中的一个单元格并运行它,它说:
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-4-248b35949c67> in <module>()
----> 1 from bot_v_bot import main
2 main()
ModuleNotFoundError: No module named 'bot_v_bot'
编辑:下面的代码有效。 eclipse在python路径上有src。
import sys
sys.path.append('src')
from bot_v_bot import main
main()
文件:bot_v_bot.py:
from __future__ import print_function
# tag::bot_vs_bot[]
from dlgo import agent
from dlgo import goboard_slow
from dlgo import gotypes
from dlgo.utils import print_board, print_move
import time
def main():
board_size = 9
game = goboard_slow.GameState.new_game(board_size)
bots = {
gotypes.Player.black: agent.naive.RandomBot(),
gotypes.Player.white: agent.naive.RandomBot(),
}
while not game.is_over():
time.sleep(0.3) # <1>
print(chr(27) + "[2J") # <2>
print_board(game.board)
bot_move = bots[game.next_player].select_move(game)
print_move(game.next_player, bot_move)
game = game.apply_move(bot_move)
if __name__ == '__main__':
main()
# <1> We set a sleep timer to 0.3 seconds so that bot moves aren't printed too fast to observe
# <2> Before each move we clear the screen. This way the board is always printed to the same position on the command line.
# end::bot_vs_bot[]
【问题讨论】:
-
如果你运行 import bot_v_bot ,输出是什么? ModuleNotFoundError 再次出现?
-
ModuleNotFoundError: 没有名为“bot_v_bot”的模块
标签: python jupyter-notebook python-import