Question:
Fix Module Not Found Error in Django

Problem


I have created a custom Django command, where I have imported models as shown below


# myapp/management/commands/send_birthday_emails.py

from django.core.management.base import BaseCommand

from django.utils import timezone

from Employeetable.EVENTS.models import Event, EmailTemplate

from django.core.mail import send_mail



class send_birthday_emails(BaseCommand):

    help = 'Send birthday emails to employees'


    def handle(self, *args, **options):

        today = timezone.now()

        current_month = today.month


        # Get employees whose birthday is in the current month

        employees = Event.objects.filter(event_type='Birthday', event_date__month=current_month)


        for employee in employees:

            # Get the email template for birthday greetings

            email_template = EmailTemplate.objects.get(event=employee)


            # Compose and send the email

            subject = email_template.subject

            body = email_template.body

            recipient_email = employee.email


            send_mail(subject, body, 'your.email@example.com', [recipient_email])


            self.stdout.write(self.style.SUCCESS(f'Successfully sent email to {recipient_email}'))


I have used the full path to models.py as you can see in the above imported module list


from Employeetable.EVENTS.models import Event, EmailTemplate


I have used the IDE to give the suggestion on the selection of path, yet I am getting below error


from Employeetable.EVENTS.models import Event, EmailTemplate

ModuleNotFoundError: No module named 'Employeetable.EVENTS'


As per the rule I have placed my custom command file send_birthday_emails inside app/management/commands.


I have attached the screenshot for your reference to show the exact path of the file.



(venv) babu@babu-All-Series:~/PycharmProjects/Employeedata/Employeetable$ ll

total 168

drwxrwxr-x 5 babu babu   4096 Oct  2 15:58 ./

drwxrwxr-x 5 babu babu   4096 Oct  2 11:57 ../

-rw-r--r-- 1 babu babu 147456 Oct  2 15:58 db.sqlite3

drwxrwxr-x 3 babu babu   4096 Oct  2 15:45 Employeetable/

drwxrwxr-x 5 babu babu   4096 Oct  2 16:44 EVENTS/

-rwxrwxr-x 1 babu babu    669 Oct  1 15:10 manage.py*

drwxrwxr-x 3 babu babu   4096 Oct  2 12:17 templates/


This is the location from where I an running the below custom command

python manage.py send_birthday_emails


Solution

I tried to replicate your directory in my own system.

If I am using absolute import from the project directory I get the same error


from EmployeeTable.EVENTS.models import Event, EmailTemplate

ModuleNotFoundError: No module named 'EmployeeTable.EVENTS'


But if I am importing it from the app level like below :


from EVENTS.models import Event, EmailTemplate


It's working fine for me, give it a try once.


Answered by: >madara_was_right

Credit: >StackOverflow


Blog Links

>Sorting the restframework in Django

>Ways to access instances of models in view in order to save both forms at once in Django

>What makes index.html have such kind of name in Django?

>Fix Module Not Found during Deployment- Django

>Creating a Django with existing directories, files, etc.?

>How to Read a CSV file with PHP using cURL?


Nisha Patel

Nisha Patel

Submit
0 Answers