简体   繁体   English

如何在 Google Colab 中导入 notebooks.py?

[英]How to import notebooks .py inside the Google Colab?

I want to use some functions that are contained in other python modules/notebooks ( .py ) in a google colab notebook.我想在 google colab notebook 中使用其他 python 模块/笔记本( .py )中包含的一些功能。

With the google colab notebook open, I went to index >> files >> upload and uploaded the file EOS.py .打开 google colab notebook 后,我转到 index >> files >> upload 并上传文件EOS.py However, when I do import EOS or from EOS import set_eos it gives an error.但是,当我import EOSfrom EOS import set_eos时,会出现错误。 What should I change so that the call of a mudlo or external function of a mutale is correct?我应该更改什么以便 mutale 的 mutale 或外部 function 的调用是正确的?

在此处输入图像描述

You can save it first, then import it.您可以先保存,然后再导入。

from google.colab import files
src = list(files.upload().values())[0]
open('EOS.py','wb').write(src)
import mylib

Now you can upload easily by现在您可以通过以下方式轻松上传

  • Click at [>] to open the left pane单击 [>] 打开左侧窗格
  • Choose file tab选择文件选项卡
  • Click [upload] and choose your [EOS.py]单击 [上传] 并选择您的 [EOS.py]
  • Import mylib导入 mylib

If you don't want to upload every time, you can store it in S3 and mount it to Colab, as shown in this https://gist.github.com/korakot/e28ea5269129b70f220fe5e8d065b446如果不想每次都上传,可以存储在S3中,挂载到Colab中,如下图https://gist.github.com/korakot/e28ea5269129b70f220fe5db464

Now that you can mount your Google Drive automatically.现在您可以自动挂载您的 Google 云端硬盘了。 It is easier to just copy it from Drive than upload it.从云端硬盘复制它比上传它更容易。

  • Store EOS.py in your Drive EOS.py存储在您的驱动器中
  • Open a new Colab打开一个新的 Colab
  • Open the (left)side pane, select Files view打开(左)侧窗格,select Files视图
  • Click Mount Drive then Connect to Google Drive点击Mount Drive然后Connect to Google Drive
  • Copy it by .cp drive/MyDrive/EOS.py .通过.cp drive/MyDrive/EOS.py .
  • import EOS

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

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