It looks like the problem is that MyClass
is not located in commons.util
, because you only imported the module named my_class
, not the class itself.
Instead the file commons/util/__init__.py
should contain the following import:
from .my_class import MyClass
I don't think this will solve your problem, because you would be getting a different error than the one shown, but you will get errors for this eventually.
Update
First, I'd recommend reading this answer for a good explanation for how imports work in python.
Basically, when you execute from commons.util import MyClass
, the interpreter scans the contents of sys.path
for a module named commons
.
I assume you didn't set sys.path
to include your project folder, hence the ModuleNotFoundError
.
TLDR; you have 2 options:
- Manually set
sys.path
in run_commands.py
to check your project folder (Don't do this!)
- Use Django's
Command
class
To use Django's Command
class, you will need to adjust your project folder similar to the following:
project
- manage.py
- commons
- management
- commands
run_commands.py
- util
- __init__.py
- my_class.py
Now in run_commands.py:
from django.core.management.base import BaseCommand
from commons.util import MyClass
class Command(BaseCommand):
def handle(*args, **options):
print("handling", MyClass)
You can execute the new command with the following:
python3 manage.py run_commands