0

I'm using Google Colab and I want to import a file from the same directory.

I've mounted my Drive:

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

And then done:

import sys
sys.path.insert(0, '/content/drive/MyDrive/NLP/Exam')

However when I do

import funct as fu

It throws the following error:

ModuleNotFoundError: No module named 'funct'

Despite the fact that funct.py is in this directory. What could I be missing?

5
  • check current directory by: !pwd then check is your file exists in that directory or not. use ls command Mar 29, 2022 at 18:53
  • 1
    If the path is correct, there's no reason for that not to work. You've done print(os.listdir(sys.path[0])) to prove that it has what you think it has? Mar 29, 2022 at 18:57
  • Tim if I run your line I get No such file or directory: '/MyDrive/NLP/Exam'
    – zest16
    Mar 29, 2022 at 19:10
  • However if I run ls there is the funct.py file.
    – zest16
    Mar 29, 2022 at 19:10
  • @zest16 from funct import * Mar 29, 2022 at 19:11

1 Answer 1

0

Can't tell from your description, but if you are in a subpackage at the time of your import (e.g., a subdirectory of your main python program), and you wish to import a module from the same directory, you must give a fully qualified package name. For example, if you have:

- main.py
- mycode/
|-- __init__.py
|-- mymodule.py
|-- funct.py

and you wish to import funct from inside mymodule.py, then you must use:

import mycode.funct as funct

If that's not it, another possible cause is that sys.path has been modified and that the entry containing an empty string ("") has been removed. That is the entry that tells python to search the main script's directory for packages.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.