ChubsKE
ChubsKE

Reputation: 11

Django - ModuleNotFoundError

I'm trying to import a python file from another python file in Django. There is a function I want to call from the other file, but every time I import the file I get an error.

Traceback (most recent call last):
  File "ams/faces-train.py", line 6, in <module>
    from ams.conn import conn
ModuleNotFoundError: No module named 'ams'

What the problem is? I have tried everything but I have not been able to fix it.

Upvotes: 1

Views: 685

Answers (3)

Omid Ostovari
Omid Ostovari

Reputation: 372

You may need to take a look at the example below

When you have the following directory and You are in the my_app directory, for example, you want to import My_modelclass from models.py to admin.py.

03/14/2020  10:04 PM    <DIR>          .
03/14/2020  10:04 PM    <DIR>          ..
03/14/2020  10:37 PM    <DIR>          .idea
03/12/2020  09:45 PM    <DIR>          env
03/14/2020  05:33 PM    <DIR>        > my_project
                                          __init__.py
                                          asgi.py
                                          settings.py
                                          urls.py
                                          wsgi.py
03/14/2020  10:06 PM    <DIR>         > my_app
                                          > migrations 
                                          __init__.py
                                          admin.py
                                          apps.py
                                          models.py
                                          tests.py
                                          urls.py
                                          views.py
03/12/2020  09:49 PM                   manage.py
03/14/2020  05:34 PM                   requirements.txt
03/14/2020  10:04 PM                   db.sqlite3

for example in this case that I created my_project with this format django-admin startproject my_project . you should import My_model class into the first line of admin.py in this format

from .models import my_model

Upvotes: 0

ChubsKE
ChubsKE

Reputation: 11

I was finally able to find a solution via a friend's assistance. The import statement should look like this.

from check import con

Since they are in the same package an all modules inside a package are visible to each other, it is not necessary to include the app_name. The editor may show warnings. In my case it highlighted the check as unresolved import 'check' but I was able to execute and it worked nonetheless.

Upvotes: 0

Manan M.
Manan M.

Reputation: 1394

please try to import like as below...

from project_name.app_name.file_name import import_variable_or_funtion

in your case...

from project_name.ams.conn import conn

Upvotes: 1

Related Questions