Is it possible to import my own modules into a google-colaboratory notebook?

谁都会走 提交于 2019-12-17 18:24:43

问题


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.

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

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?


回答1:


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

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>

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?

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

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}

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

which should show all the files in your google drive.




回答2:


I have a dirty trick that does precisely this. With all my code in one package I can do

!wget mysite/mypackage.py
from mypackage import *

Similarly if mypackage has dependencies, I can wget a zipfile and !unzip it



来源:https://stackoverflow.com/questions/49219341/is-it-possible-to-import-my-own-modules-into-a-google-colaboratory-notebook

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!