How can I import custom modules from a Github repository in Google Colab?
Asked Answered
E

2

22

I understand how to run a single notebook in Colab. However, I am not sure how to use all files from a repository, i.e to be able to import functions inside Colab notebook? Thank you.

Eluvium answered 6/10, 2018 at 17:6 Comment(1)
Possible duplicate of Methods for using Git with Google ColabTreaty
O
21

Let's say we want to run the ipynb file, named as "1-fully-connected-binarized-mnist" residing in the repo "qnn-inference-examples".

https://github.com/maltanar/qnn-inference-examples

The notebook of interest uses customly created QNN library and functions inside that repo. Yes we need to import that function. To do this, we should first upload the repo folder to Google Colab, then correct/modify library and file paths.

0) Open the ipynb file "1-fully-connected-binarized-mnist" on your Colab. You can rename it if you like. Try to run it, but will probably get some errors (as I did). So let's fix these issues

1) Insert a new code cell at the top of the notebook. And clone the repo on your Colab:

!git clone https://github.com/maltanar/qnn-inference-examples.git

now the new folder "qnn-inference-examples" created under your "content" folder. you should see something like this on the left side. And remember the path "/content/qnn-inference-examples"

check folder uploaded or not, and location

2) Now add the second new cell on top:

import sys
sys.path.insert(0,'/content/qnn-inference-examples')

This will fix the issue about not able to find the library location, when trying import the QNN libraries.

3) Manually fix the file links on the existing code, according to the new path. Because the library and files now exist under the folder "/content/qnn-inference-examples":

for example replace:

img = Image.open("7.png")

with

img = Image.open("/content/qnn-inference-examples/7.png")

These steps should do the work


Please note that: This is not my own solution, mix of 2 or 3 solutions. Credit goes to Hüseyin Elçi, KDnuggets and Alexandr Haymin

https://medium.com/analytics-vidhya/importing-your-own-python-module-or-python-file-into-colab-3e365f0a35ec

https://www.kdnuggets.com/2018/02/google-colab-free-gpu-tutorial-tensorflow-keras-pytorch.html/2

Occasionalism answered 24/12, 2020 at 15:9 Comment(0)
M
3

Please see the example below:

!git clone https://www.github.com/matterport/Mask_RCNN.git

from google.colab import files

files.os.chdir('Mask_RCNN')

# To find local version of the library
sys.path.append(os.path.join(ROOT_DIR, 'Mask_RCNN'))  

# here is your import
from mrcnn.config import Config
Mississippian answered 9/10, 2018 at 20:50 Comment(1)
This errors out with: "NameError: name 'files' is not defined"Gratian

© 2022 - 2024 — McMap. All rights reserved.