11

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?

Pablo Marin-Garcia
  • 3,846
  • 2
  • 26
  • 45

2 Answers2

10

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.

Ankur Ankan
  • 2,696
  • 2
  • 18
  • 36
  • Could you give an example, please, how to mount a specific sub-directory from google-drive to a google-colab instance? – J.E.K May 31 '18 at 11:50
  • @J.E.K I don't think you can mount a specific folder using google-drive-ocamlfuse. But since it mounts your whole drive, you can access any file from it. But if you prefer to have a specific directory you can copy it to your colab instance. This might help: https://stackoverflow.com/a/48385944/1470009 – Ankur Ankan Jun 01 '18 at 11:11
6

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

Sergio Lucero
  • 700
  • 9
  • 15