Having trouble running the django extensions command reset_db programatically

370 views Asked by At

Basically I am writing a script to reset a django webapp completely. In this script, I want to reset the database, and there is a command to do it from django extensions. Unfortunately, I haven't been able to run it programatically. It works fine when I run it via command line, but it just won't execute when I try programatically.

I have tried using os.system and subprocess.

I have also tried using management.call_command('reset_db'), but it keeps saying that there isn't a command called reset_db. I have checked to make sure the django_extensions is in my installed apps, so I have no idea why that isn't working.

Does anyone know how I could fix this? Thank you!

Also I am using python3, the most recent version of django I believe, and it is a MYSQL server that I am trying to delete.

1

There are 1 answers

0
tony On

I can't know without seeing your way of invocation directly, but my guess is the script's not running in the virtualenv. Here are some debug notes:

./manage.py --help | grep reset_db: Does this output anything?

./manage.py shell_plus

Then try:

  • from django.core.management import call_command
  • call_command('reset_db', '--help')

Anything then?

Also within ./manage.py shell_plus, try import django_extensions

Outside of the shell, try this: pip show django, pip django-extensions.

If it doesn't show those (e.g. WARNING: Package(s) not found: django-extension) and you think they're already installed, try this:

which python, which pip. Are you using venv, virtualenv, virtualenvwrapper, pipenvorpoetry`?

Try env | grep VIRT, do you see a VIRTUAL_ENV? If not you may need to make one.

When you run the script, you need to have your environmental variables set so you hook in to your site packages. In poetry we can do poetry run ./manage.py ourscript or poetry run ./ourscript.py without needing to be sourced. But we can also easily drop into virtualenv via poetry shell.

If you created an environment like virtualenv -ppython3.8 .venv, you can either do:

source .venv/bin/activate, ./myscript.py, rr you can try .venv/bin/python ./myscript.py