#25680 closed New feature (fixed)
Add support for manage.py shell -e 'run_some_code_as_django()'
Reported by: | Håkan W | Owned by: | Niels Van Och |
---|---|---|---|
Component: | Core (Management commands) | Version: | 1.8 |
Severity: | Normal | Keywords: | |
Cc: | markus.magnuson@… | Triage Stage: | Accepted |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
I'm noticing a trend in my projects that I tend to create management commands that are just wrappers around a simple call that needs to be executed as Django.
Sometimes it would be so much easier to be able to just do this (from Fabric, or a bash script):
python manage.py shell -e 'do_something()'
Attachments (1)
Change History (19)
comment:1 by , 9 years ago
Cc: | added |
---|
comment:2 by , 9 years ago
comment:3 by , 9 years ago
Did you know? You can already do (at least in bash and zsh):
python manage.py shell <<< 'do_something()'
This sends the string in on stdin
which shell
simply interprets the same as typed commands. However it should be noted you can't do this in some shells or from e.g. subprocess.Popen
without shell=True
.
Also, if this does get implemented I'd suggest the -c
flag to mirror python -c
.
comment:4 by , 9 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:5 by , 9 years ago
Component: | Uncategorized → Core (Management commands) |
---|
I'm not convinced about accepting the ticket given the workaround Adam noted.
I think something like python -c "import django; django.setup(); ..."
is also equivalent.
comment:6 by , 9 years ago
I started on a patch for this, I'll finish it up. In my head the feature has some merit.
comment:7 by , 9 years ago
Has patch: | set |
---|
by , 9 years ago
Attachment: | add_command_option_to_shell.diff.txt added |
---|
follow-up: 9 comment:8 by , 9 years ago
Adam's suggestion isn't completely equivalent as it will output the Python prompt too:
$ python manage.py shell <<< 'print(1)' Python 3.5.0 (default, Sep 14 2015, 10:39:54) [GCC 4.8.4] on linux Type "help", "copyright", "credits" or "license" for more information. (InteractiveConsole) >>> 1 >>>
The discussion about deprecating the shell command in #19737 (ultimately closed as wontfix) also left me wondering if we should add more options to it.
I created a django-developers thread to get more feedback.
comment:9 by , 9 years ago
Replying to timgraham:
Adam's suggestion isn't completely equivalent as it will output the Python prompt too:
$ python manage.py shell <<< 'print(1)' Python 3.5.0 (default, Sep 14 2015, 10:39:54) [GCC 4.8.4] on linux Type "help", "copyright", "credits" or "license" for more information. (InteractiveConsole) >>> 1 >>>
More importantly, it "confiscates" stdin, so the called function cannot interact with the user:
$ python <<< 'print input()' Traceback (most recent call last): File "<stdin>", line 1, in <module> EOFError: EOF when reading a line
follow-up: 11 comment:10 by , 9 years ago
Indeed it does confiscate. But interestingly python
alone doesn't output the prompt:
$ python <<< 'print(23 * 7)' 161
So manage.py shell
can maybe do something to avoid outputting the prompt in this case - which is still separate to the case of a flag of course.
comment:11 by , 9 years ago
Replying to adamchainz:
Indeed it does confiscate. But interestingly
python
alone doesn't output the prompt:
$ python <<< 'print(23 * 7)' 161So
manage.py shell
can maybe do something to avoid outputting the prompt in this case - which is still separate to the case of a flag of course.
In the long run it might just be easier to add this flag since we might have to also special case ipython
and bpython
also and from my understanding the <<<
trick won't work on Windows.
comment:13 by , 9 years ago
For me personally, I have a bunch of code in my ./manage.py that setups up the correct virtual environment so for me it takes a little more than django.setup().
comment:14 by , 9 years ago
Triage Stage: | Unreviewed → Accepted |
---|
No strong opposition to the feature request has appeared so far.
comment:15 by , 9 years ago
Patch needs improvement: | set |
---|---|
Summary: | Feature request: support python manage.py shell -e 'run_some_code_as_django()' → Add support for manage.py shell -e 'run_some_code_as_django()' |
Left comments for improvement on the pull request.
comment:16 by , 9 years ago
Patch needs improvement: | unset |
---|
Resolved the comments, except one, which I think can't be fixed.
I would personally use this pretty frequently.