Home > Enterprise >  Python: ModuleNotFoundError: No module named 'xyz'
Python: ModuleNotFoundError: No module named 'xyz'

Time:06-01

I have a folder, called ABC that contains a file called file_function.py, i.e.

ABC
    file_function.py

I have a folder in ABC called folder_function that contains a file called main_function.py, i.e.

ABC
    folder_function
        main_function.py

In main_function.py I am calling a function defined in file_function.py called xyz. I am using the following code in main_function.py

from ABC.file_function import xyz

However, this results in ModuleNotFoundError: No module named 'xyz'.

I have tried solutions in other posts with no luck. What could possibly be the issue? Could it be a wrong environment setup? How can I go about debugging this?

Thank you in advance.

CodePudding user response:

file function is not in folder function (main_function's parent), so it will give this error. A similar example is that your python program can't access your photos folder because they are completely different directories!

Try reorganizing the files. If it still doesn't work, than try this:

from ABC import file_function
from file_function import xyz

CodePudding user response:

In order for the files or folders of .py files to be callable with the . notation, they need to be modules. A module is defined by its init.py file at every root. The init.py can be empty, but it needs to exist, in this case in the ABC folder and in the folder_function folder.

More Info

What is __init__.py for?

THoughts on how you might want to structure your project and modules.

  • Related