38

I have a script which uses the Django ORM features, amongst other external libraries, that I want to run outside of Django (that is, executed from the command-line).

Edit: At the moment, I can launch it by navigating to a URL...

How do I setup the environment for this?

Peter Mortensen
31.3k22 gold badges110 silver badges134 bronze badges
asked Aug 21, 2009 at 7:33
2

7 Answers 7

43

The easiest way to do this is to set up your script as a manage.py subcommand. It's quite 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 apps under management/commands/yourcommand.py (with empty __init__.py files in each) and now you can call your script with ./manage.py yourcommand.

If you're using Django 1.10 or greater NoArgsCommand has been deprecated. Use BaseCommand instead. https://stackoverflow.com/a/45172236/6022521

Almenon
1,54617 silver badges29 bronze badges
answered Aug 21, 2009 at 8:11
Sign up to request clarification or add additional context in comments.

5 Comments

This is the option that I use, assuming the command is related to your project.
I get the error "NameError: name 'make_option' is not defined" when running this - is something missing?
You probably need from optparse import make_option at the top.
Maybe I missed it, but nobody seems to have pointed to the documentation: docs.djangoproject.com/en/dev/howto/custom-management-commands
NoArgsCommand is deprecated since Django 1.10. Use BaceCommand in stead. stackoverflow.com/a/45172236/6022521
21
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
fjsj
11.3k11 gold badges43 silver badges61 bronze badges
answered Aug 21, 2009 at 7:44

4 Comments

Unfortunately this method does not work if you use a script in \app1\ and need to use files from \app2\ unless you place it in the project root.
I think it should be "import setup_environ" instead of "import setup_environment"
This was deprecated in Django 1.4...long ago.
@mlissner is there a replacement?
14

All you need is importable settings and properly set python path. In the most raw form this can be done by setting up appropriate environment variables, like:

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

There are other ways, like calling settings.configure() and already mentioned setup_environ() described by James Bennett in some blog post.

answered Aug 21, 2009 at 8:00

Comments

13

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

In order to do this, you need to:

  1. pip install django-extensions
  2. Create a directory called scripts. This can be located in your root directory, or in a specific app.
  3. Initialize the directory with a blank init file:

    touch scripts/__init__.py

  4. Place your script in this directory, and include a run() function. Example:

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

Refer to docs and helpful blog post for more details.

answered Jul 31, 2013 at 20:23

Comments

11

Note that the suggestions around importing settings and using setup_environ have been deprecated with Django 1.4.

There's a thread on the Django Github describing why this was done.

There are still some other options out there but many of them seem hackish to me. My preferred method is often to include the scripted function as an extended command of manage.py

answered Aug 26, 2012 at 14:02

Comments

2

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 custom scripts, e.g:

./manage.py run-script /path/to/myscript.py --opt=value arg1 arg2
answered Aug 17, 2015 at 14:12

Comments

1

a simple way:

$ python manage.py shell -c 'import runpy; runpy.run_path("scripts/script_to_run.py")'

where scripts/script_to_run.py is the desired script to run!

you can create a bash script called "runscript.py":

#!/bin/bash
python manage.py shell -c 'import runpy; runpy.run_path("'1ドル'")'

then run:

$ runscript.py scripts/script_to_run.py
answered Jan 29, 2021 at 13:57

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.