Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I execute an arbitrary script in the context of my Django project?

Sometimes I want to execute a file in the context of my Django project, just as if I were using the shell, but with the convenience of using a text editor. This is mainly to try something out, or quickly prototype some functionality before putting it into a view, test, recurring task, or management command.

I know I can stick these lines at the top of my .py file and it'll run in the Django context:

import sys
sys.path.append('/location/of/projet')
from django.core.management import setup_environ
import settings
setup_environ(settings)

I thought it'd be easier to make a management command that takes on argument, a python module to run, and executes it in the Django environment. Here's the 'runmodule' command I wrote:

from django.core.management.base import BaseCommand, CommandError

class Command(BaseCommand):
    help = "Runs an arbitrary module, in the Django environment, for quick prototyping of code that's too big for the shell."

    def handle(self, *args, **options):
        if not args:
            return
        module_name = args[0]

        try:
            __import__(module_name)
        except ImportError:
            print("Unable to import module %s.  Check that is within Django's PYTHONPATH" % (module_name))

This looks like it works -- I can stick some code in a module, and pass it as an argument to this command, and it'll get executed, e.g.

python manage.py runmodule myapp.trysomethingout

which will execute myapp/trysomethingout.py. Is this the best way to do this?

like image 584
LS55321 Avatar asked Jun 11 '11 20:06

LS55321


2 Answers

The best way to execute a script with the correct django context is to set the DJANGO_SETTINGS_MODULE environment variable to your settings module (and appropriate PYTHONPATH if needed). In windows this usually means executing:

set DJANGO_SETTINGS_MODULE=setting 

and in bash :

export DJANGO_SETTINGS_MODULE=setting 

You can now import your models etc.

see: https://docs.djangoproject.com/en/dev/topics/settings/#designating-the-settings .

Note that in order to import the settings module, one should use from django.conf import settings. This takes into account the DJANGO_SETTINGS_MODULE instead of automatically using settings.py .

like image 99
Udi Avatar answered Nov 02 '22 20:11

Udi


Here's the simplest solution:

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")

import django
django.setup()

Be sure to include this at the top of your python file, before importing any of your models.

Alternatively, you could use the solution pointed out by @beyondfloatingpoint:

import os, sys

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
sys.path.append("mysite")
os.chdir("mysite")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
like image 44
caram Avatar answered Nov 02 '22 20:11

caram