Forums

ModuleNotFoundError: No module named

Given a directory layout (just for example):

my_project/
        bar/
            baz.py
            quux.py
            quuz.py
            eggs.py
        spam.py

On my local machine import works:

eggs.py

from bar.quux import my_function

But why do I get ModuleNotFoundError: No module named "bar" on Pythonanywhere with the same code and folders structure? To make the import working on Pythonanywhere I have to change manually the code to: from quux import my_function

there should be empty __init__.py file in bar dir

Also, take a look at https://help.pythonanywhere.com/pages/DebuggingImportError/

I added init.py file in bar dir. Nothing changed. Import works on a local machine, but cloned to Pythonanywhere project import does not work. While folders structure and files are the same.

Why my subfolder does not see anything outside? I can not import anything from subfolder. It looks like the subfolder 'thinks' that it is the main project folder.

UPD: Each folder in the project thinks that it is the root folder and does not see anything outside. So I can not use any subfolders in my project on Pythonanywhere.

Adding /home/<my_username>/<my_project>/ to the sys.path does not help.

Do you have /home/Aleksey/my_project/bar on the sys.path before /home/Aleksey/my_project/?

print('\n'.join(sys.path)) output:

.
/bin
/usr/lib/python37.zip
/usr/lib/python3.7
/usr/lib/python3.7/lib-dynload
/usr/lib/python3.7/site-packages
/home/Aleksey/my_project

when running a file inside project's subfolder:

from my_project import spameggs

outpput:

Traceback (most recent call last):
  File "/home/Aleksey/my_project/foobar/temp_local.py", line 9, in <module>
    from my_project import spameggs
ModuleNotFoundError: No module named 'my_project'

If /home/Aleksey/my_project is on the python path, then you cannot import my_project - Python looks inside directories that are on sys.path, not at those directories.