Home > other >  Python script not run in Django environment
Python script not run in Django environment

Time:04-15

Terminal says that i didn't defined DJANGO_SETTINGS_MODULE but i tried as i thought every method to do it and so far nothing helper (coding on windows) Can someone help me out please? I am stuck and dont know what to do Maby this will help - i run my virtual env through Anaconda -

conda activate djangoenv





raise ImproperlyConfigured(django.core.exceptions.ImproperlyConfigured: 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.
from faker import Faker
from app.models import AccessRecrod, Webpage, Topic
import random
import django
import os

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project.settings')


django.setup()


fakegen = Faker()
topics = ['Search', 'Social', 'Marketplace', 'News', 'Games']


def add_topic():
    t = Topic.objects.get_or_create(top_name=random.choice(topics))[0]
    t.save()
    return t


def populate(N=5):

    for entry in range(N):

        # get topic for entry

        top = add_topic()

        # create fake data for entry

        fake_url = fakegen.url()
        fake_date = fakegen.date()
        fake_name = fakegen.company()

        # create new webpage entry

        webpg = Webpage.objects.get_or_create(
            topic=top, url=fake_url, name=fake_name)[0]

        # create fake access record

        acc_rec = AccessRecrod.objects.get_or_create(
            name=webpg, date=fake_date)[0]


if __name__ == '__main__':
    print('populating script!')
    populate(20)
    print('populating complete!')

CodePudding user response:

The Problem is simply your import of models. You just need to setup django before you import anything related to django:

import django
import os

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project.settings')
django.setup()


from faker import Faker
from app.models import AccessRecrod, Webpage, Topic
import random

rest of your code

If you check the code in manage.py and django code on github you will essentially find the same sequence of code (besides a lot of additional stuff) when you run a management command by "python manage.py your_command"

To use the management command directly as described in the answer from @Code-Apprentice would be the other "normal" or "more django way" to integrate your script.

But you asked for the reason of the error message in your way of doing it.

If you study the complete error trace you will also find that it starts at the import command.

CodePudding user response:

Instead of trying to shoe horn a regular python script into the Django environment like this, I suggest you create a command which you can run with ./manage.py. You can learn how to create your own custom commands here. When you do this correctly, you can run a command like ./manage.py create_topics or whatever name you give your command. This allows manage.py to load the Django environment for you.

Alternatively, you could look at creating fixtures with ./manage.py dumpdata and ./manage.py loaddata. These commands will allow you to create data and save them to a .json file which you can load into your database at any time.

  • Related