是否可以将我自己的模块导入Google协作型笔记本? [英] Is it possible to import my own modules into a google-colaboratory notebook?

查看:39
本文介绍了是否可以将我自己的模块导入Google协作型笔记本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用google colaboratory用python讲授数据科学,为了让笔记本只留给学生上一堂特定的课,我想输入一些我们编写的基本方法,而不是给他们预先装好的大笔记本这些方法可以防止任何干扰.

I am using google colaboratory for teaching Data Science with python and in order to leave the notebooks only with the specific lesson for the students I would like to import some basic methods we have coded instead to give them a big notebook pre-filled with these methods for preventing any distractions.

如何在不将其发布到pip的情况下导入这些方法?可以从github安装google-colaborary pip吗?

How can I import these methods without publishing them on pip?. Can google-colaborary pip install from github?

对我们来说,最好的选择是能够将代码保存在云端硬盘中,并将模块上传到colab空间,就像处理csv文件一样,并使用标准导入.有可能吗?

The best option for us would be to be able to have the code in Drive and an upload the module to colab space like we do with the csv files, and use standard import. Is that possible?

推荐答案

如何在不将其发布到pip的情况下导入这些方法?能 是从github安装google-colaborary pip吗?

How can I import these methods without publishing them on pip?. Can google-colaborary pip install from github?

是的,您可以通过在collab中运行bash命令(通过将!附加到命令)来从github进行pip安装.例如:

Yes, you can do pip install from github by running bash commands (by appending ! to the commands) in collab. For example:

!pip install git+<github_link>

对我们而言,最好的选择是能够将代码保存在云端硬盘中 然后将模块上传到colab空间,就像处理csv文件一样, 并使用标准导入.有可能吗?

The best option for us would be to be able to have the code in Drive and an upload the module to colab space like we do with the csv files, and use standard import. Is that possible?

这有点棘手,但是可以通过使用[google-drive-ocamlfuse][1]将google-drive安装在google collab实例上来完成.

This is a bit tricky but can be done by mounting your google-drive on your google collab instance using [google-drive-ocamlfuse][1].

您将需要使用以下方法安装ocamlfuse并获取您的Google帐户的权限:

You will need to install ocamlfuse and get permissions for your google account using:

!apt-get install -y -qq software-properties-common python-software-properties module-init-tools
!add-apt-repository -y ppa:alessandro-strada/ppa 2>&1 > /dev/null
!apt-get update -qq 2>&1 > /dev/null
!apt-get -y install -qq google-drive-ocamlfuse fuse
from google.colab import auth
auth.authenticate_user()
from oauth2client.client import GoogleCredentials
creds = GoogleCredentials.get_application_default()
import getpass
!google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret} < /dev/null 2>&1 | grep URL
vcode = getpass.getpass()
!echo {vcode} | google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret}

,然后使用以下方法挂载Google驱动器:

and then mount google drive using:

!mkdir -p drive
!google-drive-ocamlfuse drive

之后,您可以使用以下方法检查挂载是否成功:

After that you can check if the mount was successful using:

!ls drive

应显示您的Google驱动器中的所有文件.

which should show all the files in your google drive.

这篇关于是否可以将我自己的模块导入Google协作型笔记本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆