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.
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_plusThen try:
from django.core.management import call_commandcall_command('reset_db', '--help')Anything then?
Also within
./manage.py shell_plus, tryimport django_extensionsOutside 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 usingvenv,virtualenv,virtualenvwrapper,pipenvorpoetry`?Try
env | grep VIRT, do you see aVIRTUAL_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
poetrywe can dopoetry run ./manage.py ourscriptorpoetry run ./ourscript.pywithout needing to be sourced. But we can also easily drop into virtualenv viapoetry 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