1

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.

James Z
  • 12,209
  • 10
  • 24
  • 44
ChubsKE
  • 11
  • 1
  • 4
  • have you added `ams` to your `installed_apps` – Sahil Mar 14 '20 at 17:07
  • 1
    @Sahil `installed_apps` has nothing to do with resolving Python modules. It only influences what apps Django loads on startup to discover template tags, models, etc. – Brian61354270 Mar 14 '20 at 17:09
  • 1
    Also, I don't think you can have `-` in your Python files – Sahil Mar 14 '20 at 17:09
  • @Sahil Strictly speakding, that is also incorrect. Hyphens are allowed, but modules that include them require extra legwork to import. – Brian61354270 Mar 14 '20 at 17:09
  • Chubs, is `ams` on your Python path? Can you paste in your project structure? – Brian61354270 Mar 14 '20 at 17:10
  • Yes, please post your project code so other users can see where you are going wrong. Also, see this question about hyphens in module names: https://stackoverflow.com/questions/7583652/python-module-with-a-dash-or-hyphen-in-its-name – Hektor Mar 14 '20 at 17:12
  • @Brian https://i.stack.imgur.com/GKDha.png is this what you are asking for?? – ChubsKE Mar 14 '20 at 17:44

3 Answers3

1

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
MK Patel
  • 1,354
  • 1
  • 7
  • 18
0

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
Omid Ostovari
  • 342
  • 2
  • 9
  • I haven't added any models the models.py file. I just created two .py files and I have been trying to import a function from one file. Check this...https://i.stack.imgur.com/GKDha.png – ChubsKE Mar 15 '20 at 09:04
0

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.

ChubsKE
  • 11
  • 1
  • 4