如何使用 google colab 运行 导入自定义 python 模块(两个文件在存储库文件夹中的同一级别)的现有 jupyter notebook?

How to run existing jupyter notebook that imports custom python module(both files are same level in repository folder) using google colab?

我有一个 github 存储库,其中有一个 program.py 包含我的 python 程序和notebook.ipynb.

在笔记本中,我导入了程序并调用了它的函数。这正常工作,因为我 运行 notebook 在本地。

在 Colab 上,我可以轻松打开 notebook,但每次我将更改推送到我的 repo 时,我都必须删除并重新启动Colab 运行时间,重新克隆 repo 以 运行 新代码正确。 有没有办法让这个过程不那么复杂?

您可以直接获取 program.py 的更新代码,方法是使用 wget 获取原始代码的 URL,例如 this。这应该消除了重新克隆的需要。

然后要将该代码生成的新版本放入您的活动命名空间,而无需重新启动内核,请使用 importlib 中的 reload 重新加载 program.py 中的函数,参见 here and here and here for options. Plus, there is an autoreload magic for when you work in a Juptyer notebook。我首先引用的纯 Python 也适用于笔记本。纯 Python 版本不太方便。