Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

8
  • 2
    My setup has the settings file already referenced in the DJANGO_SETTINGS_MODULE environment variable. With that it was enough to just: import django ; django.setup() under 1.7. Commented Jun 19, 2015 at 19:50
  • 3
    With django 1.7+ this should be the accepted answer :) Commented Jan 14, 2016 at 13:39
  • 1
    This is definitively the correct answer for Django 1.9.x. Not running django.setup() will not let the script access Django models, not even import them! Commented Jun 26, 2017 at 19:50
  • 2
    You made my day! Thanks for this post. i was getting creazy on celery because i was not able to exec tasks onto management.call_command :D :D :D Commented Jan 18, 2019 at 23:20
  • 1
    This is very helpful to get started making a stand alone script that runs in Django environment. You may also want import sys and sys.path.insert(0, ".") and sys.path.insert(0, "../lib") or similar to be able to access libraries at different locations outside the start directory. The settings module name is just the directory names on the path to settings.py separated by . instead of /. Commented Mar 16, 2020 at 11:42