繁体   English   中英

如何将功能从文件导入木星笔记本?

[英]How to import a function from a file into a jupiter notebook?

我有一个关于深度学习的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[]

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

sys.path值可以通过以下代码检索。

import sys
sys.path

如果未在sys.path中添加模块的路径,则可以通过以下方式将路径添加到sys.path中:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM