Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 877
Pablo Marin-Garcia Avatar asked Mar 11 '18 11:03

Pablo Marin-Garcia


People also ask

Can you import modules in Jupyter Notebook?

A module can be imported into an interactive console environment (e.g. a Jupyter notebook) or into another module. Importing a module executes that module's code and produces a module-type object instance.

How do I import a Python library into Google Colab?

Installing a Python package in Google Colab is simple using the pip command along with the exclamation mark (!). The exclamation mark at the start of a cell allows to run a shell command, and pip is the Python package installer that allows to install Python libraries.

How do I import data into Google Colab?

Click on “Choose Files” then select and upload the file. Wait for the file to be 100% uploaded. You should see the name of the file once Colab has uploaded it. Finally, type in the following code to import it into a dataframe (make sure the filename matches the name of the uploaded file).


2 Answers

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

like image 191
Sergio Lucero Avatar answered Sep 22 '22 06:09

Sergio Lucero


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.

like image 44
Ankur Ankan Avatar answered Sep 20 '22 06:09

Ankur Ankan