![](/img/trans.png)
[英]How to access files on Google Drive via Google Colab notebook (.ipynb)?
[英]import its own ipynb files on google colab
我正在使用 google colab 使用 jupyter 筆記本(所有文件都在驅動器中)。 我有 2 個文件:Exploratory_Data_Analysis.ipynb 和 PCA.ipynb。 我想導入以在第二個中使用來自第一個的數據。 僅在本地使用 jupyter notebook(不使用 google colaboratory),只需執行以下操作即可進行導入:
!pip install import-ipynb
import import_ipynb
import Exploratory_Data_Analysis as eda
但是使用 google 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: ''...
還有其他方法可以在google colab上導入自己的ipynb文件嗎?
您是否成功將筆記本/ ipynb文件導入到Google Colab項目? 我將本地Jupyter筆記本中正在進行的工作遷移到Google Colab的方式是使用Github和Clouderizer。 這種方法還使我可以在jupyter筆記本環境中工作,就好像我在本地進行工作一樣,但是可以將我的工作立即 同步到Google Colab。 另外,這種方法允許我通過簡單地執行諸如import <my own python/ipynb module>
類的操作,將模塊.ipynb / .py導入到正在進行的筆記本中。 我建議使用此安裝程序,而不要在Google Colab上使用有毛的Linux命令行。
以下是該教程如何使用Clouderizer將筆記本從github輕松設置到Google Colab: 中型教程 。
基本上,這些是使用Clouderizer設置ipynb筆記本以及數據集文件夾所需的步驟:
下面的代碼對我來說很完美。 1.將所有ipynb文件復制到colab中的一個文件夾中。2.從colab共享ipynb文件,請參閱鏈接: https ://www.pingshiuanchua.com/blog/post/importing-your-own-python-module-or-python -file-in-colaboratory 3.然后按照以下步驟操作:
!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
是你需要運行的一切:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.