pythonfunctionimportmoduledirectory

Python: ModuleNotFoundError: No module named 'xyz'


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.


Solution

  • 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