[Solved]-How to execute manage.py from the Python shell



You could start a Django shell from the command line:

python manage.py shell

Then import execute_from_command_line:

from django.core.management import execute_from_command_line

And finally, you could execute the commands you need:

execute_from_command_line(["manage.py", "syncdb"])

It should solve your issue.

As an alternative, you could also take a look at the subprocess module documentation. You could execute a process and then check its output:

import subprocess
output = subprocess.check_output(["python", "manage.py", "syncdb"])
for line in output.split('\n'):
    # do something with line


Note: this is for interactive usage, not something you could put in production code.

If you’re using ipython, you can do

!python manage.py syncdb

The β€˜!’ says:

I want to execute this as if it is a shell command

If you have pip installed, you can get ipython with:

pip install ipython

which you would want to run at the command line (not in the Python interpreter). You might need to throw a sudo in front of that, depending on how your environment is set up.


Leave a comment