12
votes

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?

2

2 Answers

10
votes

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.

6
votes

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