Home > Blockchain >  Python Django ModeulNotFoundError: No module named 'SomeFolder'
Python Django ModeulNotFoundError: No module named 'SomeFolder'

Time:12-17

I have a django app. It is quite complicated, I can't post all the code here, so I have a big picture summary. The setup of the folders in the big picture related to what I'm doing is below. Note that "SomeFolder" is a placeholder name for the actual name. Some folder is a folder containing some python scripts with functions inside of them. It wasn't installed in the Django app. I dragged and dropped SomeFolder into the backend folder. This is necessary and had to be done. It's some python stuff unrelated to Django.

Dashboard
    backend
        SomeFolder
            A.py
            B.py
            C.py
        views.py
    Dashboard
    frontend
    manage.py
    

Inside of views.py at the top, I do

import SomeFolder

Then, inside of views.py, I have a function that calls some of the functions within the python scripts A.py, B.py, and C.py. This is why I need to import SomeFolder. For example:

def someFunction():
    SomeFolder.A_Function_1
    SomeFolder.A_Function_2
    SomeFolder.B_Function_2
    etc.

The issue is, when I run python manage.py runserver I get a ModuleNotFoundError: No module named 'SomeFolder' in views.py. The SomeFolder folder is in the same directory as views.py, so I'm not sure why it's not being recognized. I've seen other similar examples on stackoverflow, but didn't really understand them or how to fix it so I'm posting my own one here.

CodePudding user response:

Try

from . import SomeFolder

You're essentially telling Django to import SomeFolder from the same directory as your views.py file. In this case, I believe that would be backend, which I'm assuming is an app of your project, dashboard.

One way to quickly pick this up is to check for syntax highlighting in the IDE/text editor you are using. If you have successfully imported the folder you want, the function you call from that folder should turn a specific colour. See if you notice that the function is now in a different colour to what it was before.

  • Related