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

2024-04-25 15:17:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个python文件,它来自于这本关于deep learning and go的书,如下所示。如果我做一个python.exe机器人_机器人.py,程序运行。在

如果我跑的话_机器人.py从eclipse/pydev开始工作。在

ipnb文件与bot\v位于同一文件夹中_机器人.py. 在

如果我把:

from bot_v_bot import main
main()

在.ipnb文件中的一个单元格中运行它,它显示:

^{pr2}$

编辑:下面的代码有效。eclipse在python路径上有src。在

import sys
sys.path.append('src')
from bot_v_bot import main
main()

文件:bot_v_机器人.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[]

Tags: 文件thefrompyimportboardgamemove
1条回答
网友
1楼 · 发布于 2024-04-25 15:17:34

请检查并确保您的模块位于任何一个系统路径下(搜索路径)在

在系统路径可以通过以下代码检索值。在

import sys
sys.path

如果模块的路径未添加到搜索路径,我们可以在系统路径通过

^{pr2}$

相关问题 更多 >