Forums

Scheduled Tasks - ImportError: attempted relative import with no known parent package

Hi, I am trying to create a scheduled task, and I need to import from my models file (I am using Django with Python 3.7), I tried importing using from .models and from appName.models, and didn't work both ways. Files structure is:

  • appName
    • models.py
    • task.py

Pleeease help, thank you!

Did you try:

  • from models import ...

or

  • from . import ...

?

Hi,

Thank you, the first one worked perfectly, and now the task.py is importing from the models.py file, but it is giving me errors regarding the imports in my models.py file.

  • File "/appName/task.py", line 1, in <module>
  • from models import myModel
  • File "/appName/models.py", line 13, in <module>
  • from phone_field import PhoneField
  • ModuleNotFoundError: No module named 'phone_field'

Although phone_field is installed and added to the settings.py file and it works perfectly on my website. How can I solve this?

Make sure you're paying attention to the working directory for your task: https://help.pythonanywhere.com/pages/ScheduledTasks/ Also, the theory part of this page will help you to understand how Python finds the modules that you're trying to import.