How to execute a Python script from the Django shell?

How to execute a Python script from the Django shell?

I need to execute a Python script from the Django shell. I tried:
./ shell <<

But it didn't work. It was just waiting for me to write something.


Answer 1:

The << part is wrong, use < instead:

$ ./ shell <

You could also do:

$ ./ shell
>>> execfile('')

For python3 you would need to use

>>> exec(open('').read())

Answer 2:

You're not recommended to do that from the shell - and this is intended as you shouldn't really be executing random scripts from the django environment (but there are ways around this, see the other answers).

If this is a script that you will be running multiple times, it's a good idea to set it up as a custom command ie

 $ ./ my_command

to do this create a file in a subdir of management and commands of your app, ie


and in this file define your custom command (ensuring that the name of the file is the name of the command you want to execute from ./

from import BaseCommand

class Command(BaseCommand):
    def handle(self, **options):
        # now do the things that you want with your models here

Answer 3:

For anyone using Django 1.7+, it seems that simply import the settings module is not enough.

After some digging, I found this Stack Overflow answer:

You now need to:

import os, django
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp.settings")
# now your code can go here...

Without doing the above, I was getting a django.core.exceptions.AppRegistryNoReady error.

My script file is in the same directory as my django project (ie. in the same folder as

Answer 4:

I'm late for the party but I hope that my response will help someone:
You can do this in your Python script:

import sys, os
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
from django.conf import settings

the rest of your stuff goes here ....

Answer 5:

runscript from django-extensions

python runscript

A sample to test it out:

from django.contrib.auth.models import User

Mentioned at: and documented at:

python runscript --help

There is a tutorial too.

Tested on Django 1.9.6, django-extensions 1.6.7.

Answer 6:

If IPython is available (pip install ipython) then ./ shell will automatically use it's shell and then you can use the magic command %run:


Answer 7:

You can just run the script with the DJANGO_SETTINGS_MODULE environment variable set. That's all it takes to set up Django-shell environment.

This works in Django >= 1.4

Answer 8:

@AtulVarma provided a very useful comment under the not-working accepted answer:

echo 'import myscript' | python shell

Answer 9:

if you have not a lot commands in your script use it: shell --command="import django; print(django.__version__)"

Django docs

Answer 10:

As other answers indicate but don't explicitly state, what you may actually need is not necessarily to execute your script from the Django shell, but to access your apps without using the Django shell.

This differs a lot Django version to Django version. If you do not find your solution on this thread, answers here --
Django script to access model objects without using shell
-- or similar searches may help you.

I had to begin with

import os,sys
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.file")
import django

#do things with my models, yay

and then ran python3

(Django 2.0.2)

Answer 11:

import os, sys, django
os.environ["DJANGO_SETTINGS_MODULE"] = "settings"
sys.path.insert(0, os.getcwd())


Answer 12:

Note, this method has been deprecated for more recent versions of django! (> 1.3)

An alternative answer, you could add this to the top of

from import setup_environ
import settings

and execute just with python in the directory where you have but this is a bit hacky.

$ python

Answer 13:

Try this if you are using virtual enviroment :-

python shell

for using those command you must be inside virtual enviroment. for this use :-

workon vir_env_name

for example :-

dc@dc-comp-4:~/mysite$ workon jango
(jango)dc@dc-comp-4:~/mysite$ python shell
Python 2.7.6 (default, Mar 22 2014, 22:59:56) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.

Note :- Here mysite is my website name and jango is my virtual enviroment name

Answer 14:

If you want to run in in BG even better:

nohup echo 'exec(open("").read())' | python shell &

The output will be in nohup.out