[Django] #25680: Feature request: support python manage.py shell -e 'run_some_code_as_django()'

15 views
Skip to first unread message

Django

unread,
Nov 5, 2015, 6:06:05 AM11/5/15
to django-...@googlegroups.com
#25680: Feature request: support python manage.py shell -e
'run_some_code_as_django()'
-------------------------------+--------------------
Reporter: hakanw | Owner: nobody
Type: New feature | Status: new
Component: Uncategorized | Version: 1.8
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------
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()'

--
Ticket URL: <https://code.djangoproject.com/ticket/25680>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Nov 5, 2015, 6:08:17 AM11/5/15
to django-...@googlegroups.com
#25680: Feature request: support python manage.py shell -e
'run_some_code_as_django()'
-------------------------------+--------------------------------------

Reporter: hakanw | Owner: nobody
Type: New feature | Status: new
Component: Uncategorized | Version: 1.8
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by giuliettamasina):

* cc: markus.magnuson@… (added)
* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/25680#comment:1>

Django

unread,
Nov 5, 2015, 7:33:00 AM11/5/15
to django-...@googlegroups.com
#25680: Feature request: support python manage.py shell -e
'run_some_code_as_django()'
-------------------------------+--------------------------------------

Reporter: hakanw | Owner: nobody
Type: New feature | Status: new
Component: Uncategorized | Version: 1.8
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------

Comment (by collinanderson):

I would personally use this pretty frequently.

--
Ticket URL: <https://code.djangoproject.com/ticket/25680#comment:2>

Django

unread,
Nov 5, 2015, 7:33:45 AM11/5/15
to django-...@googlegroups.com
#25680: Feature request: support python manage.py shell -e
'run_some_code_as_django()'
-------------------------------+--------------------------------------

Reporter: hakanw | Owner: nobody
Type: New feature | Status: new
Component: Uncategorized | Version: 1.8
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------

Comment (by collinanderson):

I would personally use this pretty frequently.

--
Ticket URL: <https://code.djangoproject.com/ticket/25680#comment:3>

Django

unread,
Nov 7, 2015, 4:58:20 AM11/7/15
to django-...@googlegroups.com
#25680: Feature request: support python manage.py shell -e
'run_some_code_as_django()'
-------------------------------+--------------------------------------

Reporter: hakanw | Owner: nobody
Type: New feature | Status: new
Component: Uncategorized | Version: 1.8
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------

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}}}.

Django

unread,
Nov 7, 2015, 5:08:06 AM11/7/15
to django-...@googlegroups.com
#25680: Feature request: support python manage.py shell -e
'run_some_code_as_django()'
-------------------------------+---------------------------------------
Reporter: hakanw | Owner: nielsvanoch
Type: New feature | Status: assigned
Component: Uncategorized | Version: 1.8
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+---------------------------------------
Changes (by nielsvanoch):

* owner: nobody => nielsvanoch
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/25680#comment:4>

Django

unread,
Nov 7, 2015, 5:50:50 AM11/7/15
to django-...@googlegroups.com
#25680: Feature request: support python manage.py shell -e
'run_some_code_as_django()'
-------------------------------------+-------------------------------------

Reporter: hakanw | Owner:
| nielsvanoch
Type: New feature | Status: assigned
Component: Core (Management | Version: 1.8
commands) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* 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>

Django

unread,
Nov 7, 2015, 6:05:54 AM11/7/15
to django-...@googlegroups.com
#25680: Feature request: support python manage.py shell -e
'run_some_code_as_django()'
-------------------------------------+-------------------------------------
Reporter: hakanw | Owner:
| nielsvanoch
Type: New feature | Status: assigned
Component: Core (Management | Version: 1.8
commands) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

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>

Django

unread,
Nov 7, 2015, 6:26:46 AM11/7/15
to django-...@googlegroups.com
#25680: Feature request: support python manage.py shell -e
'run_some_code_as_django()'
-------------------------------------+-------------------------------------
Reporter: hakanw | Owner:
| nielsvanoch
Type: New feature | Status: assigned
Component: Core (Management | Version: 1.8
commands) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by nielsvanoch):

* has_patch: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/25680#comment:7>

Django

unread,
Nov 7, 2015, 6:27:32 AM11/7/15
to django-...@googlegroups.com
#25680: Feature request: support python manage.py shell -e
'run_some_code_as_django()'
-------------------------------------+-------------------------------------
Reporter: hakanw | Owner:
| nielsvanoch
Type: New feature | Status: assigned
Component: Core (Management | Version: 1.8
commands) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by nielsvanoch):

* Attachment "add_command_option_to_shell.diff.txt" added.

Django

unread,
Nov 7, 2015, 8:58:38 AM11/7/15
to django-...@googlegroups.com
#25680: Feature request: support python manage.py shell -e
'run_some_code_as_django()'
-------------------------------------+-------------------------------------
Reporter: hakanw | Owner:
| nielsvanoch
Type: New feature | Status: assigned
Component: Core (Management | Version: 1.8
commands) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by nielsvanoch):

* Attachment "add_command_option_to_shell.diff.txt" added.


Django

unread,
Nov 7, 2015, 8:23:14 PM11/7/15
to django-...@googlegroups.com
#25680: Feature request: support python manage.py shell -e
'run_some_code_as_django()'
-------------------------------------+-------------------------------------
Reporter: hakanw | Owner:
| nielsvanoch
Type: New feature | Status: assigned
Component: Core (Management | Version: 1.8
commands) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by nielsvanoch):

* Attachment "add_command_option_to_shell.diff.txt" added.


Django

unread,
Nov 9, 2015, 3:30:30 PM11/9/15
to django-...@googlegroups.com
#25680: Feature request: support python manage.py shell -e
'run_some_code_as_django()'
-------------------------------------+-------------------------------------
Reporter: hakanw | Owner:
| nielsvanoch
Type: New feature | Status: assigned
Component: Core (Management | Version: 1.8
commands) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

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>

Django

unread,
Nov 9, 2015, 9:54:26 PM11/9/15
to django-...@googlegroups.com
#25680: Feature request: support python manage.py shell -e
'run_some_code_as_django()'
-------------------------------------+-------------------------------------
Reporter: hakanw | Owner:
| nielsvanoch
Type: New feature | Status: assigned
Component: Core (Management | Version: 1.8
commands) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

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>

Django

unread,
Nov 10, 2015, 5:33:58 PM11/10/15
to django-...@googlegroups.com
#25680: Feature request: support python manage.py shell -e
'run_some_code_as_django()'
-------------------------------------+-------------------------------------
Reporter: hakanw | Owner:
| nielsvanoch
Type: New feature | Status: assigned
Component: Core (Management | Version: 1.8
commands) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

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>

Django

unread,
Nov 10, 2015, 5:44:55 PM11/10/15
to django-...@googlegroups.com
#25680: Feature request: support python manage.py shell -e
'run_some_code_as_django()'
-------------------------------------+-------------------------------------
Reporter: hakanw | Owner:
| nielsvanoch
Type: New feature | Status: assigned
Component: Core (Management | Version: 1.8
commands) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

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>

Django

unread,
Nov 11, 2015, 9:53:54 PM11/11/15
to django-...@googlegroups.com
#25680: Feature request: support python manage.py shell -e
'run_some_code_as_django()'
-------------------------------------+-------------------------------------
Reporter: hakanw | Owner:
| nielsvanoch
Type: New feature | Status: assigned
Component: Core (Management | Version: 1.8
commands) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by hobarrera):

Why not just

{{{
import django
django.setup()

more_code_here
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/25680#comment:12>

Django

unread,
Nov 12, 2015, 9:10:53 AM11/12/15
to django-...@googlegroups.com
#25680: Feature request: support python manage.py shell -e
'run_some_code_as_django()'
-------------------------------------+-------------------------------------
Reporter: hakanw | Owner:
| nielsvanoch
Type: New feature | Status: assigned
Component: Core (Management | Version: 1.8
commands) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

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>

Django

unread,
Nov 13, 2015, 12:26:44 PM11/13/15
to django-...@googlegroups.com
#25680: Feature request: support python manage.py shell -e
'run_some_code_as_django()'
-------------------------------------+-------------------------------------
Reporter: hakanw | Owner:
| nielsvanoch
Type: New feature | Status: assigned
Component: Core (Management | Version: 1.8
commands) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* 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>

Django

unread,
Nov 19, 2015, 10:50:14 AM11/19/15
to django-...@googlegroups.com
#25680: Add support for manage.py shell -e 'run_some_code_as_django()'

-------------------------------------+-------------------------------------
Reporter: hakanw | Owner:
| nielsvanoch
Type: New feature | Status: assigned
Component: Core (Management | Version: 1.8
commands) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* needs_better_patch: 0 => 1


Comment:

Left comments for improvement on the pull request.

--
Ticket URL: <https://code.djangoproject.com/ticket/25680#comment:15>

Django

unread,
Nov 21, 2015, 3:27:46 PM11/21/15
to django-...@googlegroups.com
#25680: Add support for manage.py shell -e 'run_some_code_as_django()'
-------------------------------------+-------------------------------------
Reporter: hakanw | Owner:
| nielsvanoch
Type: New feature | Status: assigned
Component: Core (Management | Version: 1.8
commands) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by nielsvanoch):

* 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>

Django

unread,
Jan 6, 2016, 6:44:12 PM1/6/16
to django-...@googlegroups.com
#25680: Add support for manage.py shell -e 'run_some_code_as_django()'
-------------------------------------+-------------------------------------
Reporter: hakanw | Owner:
| nielsvanoch
Type: New feature | Status: closed

Component: Core (Management | Version: 1.8
commands) |
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham <timograham@…>):

* 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>

Django

unread,
Nov 11, 2020, 4:55:06 AM11/11/20
to django-...@googlegroups.com
#25680: Add support for manage.py shell -e 'run_some_code_as_django()'
-------------------------------------+-------------------------------------
Reporter: Håkan W | Owner: Niels Van
| Och

Type: New feature | Status: closed
Component: Core (Management | Version: 1.8
commands) |
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

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>

Reply all
Reply to author
Forward
0 new messages