Running Python script outside of Django

I have a script that uses the Django ORM functions among other external libraries that I want to run outside of Django (i.e. it is executed from the command line).

Edit: At the moment, I can run it by going to the url ...

How to set up the environment for this?

+34
python django
Aug 21 '09 at 7:33
source share
6 answers

The easiest way to do this is to configure the script as a manage.py subcommand. This is pretty easy to do:

 from django.core.management.base import NoArgsCommand, make_option class Command(NoArgsCommand): help = "Whatever you want to print here" option_list = NoArgsCommand.option_list + ( make_option('--verbose', action='store_true'), ) def handle_noargs(self, **options): ... call your script here ... 

Put this in a file in any of your applications running /commands/yourcommand.py (with empty __init__.py files in each), and now you can invoke your script with ./manage.py yourcommand .

+38
Aug 21 '09 at 8:11
source share
 from <Project path> import settings #your project settings file from django.core.management import setup_environ #environment setup function setup_environ(settings) #Rest of your django imports and code go here 
+20
Aug. 21 '09 at 7:44
source share

All you need is importable settings and proper python path setup. In its most raw form, this can be done by setting the appropriate environment variables, for example:

 $ DJANGO_SETTINGS_MODULE=myproject.settings PYTHONPATH=$HOME/djangoprojects python myscript.py 

There are other ways, such as calling settings.configure() and the already mentioned setup_environ() , described by James Bennett on his blog .

+13
Aug 21 '09 at 8:00
source share

Note that suggestions about importing settings and using setup_environ are deprecated with Django 1.4.

There is a thread on Django Github describing why this was done .

There are some more options, but many of them seem to me to be hacks. My preferred method often includes a script function as an extended manage.py command

+10
Aug 26 2018-12-12T00:
source share

Use runscript from django-extensions : python manage.py runscript <my_script>

For this you need:

  • pip install django-extensions
  • Create a directory called scripts . This can be located in the root directory or in a specific application.
  • Initialize the directory with an empty initialization file:

    touch scripts/__init__.py

  • Put your script in this directory and enable the run() function. Example:

     #hello.py def hello(): return "Hello, World" def run(): print hello() 
  • Run the script using python manage.py runscript hello

See docs and a useful blog post for more details.

+10
Jul 31 '13 at 20:23
source share

I like to add the following command to my projects:

myproject/management/commands/run-script.py :

 from django.core.management.base import BaseCommand, CommandError import imp import sys class Command(BaseCommand): help = """Run a non-django script with django settings.""" args = "<path_to_script.py> [<script_args>...]" def handle(self, *args, **options): if len(args) == 0: raise CommandError("Path to script to run is required") sys.argv = list(args) imp.load_source("__main__", args[0]) 

Then I can run my own scripts, for example:

 ./manage.py run-script /path/to/myscript.py --opt=value arg1 arg2 
+1
Aug 17 '15 at 14:12
source share



All Articles