0

I am doing some machine learning project and want to run the project on google colab since my own machine is too weak for it and hangs when i try to run the project on it. My project has the structure as shown in picture . project structure. I have multiple .py files each importing modules from one another. I converted the project to .zip file in my pc and then used the upload tab on google colab to upload the project. i unzipped the file and tried to run one code from the "examples" folder which is importing some function from the modAL function like this

from modAL.models import ActiveLearner.

this import is failing on google colab with error " no module named modAL " . Can someone please tell me how to get around this issue? The code works just fine on my own laptop.

desertnaut
  • 57,590
  • 26
  • 140
  • 166
  • Potential duplicate [question](https://stackoverflow.com/questions/52733786/how-to-import-custom-modules-in-google-colab) – Biranjan Feb 26 '19 at 08:40

2 Answers2

1

I found this explanation: https://zerowithdot.com/colab-workspace/ - very useful. After creating a space in your google drive

from os.path import join
from google.colab import drive
ROOT = "/content/drive"
drive.mount(ROOT)

fetch the git repo

GIT_USERNAME = "xxx"
GIT_TOKEN = "xxx"
GIT_REPOSITORY = "Repo"

!mkdir "{PROJECT_PATH}"
!git clone https://{GIT_TOKEN}@github.com/{GIT_USERNAME}/{GIT_REPOSITORY}.git " 
{PROJECT_PATH}"

Finally use importlib to get access to definitions

from importlib.machinery import SourceFileLoader
somemodule = SourceFileLoader('somelib', join(PROJECT_PATH, 
'utils/somelib.py')).load_module()
Uralan
  • 79
  • 1
  • 9
0

If the project is public (probably possible to make it work otherwise too) you can create a package [1] and install it with pip:

!pip install git+https://github.com/myuser/myproject

[1] https://packaging.python.org/tutorials/packaging-projects/

olejorgenb
  • 1,203
  • 14
  • 25