是否可以将我自己的模块导入 google-colaboratory notebook?
Is it possible to import my own modules into a google-colaboratory notebook?
我正在使用 google colaboratory 与 python 一起教授数据科学,为了让笔记本只包含学生的特定课程,我想导入一些我们编码的基本方法而不是给他们一个大笔记本,预先填满这些方法以防止分心。
如何导入这些方法而不在 pip 上发布它们?。 google-colaborary pip 可以从 github 安装吗?
对我们来说,最好的选择是能够将代码保存在云端硬盘中,并将模块上传到 colab space,就像我们处理 csv 文件一样,并使用标准导入。这可能吗?
How can I import these methods without publishing them on pip?. Can
google-colaborary pip install from github?
是的,您可以在协作中通过 运行 bash 命令(通过将 !
附加到命令)从 github 执行 pip 安装。例如:
!pip install git+<github_link>
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 协作实例上安装 google-drive 来完成。
您将需要安装 ocamlfuse 并使用以下方式为您的 google 帐户获取权限:
!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 驱动器使用:
!mkdir -p drive
!google-drive-ocamlfuse drive
之后您可以使用以下方法检查挂载是否成功:
!ls drive
应该会显示 google 驱动器中的所有文件。
我有一个卑鄙的把戏可以做到这一点。将我所有的代码都放在一个包中,我可以做到
!wget mysite/mypackage.py
from mypackage import *
同样,如果 mypackage 有依赖项,我可以 wget 一个 zipfile 并 !unzip 它
我正在使用 google colaboratory 与 python 一起教授数据科学,为了让笔记本只包含学生的特定课程,我想导入一些我们编码的基本方法而不是给他们一个大笔记本,预先填满这些方法以防止分心。
如何导入这些方法而不在 pip 上发布它们?。 google-colaborary pip 可以从 github 安装吗?
对我们来说,最好的选择是能够将代码保存在云端硬盘中,并将模块上传到 colab space,就像我们处理 csv 文件一样,并使用标准导入。这可能吗?
How can I import these methods without publishing them on pip?. Can google-colaborary pip install from github?
是的,您可以在协作中通过 运行 bash 命令(通过将 !
附加到命令)从 github 执行 pip 安装。例如:
!pip install git+<github_link>
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 协作实例上安装 google-drive 来完成。
您将需要安装 ocamlfuse 并使用以下方式为您的 google 帐户获取权限:
!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 驱动器使用:
!mkdir -p drive
!google-drive-ocamlfuse drive
之后您可以使用以下方法检查挂载是否成功:
!ls drive
应该会显示 google 驱动器中的所有文件。
我有一个卑鄙的把戏可以做到这一点。将我所有的代码都放在一个包中,我可以做到
!wget mysite/mypackage.py
from mypackage import *
同样,如果 mypackage 有依赖项,我可以 wget 一个 zipfile 并 !unzip 它