42๐
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
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
- [Django]-Django: signal when user logs in?
- [Django]-Why does django's prefetch_related() only work with all() and not filter()?
- [Django]-Adding model-wide help text to a django model's admin form
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.
- [Django]-Django, filter by specified month and year in date range
- [Django]-Django, template context processors
- [Django]-How do I get multiple values from checkboxes in Django
13๐
Use runscript
from django-extensions: python manage.py runscript <my_script>
In order to do this, you need to:
pip install django-extensions
- Create a directory called
scripts
. This can be located in your root directory, or in a specific app. -
Initialize the directory with a blank init file:
touch scripts/__init__.py
-
Place your script in this directory, and include a
run()
function. Example:#hello.py def hello(): return "Hello, World" def run(): print hello()
-
Run the script with
python manage.py runscript hello
Refer to docs and helpful blog post for more details.
- [Django]-Django: return string from view
- [Django]-Modifying Dictionary in Django Session Does Not Modify Session
- [Django]-Django: Fat models and skinny controllers?
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
- [Django]-HTML โ How to do a Confirmation popup to a Submit button and then send the request?
- [Django]-Django override default form error messages
- [Django]-What is the benefit of using a HyperlinkedModelSerializer in DRF?
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
- [Django]-Django โ view sql query without publishing migrations
- [Django]-Manipulating Data in Django's Admin Panel on Save
- [Django]-How to use schemas in Django?
0๐
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
- [Django]-Add a custom button to a Django application's admin page
- [Django]-Readonly models in Django admin interface?
- [Django]-Django template system, calling a function inside a model