[Answered ]-Django "ImproperlyConfigured", but not always

2πŸ‘

βœ…

If it’s a standalone script, you need to setup Django settings first. In this case, manage.py has nothing to do with it because you are not using it to call your script. To fix this, you have two options:

  1. Write this at the top of your script:

    sys.path.append("/path/to/project") #Set it to the root of your project
    os.environ["DJANGO_SETTINGS_MODULE"] = "<project>.settings"
    django.setup()
    

    After that, you should be able to import your models or whatever you need from your project. You can check this for more detailed explanation if you want.

  2. Create a custom management command so you will be able to call python manage.py mycommand.

Leave a comment