Timeline for How to execute a Python script from the Django shell?
Current License: CC BY-SA 3.0
17 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Feb 25, 2021 at 9:06 | comment | added | Serhii Kushchenko | I confirm that exec(open('myscript.py').read()) works currently in Django 3.1.5 shell | |
| Jan 14, 2021 at 7:15 | comment | added | Tech | I've used ./manage.py shell < myscript.py with python 3. Worked flawlessly | |
| Jun 2, 2020 at 1:39 | comment | added | Aditya Wirayudha |
if you're doing this on Windows power shell : Get-Content myscript.py | .\manage.py shell
|
|
| Dec 27, 2018 at 2:48 | comment | added | Sean Letendre |
And how can I use command line arguments to myscript.py in this manner?
|
|
| Jun 22, 2017 at 8:00 | history | edited | Jonathan | CC BY-SA 3.0 |
Python3 specific code.
|
| Sep 1, 2016 at 13:41 | comment | added | acidjunk | the above comment even works with an installed ipython :) | |
| Jun 8, 2016 at 15:02 | comment | added | Atul Varma |
Another solution that appears to work for both Python 2.x and 3.x is echo 'import myscript' | python manage.py shell. I've found this can be useful for quick-and-dirty scripts that you only need to run once, without having to go through the cumbersome process of creating a manage.py command.
|
|
| May 13, 2016 at 16:01 | comment | added | Ciro Santilli OurBigBook.com |
1.9.6 passes the stdin to a code.interact github.com/django/django/blob/1.9.6/django/core/management/… | docs.python.org/2/library/code.html Likely the interactive nature of it breaks things. There should be a CLI option of shell to pass stdin to an eval.
|
|
| Apr 23, 2015 at 12:13 | comment | added | peter2108 | @DavidD. The replacement is given in this answer here | |
| Apr 5, 2015 at 13:27 | comment | added | David Dahan | It does not work anymore with Python 3+. Any idea to replace this? | |
| Jul 8, 2013 at 12:24 | vote | accept | Community Bot | ||
| Jul 8, 2013 at 12:24 | vote | accept | Community Bot | ||
| Jul 8, 2013 at 12:24 | |||||
| Jul 5, 2013 at 0:49 | comment | added | Steve Bennett |
For me, this only executes the first line of the script. The only thing that works is combining both methods: ./manage.py shell <<EOF\ execfile('myscript.py') \EOF
|
|
| Jun 28, 2013 at 12:52 | audit | Low quality answers | |||
| Jun 28, 2013 at 12:52 | |||||
| May 31, 2013 at 10:34 | vote | accept | Community Bot | ||
| Jul 8, 2013 at 12:24 | |||||
| May 31, 2013 at 9:22 | vote | accept | Community Bot | ||
| May 31, 2013 at 10:34 | |||||
| May 31, 2013 at 9:13 | history | answered | codeape | CC BY-SA 3.0 |