简体   繁体   English

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

[英]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.

相关问题 如何在 Google Colab 的 Jupyter 笔记本中导入自定义 Python package 和模块? - How can I import a custom Python package and module in a Jupyter notebook on Google Colab? Google CoLab - 如何运行位于我的 CoLab 环境的“文件”选项卡(即 /content/)中的 jupyter notebook 文件 - Google CoLab - How to run a jupyter notebook file that is in the 'Files' tab (i.e. /content/) of my CoLab environment 如何在 google colab(python,Jupyter notebook)中安装预定义的包(类)? - How to install a predefined package(class) in google colab(python,Jupyter notebook)? 如何自动将文件生成到与colab笔记本相同的google驱动器文件夹? - How do I automatically generate files to the same google drive folder as my colab notebook? 如何使用 google colab 在 jupyter notebook 中显示 GIF? - How to display a GIF in jupyter notebook using google colab? 在 jupyter notebook 和 google colab 中使用.so - Using .so in a jupyter notebook and google colab 如何在 Google Colab 上实现这个 Jupyter Notebook? - How to implement this Jupyter notebook on Google Colab? 如何在 Google Colab 笔记本的“.py”文件中运行 Python 脚本? - How to run a Python script in a '.py' file from a Google Colab notebook? 使用 python 的文件夹中 all.wav 文件的 Plot 频谱图(在 jupyter 笔记本中) - Plot spectograms for all .wav files in a folder using python (in jupyter notebook) 我无法将文件上传到Google Colab上的Jupyter Notebook - I'm unable to upload files to my Jupyter Notebook on Google Colab
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM