[英]How to run existing jupyter notebook that imports custom python module(both files are same level in repository folder) using google colab?
I got a github repository having a program.py containing my python program and a notebook.ipynb .我有一个 github 存储库,其中包含一个program.py包含我的 python 程序和notebook.ipynb 。
In the notebook, i import program and call its functions.在笔记本中,我导入程序并调用它的函数。 This works normally as i run the notebook locally.
当我在本地运行笔记本时,这可以正常工作。
On Colab, i can open the notebook easily but every time i push changes to my repo i have to delete and restart the Colab runtime, reclone repo to it in order to run the new code right.在 Colab 上,我可以轻松打开笔记本,但每次我将更改推送到我的 repo 时,我都必须删除并重新启动 Colab 运行时,重新克隆 repo 以正确运行新代码。 Is there any way to make this process less complicate?
有没有办法让这个过程变得不那么复杂?
You can directly fetch the updated code for program.py
by using wget
to get the URL for the raw code, like this .您可以使用
wget
直接获取program.py
的更新代码,以获取原始代码的 URL,如下所示。 That should eliminate the need for recloning.这应该消除重新克隆的需要。
And then to get the new versions produced by that code into your active namespace, without need for restarting the kernel, use reload
from importlib
to reload the functions in program.py
, see here and here and here for options.然后将该代码生成的新版本放入您的活动命名空间,无需重新启动内核,使用
reload
from importlib
重新加载program.py
中的函数,请参阅此处和此处以及此处了解选项。 Plus, there is an autoreload magic for when you work in a Juptyer notebook .此外,当您在 Juptyer notebook 中工作时,还有一种自动重装魔法。 The pure Python I referenced first will work in the notebook, too.
我首先引用的纯 Python 也可以在 notebook 中使用。 The pure Python versions are just less convenient.
纯 Python 版本不太方便。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.