Home > Enterprise >  Cannot import from or debug models.py
Cannot import from or debug models.py

Time:08-15

I've created 5 classes in a models.py inside my app city. I successfully created a db in postgres by importing each class inside the console accordingly. Next, I created a jobs.py script inside the city app so I can automatically scrape data using bs4 and populate/update a db accordingly.

The problem occurs when I try to import each class from models.py at the top of my jobs.py script:

from city.models import restaurants, barbers, apartments, bars, parks

Generates:

'Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.'

Any idea why I'm unable to import the classes from models.py into the jobs.py script?

I also created a python package utils inside my project. Inside utils is a name_directory.py script that I'm also unable to import into my jobs.py script.

Also, I just tried debugging my models.py file and it generated the same error!

CodePudding user response:

Please, can you read the manual, before ask?

https://docs.djangoproject.com/en/4.1/topics/settings/#calling-django-setup-is-required-for-standalone-django-usage

if you settings of environment are correct:

import django
django.setup()

# after django.setup() you can start import models from you rdjango  app
  • Related