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()'
--
Ticket URL: <https://code.djangoproject.com/ticket/25680>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* cc: markus.magnuson@… (added)
* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0
--
Ticket URL: <https://code.djangoproject.com/ticket/25680#comment:1>
Comment (by collinanderson):
I would personally use this pretty frequently.
--
Ticket URL: <https://code.djangoproject.com/ticket/25680#comment:2>
Comment (by collinanderson):
I would personally use this pretty frequently.
--
Ticket URL: <https://code.djangoproject.com/ticket/25680#comment:3>
Comment (by adamchainz):
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}}}.
--
Ticket URL: <https://code.djangoproject.com/ticket/25680#comment:3>
* owner: nobody => nielsvanoch
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/25680#comment:4>
* component: Uncategorized => Core (Management commands)
Comment:
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.
--
Ticket URL: <https://code.djangoproject.com/ticket/25680#comment:5>
Comment (by nielsvanoch):
I started on a patch for this, I'll finish it up. In my head the feature
has some merit.
--
Ticket URL: <https://code.djangoproject.com/ticket/25680#comment:6>
* has_patch: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/25680#comment:7>
* Attachment "add_command_option_to_shell.diff.txt" added.
* Attachment "add_command_option_to_shell.diff.txt" added.
* Attachment "add_command_option_to_shell.diff.txt" added.
Comment (by 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
>>>
}}}
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 [https://groups.google.com/d/topic/django-
developers/9OQ6mHArOQk/discussion django-developers thread] to get more
feedback.
--
Ticket URL: <https://code.djangoproject.com/ticket/25680#comment:8>
Comment (by shaib):
Replying to [comment:8 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
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/25680#comment:9>
Comment (by adamchainz):
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.
--
Ticket URL: <https://code.djangoproject.com/ticket/25680#comment:10>
Comment (by charettes):
Replying to [comment:10 adamchainz]:
> 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.
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.
--
Ticket URL: <https://code.djangoproject.com/ticket/25680#comment:11>
Comment (by hobarrera):
Why not just
{{{
import django
django.setup()
more_code_here
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/25680#comment:12>
Comment (by collinanderson):
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().
--
Ticket URL: <https://code.djangoproject.com/ticket/25680#comment:13>
* stage: Unreviewed => Accepted
Comment:
No strong opposition to the feature request has appeared so far.
--
Ticket URL: <https://code.djangoproject.com/ticket/25680#comment:14>
* needs_better_patch: 0 => 1
Comment:
Left comments for improvement on the pull request.
--
Ticket URL: <https://code.djangoproject.com/ticket/25680#comment:15>
* needs_better_patch: 1 => 0
Comment:
Resolved the comments, except one, which I think can't be fixed.
--
Ticket URL: <https://code.djangoproject.com/ticket/25680#comment:16>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"7f7553dd30534d606c84952a3f6dcb64b396ce37" 7f7553dd]:
{{{
#!CommitTicketReference repository=""
revision="7f7553dd30534d606c84952a3f6dcb64b396ce37"
Fixed #25680 -- Added django-admin shell --command option.
Add a -c option to the shell command to execute a command passed as a
string as Django.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/25680#comment:17>
Comment (by Mariusz Felisiak <felisiak.mariusz@…>):
In [changeset:"d26d1c196dbb82bf034608576b1019b163086e51" d26d1c1]:
{{{
#!CommitTicketReference repository=""
revision="d26d1c196dbb82bf034608576b1019b163086e51"
Refs #25680 -- Added shell tests for globals available in passed commands.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/25680#comment:18>