在googlecolab上导入自己的ipynb文件



我正在使用谷歌colab处理jupyter笔记本电脑(所有文件都在驱动器中(。我有两个文件:Exploratory_Data_Analysis.ipynb和PCA.ipynb。我想导入以使用从第一个到第二个的数据组合。仅在本地使用jupyter笔记本电脑(而不是与谷歌合作(,导入只需执行以下操作即可:

!pip install import-ipynb
import import_ipynb
import Exploratory_Data_Analysis as eda

但在谷歌colab上,我尝试了以下方法:

!pip install import-ipynb
import import_ipynb
!pip install -U -q PyDrive
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials
import os
import pandas as pd
auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)
listed = drive.ListFile({'q': "'1CXqv7-PZmYrWes4MOk' in 
parents and trashed=false"}).GetList()
for file in listed:
print('title {}, id {}'.format(file['title'], file['id']))
eda = os.path.join(download_path, 'Exploratory_Data_Analysis.ipynb')
temp_eda = drive.CreateFile({'id': '1YpDhXGeJVtzuxUJS5gKsUbm'})
temp_eda.GetContentFile(eda)
import Exploratory_Data_Analysis

得到这个:

importing Jupyter notebook from Exploratory_Data_Analysis.ipynb
NotJSONError: Notebook does not appear to be JSON: ''...

有没有其他方法可以在googlecolab上导入自己的ipynb文件?

您是否已成功将笔记本/ipynb文件导入Google Colab项目?我将本地jupyter笔记本中正在进行的工作迁移到Google Colab的方式是使用Github和Clouderizer。这种方法还允许我在jupyter笔记本电脑环境中工作,就像我在本地工作一样,但能够立即将我的工作同步到Google Colab。此外,这种方法允许我通过简单地执行import <my own python/ipynb module>之类的操作,将模块.ipynb/.py导入到我正在处理的笔记本中。我建议使用这种设置,而不是在GoogleColab上使用毛茸茸的linux命令行。

以下是如何使用Clouderizer轻松设置笔记本电脑从github到Google Colab的教程:Medium教程。

基本上,这些是使用Clouderizer:设置ipynb笔记本以及数据集文件夹所需的步骤

谷歌Colab项目的Clouderizer

先决条件

  1. 注册google/gmail帐户
  2. 注册Clouderizer帐户[链接]
  3. 在github上创建一个存储库,并将您的项目(所有ipynb、py模块,甚至数据集(如果<1GB,请将其压缩(上传到您的repo

设置Clouderizer项目

  1. 登录Clouderizer控制台。第一次登录时,系统会提示您将Google Drive与Clouderizer链接。按照屏幕上的说明进行操作。如果它没有提示链接,您可以通过Clouderizer仪表板->侧边栏菜单->Clouderizer Drive来配置您的Google Drive。Clouderizer将在您的Google Drive中设置一个名为"Clouderizer"的文件夹,以包含您的ipynb项目(无论是机器学习还是任何项目(
  2. 返回Clouderizer Dashboard。然后单击"新建项目"。按照说明进行操作时,您可以选择加载整个github项目,该项目包含您打算上传到Google Colab的ipynb作品
  3. 在步骤5中,您可以通过指定数据集的URL(例如,从kaggle数据集URL(来包含您想要处理的其他数据集。您还可以选择并指定是否要通过为数据集、主代码/模块和输出文件设置单独的文件夹来重构项目
  4. 接下来,前往Google Colab/在Google Drive的任何位置创建一个Google Colab文件,然后执行:!wget NS-内容处置https://to_whatever_link_you_get_to_console'
  5. 最后,返回Clouderizer面板,检查您的项目环境是否已经运行并同步到Google Colab。然后点击该面板中的jupyter笔记本图标,就在你给出的项目标题名称旁边。现在,你可以开始进行机器学习或任何你所做的工作,就像你在本地机器上一样,然后立即在谷歌Colab上同步
  6. 最后,您将能够验证您的.ipynb是否有效,并能够在Google Colab中导入模块/数据集。只需返回谷歌硬盘->clouderizer文件夹->您的项目。然后试着运行,看看是否一切都像在jupyter笔记本上一样工作

下面的代码非常适合我。1.将所有ipynb文件复制到colab中的一个文件夹中2.共享colab中的ipynb文件,请参阅链接:https://www.pingshiuanchua.com/blog/post/importing-your-own-python-module-or-python-file-in-colaboratory3.然后按照以下步骤操作:

!pip install import-ipynb
import import_ipynb
# Install the PyDrive wrapper & import libraries.
# This only needs to be done once per notebook.
!pip install -U -q PyDrive
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials
# Authenticate and create the PyDrive client.
# This only needs to be done once per notebook.
auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)
# Copy the link and remove the front part of the link (i.e. https://drive.google.com/open?id=) to get the file ID.
your_module = drive.CreateFile({'id':'eyetgd1zyxwvutsrqponmlkjihgfedcba'})
your_module.GetContentFile('myfile.ipynb')
import myfile

您只需要运行:

%run YourOtherModule.ipynb

最新更新