代码之家  ›  专栏  ›  技术社区  ›  Ray Tayek

如何将函数从文件导入jupiter笔记本?

  •  0
  • Ray Tayek  · 技术社区  · 6 年前

    deep learning and go 如下所示。如果我执行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()
    

    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[]
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Salai Madhavan    6 年前

    请检查并确保您的模块位于任何一个系统路径(sys.path)下

    import sys
    sys.path
    

    如果模块的路径没有添加到sys.path中,我们可以通过

    sys.path.append('C:\\') # sample path C:\\