pythondeep-learningpackagegoogle-colaboratory

How can I run python package using Google Collab?


I want to run the DAN repo. I am using Google Cloud Collab. I cloned the project on my Google Drive in the following directory /content/drive/MyDrive/DAN/DAN Trying to run An example script file is available at OCR/document_OCR/dan/predict_examples to recognize images directly from paths using trained weights.

using collab notebook located inside /content/drive/MyDrive/DAN/DAN

from google.colab import drive
drive.mount('/content/drive')

import sys
sys.path.append('/content/drive/MyDrive/DAN/DAN')

!python3 /content/drive/MyDrive/DAN/DAN/OCR/document_OCR/dan/predict_example.py

But this is not working for me And I am facing the following issue Traceback (most recent call last): File "/content/drive/MyDrive/DAN/DAN/OCR/document_OCR/dan/predict_example.py", line 9, in <module> from basic.models import FCN_Encoder ModuleNotFoundError: No module named 'basic'

Where the predict_example.py scripts starts with

import os.path

import torch
from torch.optim import Adam
from PIL import Image
import numpy as np

from basic.models import FCN_Encoder
from OCR.document_OCR.dan.models_dan import GlobalHTADecoder
from OCR.document_OCR.dan.trainer_dan import Manager
from basic.utils import pad_images
from basic.metric_manager import keep_all_but_tokens

Solution

  • !python3 spawns a new interpreter which doesn't know about sys.path.append(...) above it, it only affects the current interpreter.

    instead either use runpy.run_path to execute a file within the current interpreter, or modify os.environ["PYTHONPATH"] by appending :/your_path to it before calling !python3 so it will carry over to other spawned interpreters.