Error importing a file: ModuleNotFoundError: No module named

Viewed 3938

I know there are a lot of questions asking the same I'm now asking, but I've tried most of their answers and they doesn't fix my problem. I have a file named fileA.py where I need to call and import fileB.py. Until now i was doing this:

# in fileA.py
import fileB.py

And it was working. But suddenly now it does not. It gives me this error:

ModuleNotFoundError: No module named 'fileB'

They are on the same folder at the same level. I've tried all of these solutiones:

import folder.fileB
from . import fileB
import fileB.py

And still same error. How can I fix this?

3 Answers

It's better to use absolute imports. Starting from the root, assume you have a folder called folder which holds your modules, you would import it like so:

from folder import fileB

If folder is not the root of the code, then start from the root source folder:

from root_source_folder.some_package.folder import fileB

try

import fileB

or

from fileB import *

Make sure that your present working directory in terminal while executing your code is same as that of fileA.py.

Related