I need to execute a Python script from the Django shell. I tried:
./manage.py shell << my_script.py
But it didn't work. It was just waiting for me to write something.
I need to execute a Python script from the Django shell. I tried:
./manage.py shell << my_script.py
But it didn't work. It was just waiting for me to write something.
Try this if you are using virtual enviroment :-
for using those command you must be inside virtual enviroment. for this use :-
for example :-
Note :- Here mysite is my website name and jango is my virtual enviroment name
@AtulVarma provided a very useful comment under the not-working accepted answer:
I'm late for the party but I hope that my response will help someone: You can do this in your Python script:
the rest of your stuff goes here ....
As other answers indicate but don't explicitly state, what you may actually need is not necessarily to execute your script from the Django shell, but to access your apps without using the Django shell.
This differs a lot Django version to Django version. If you do not find your solution on this thread, answers here -- Django script to access model objects without using manage.py shell -- or similar searches may help you.
I had to begin my_command.py with
and then ran
python3 my_command.py
(Django 2.0.2)