popeye
popeye

Reputation: 896

Is is possible to run a python project in google colab?

I'm trying to run a project on google colab's GPU. Now like a typical project I have the project divided into sub-folders and various .py scripts. For an example, my project structure is like:

test_project
    ├── module1.py
    ├── module2.py
    ├── run
    │   ├── run2.py
    │   └── run.py
    ├── tools
    │   └── tool.py
    └── utils
        ├── util1.py
        └── util2.py

The file I want to execute is run.py. Also, I have __init__.py (empty) files in all the folders.

Now, I know I can do sys.path.insert(1, 'test_project') and make this importable in colab notebook, but the problem is when I do something like

from test_project.module1 import foo

The import fails yelling

ImportError: cannot import name 'module1' from 'test_project'`. 

Though, import test_project works.

Does anyone has any idea on how can I import all the packages and functions inside them just like I'd be able to import when test_project is installed with pip install?

Thanks.

Upvotes: 2

Views: 7256

Answers (2)

niran
niran

Reputation: 1980

I think a simple way is, you can follow the below steps,

Step1: Mount the drive, by clicking on the left navigation

enter image description here

Step2: Type the pwd command to check the current working directory

enter image description here

Step3: Use the below command to change the working directory, that is you can specify the path where your python project folder is location

import os
import sys
#list the current working dir
os.getcwd()
#change the current working dir
os.chdir('/content/drive/MyDrive/ColabNotebooks/<Give project folder>')

Step4: Run the below command to check if the project folder successfully imported or you can run pwd to verify the current working directory

!ls

The main advantage of this approach is you can work with more than multiple python scripts or files

Upvotes: 0

Manolo Dominguez Becerra
Manolo Dominguez Becerra

Reputation: 1373

You can mount your project in Google Drive.

  1. Store mylib.py in your Drive
  2. Open a new Colab
  3. Open the (left)side panel, select Files view
  4. Click Mount Drive then Connect to Google Drive
  5. Copy it by !cp drive/MyDrive/mylib.py .
  6. import mylib

Another solution it might work is

import sys import os

py_file_location = "/content/drive/My Drive"
sys.path.append(os.path.abspath(py_file_location))

Now you can import it as a module in notebook for that location.

import what you want

Upvotes: 2

Related Questions