Gabriel Saldaña's blog

personal blog, photography and programming

Using Django models in external Python scripts

python snakes

Since Django 1.4 the syntax to import you models in external scripts has changed. The setup_environ method has been deprecated for a while and it no longer exists in Django 1.6.

Assuming you’re using a virtualenv and your script is one directory outside the main Django project directory structure.

  |---> virtualenv/
  |---> my_project/   <--- Django project files

I use this for Django 1.6 integration in

sys.path.append(os.path.join(os.path.dirname(__file__), ‘my_project’))
os.environ.setdefault(“DJANGO_SETTINGS_MODULE”, “my_project.settings”)
from django.conf import settings

from my_app.models import MyModel

About the author

Gabriel Saldaña Gabriel Saldaña is a web developer, photographer and free software advocate. Connect with him on and Twitter

Posted Under

Post navigation

4 thoughts on “Using Django models in external Python scripts

  1. Jefri Yeh says:

    Just want to say thanks for the tips 🙂

  2. Sandibellota says:

    Excellent!!! I was looking for this, 😉 thnx

  3. Manjunath says:

    Awesome, thanks for the post.

  4. jinsu jang says:

    django.core.exceptions.AppRegistryNotReady: Apps aren’t loaded yet.

    I followed what you’re saying exactly. but I got this.
    Please let me know how to solve this problem.

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: