简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM