2

I want to import a class named DataGenerator that is in the current file directory:

/content/gdrive/My Drive/mrcnn_fire/ssd/python_files/object_detection_2d_data_generator.py

I'm importing as follows:

from content.gdrive.My Drive.mrcnn_fire.ssd.python_files.object_detection_2d_data_generator import DataGenerator

It gives me the following error:

File "<ipython-input-3-7d861f8abbeb>", line 4
    from content.gdrive.My Drive.mrcnn_fire.ssd.python_files.object_detection_2d_data_generator import DataGenerator
                               ^
SyntaxError: invalid syntax
Fabio Araujo
  • 309
  • 1
  • 4
  • 13
  • Is your main script _also_ in `/content/gdrive/My Drive/mrcnn_fire/ssd/python_files`? If so, try importing without the path: `from object_detection_2d_data_generator import DataGenerator` – Kevin Apr 16 '19 at 13:15
  • 2
    Possible duplicate of [How do you import a file in python with spaces in the name?](https://stackoverflow.com/questions/9123517/how-do-you-import-a-file-in-python-with-spaces-in-the-name) – Moldovan Daniel Apr 16 '19 at 13:16
  • main script is not in same directory – Fabio Araujo Apr 16 '19 at 13:29
  • The file name is the name of the module, but that means the file name has to obey Python's rules for module names: no spaces allowed. You import *modules*, not arbitrary files. – chepner Apr 16 '19 at 13:31

2 Answers2

1

Also you can try it this way:

# object_detection_2d_data_generator.py
import sys
sys.path.insert(0, '/content/gdrive/My Drive/mrcnn_fire/ssd/python_files')

from object_detection_2d_data_generator import DataGenerator
Fabian
  • 1,130
  • 9
  • 25
1

You can import it this way:

DataGenerator = __import__("content/gdrive/My Drive/mrcnn_fire/ssd/python_files/object_detection_2d_data_generator/DataGenerator")
Anonymous
  • 738
  • 4
  • 14
  • 36