1

I have a file in my management/commands folder and I am trying to import my models into the report.py folder. However, I get the following error.

Traceback (most recent call last):
  File "c:\Users\Timmeh\source\Python\Django Projects\env\topxgym\members\management\commands\report.py", line 2, in <module>
    from members.models import ActiveMember
ModuleNotFoundError: No module named 'members'

I have a active.py file in that same folder with the same import and it works. However, the active.py is run from python manage.py active. The report.py I am just compiling/running it (not running it from python manage.py). That is the only difference.

the file structure. enter image description here

wormy505
  • 75
  • 8
  • And what is the Import Error messages you receive? The error is expected since running `report.py` directly sets the work dir to the directory which contains this file. To work around you'll need to change the import to a relative import. Let us know the error message exactly for further comments. – P S Solanki Aug 27 '22 at 14:07
  • The error message is the traceback in the beginning of my post. says line 2 has a problem. from members.models import ActiveMember ---- No module named 'members' which is the folder's name which has my models.py file. That is the old error message I am getting. – wormy505 Aug 27 '22 at 14:18

2 Answers2

0

Please don't forget to add __init__.py for every folder.

More here: https://docs.python.org/3/reference/import.html#regular-packages

Try to made relative import.

from ..models import ActiveMember

if it works, this answer can help: Absolute imports in python not working, relative imports work

Try to check your project settings.

Have you add yors members app in settings.INSTALLED_APPS list?

Maxim Danilov
  • 2,472
  • 1
  • 3
  • 8
  • Hi Maxim, I thought in python3 it's no longer required? However, if you look at the picture every level has a __init__.py management/commands both have __init__.py files. and members is created by django it's and app so it has that file automatically. – wormy505 Aug 27 '22 at 14:38
  • sorry, you are right. My IDE shows me non-module folder and if import is not possible. A add info in Answer – Maxim Danilov Aug 27 '22 at 15:37
0

Here is a dirty hack which should solve the issue.

and I'll also point out an alternate one if that suits your use case.

First Option

import sys
sys.path.append(r'c:\Users\Timmeh\source\Python\Django Projects\env\topxgym')

# now your import should work
from members.models import ActiveMember

This is dirty because it manipulates sys path which is not a good practice. I'm assuming this script is just a standalone script you wish to run, hence it should not be an issue overall.

Second Option

IFF this is a possibility, you could just move your report.py and keep it inside topxgym folder (at the same level as requirements.txt)

In that case too, the same import statement would work.

side note

If you're looking to access the ORM from a standalone script, it looks like you might be missing to add the django configuration in the script as recommended by the documentation.

After your import issues are fixed by either of the above two methods, if it works, great. If it doesn't it would mean that you have to add the django.setup() config and specify a settings file.

P S Solanki
  • 1,033
  • 2
  • 11
  • 26
  • Thanks for your help. However, the problem was solved when I installed django Globally not inside my venv. – wormy505 Aug 28 '22 at 13:32