Python history won't work in Django shell (maybe found bug?)

346 views
Skip to first unread message

Todd O'Bryan

unread,
Nov 13, 2007, 4:48:05 PM11/13/07
to django-d...@googlegroups.com
I finally decided I wanted to get a history working in the Python
shell, which I managed by using Collin Grady's script here:
http://collingrady.com/2006/11/17/tab-complete-and-a-persistent-history-in-python/
Unfortunately, it wouldn't work in Django's shell launched by ./manage.py.

I started poking around and noticed that in
django.core.management.commands.shell, there's line 50 or so:

if not use_plain:
# here's where it uses PYTHONSTARTUP and stuff

Why the heck is that "if not use_plain" there? If I take it out,
Collin's lovely history script works, even in the Django shell. And I
can't figure out what it's there for. Should I submit a patch, or is
there a reason it exists?

Todd

Deryck Hodge

unread,
Nov 13, 2007, 6:02:56 PM11/13/07
to django-d...@googlegroups.com
On Nov 13, 2007 3:48 PM, Todd O'Bryan <toddo...@gmail.com> wrote:
> if not use_plain:
> # here's where it uses PYTHONSTARTUP and stuff
>
> Why the heck is that "if not use_plain" there? If I take it out,
> Collin's lovely history script works, even in the Django shell. And I
> can't figure out what it's there for. Should I submit a patch, or is
> there a reason it exists?

It's the variable that says whether or not the option --plain was
used. It determines whether the normal Python shell is used or
iPython. iPython is used if available unless --plain is set.

Cheers,
deryck

Collin Grady

unread,
Nov 13, 2007, 6:24:15 PM11/13/07
to django-d...@googlegroups.com
Deryck Hodge said the following:

> It's the variable that says whether or not the option --plain was
> used. It determines whether the normal Python shell is used or
> iPython. iPython is used if available unless --plain is set.

Hmm, that sounds like a bug then, as this happens even when ipython
isn't present, so it should be parsing the normal stuff in that case, right?

--
Collin Grady

A robin redbreast in a cage
Puts all Heaven in a rage.
-- Blake

Todd O'Bryan

unread,
Nov 14, 2007, 5:02:16 AM11/14/07
to django-d...@googlegroups.com
I submitted a patch as http://code.djangoproject.com/ticket/5936

Should be an easy change, I hope.

Deryck Hodge

unread,
Nov 14, 2007, 8:17:57 AM11/14/07
to django-d...@googlegroups.com
On Nov 14, 2007 4:02 AM, Todd O'Bryan <toddo...@gmail.com> wrote:
>
> I submitted a patch as http://code.djangoproject.com/ticket/5936
>
> Should be an easy change, I hope.
>
>

Hi, Todd.

Were you using --plain and still wanting the history and tab
completion, or was use_plain being incorrectly set? If use_plain was
causing PYTHONSTARTUP to not be recognized, but you didn't use
--plain, then we need to figure out why that is, rather than just
removing the use_plain check.

If you were using --plain, and just want to have PYTHONSTARTUP
recognized with the plain shell, then the patch makes sense to me.

Cheers,
deryck

Todd O'Bryan

unread,
Nov 14, 2007, 10:04:13 AM11/14/07
to django-d...@googlegroups.com
Good point. In fact, I wasn't using --plain, but PYTHONSTARTUP wasn't
being respected anyway. Any ideas why the heck that should happen?

At any rate, I don't think it matters whether you use --plain or not.
If you use --plain, you'd expect to get normal behavior for typing
python at a terminal prompt, which does use PYTHONSTARTUP. If you
don't use --plain and don't have IPython installed, why should you not
just get the normal behavior you'd expect from using plain Python,
i.e., respecting the PYTHONSTARTUP environment variable?

Todd

Deryck Hodge

unread,
Nov 14, 2007, 10:17:58 AM11/14/07
to django-d...@googlegroups.com
Hi, Todd. So this is two issues, then.

On Nov 14, 2007 9:04 AM, Todd O'Bryan <toddo...@gmail.com> wrote:
>
> Good point. In fact, I wasn't using --plain, but PYTHONSTARTUP wasn't
> being respected anyway. Any ideas why the heck that should happen?

No idea, sorry. :-) I'd attach with pdb and walk through how the
options are being set and try to deduce why you're getting this
behavior. That's how I would handle it, but others will certainly
have there own ideas.

This is the real bug here, so it would be nice to see why it happens for you.

>
> At any rate, I don't think it matters whether you use --plain or not.
> If you use --plain, you'd expect to get normal behavior for typing
> python at a terminal prompt, which does use PYTHONSTARTUP. If you
> don't use --plain and don't have IPython installed, why should you not
> just get the normal behavior you'd expect from using plain Python,
> i.e., respecting the PYTHONSTARTUP environment variable?
>

I would agree with this, too. I think the initial thought when this
was added during the last sprint was that plain implies nothing extra,
but really I think it should be the plain shell as you have it setup
by honoring PYTHONSTARTUP or pythonrc.

It might be nice to update the ticket to point to this discussion with
a note summarizing, since it's not really the real bug.

Cheers,
deryck

Todd O'Bryan

unread,
Nov 18, 2007, 11:34:32 PM11/18/07
to django-d...@googlegroups.com
Think I figured out the problem. Ubuntu doesn't look at
/etc/profile--that's why I wasn't getting PYTHONSTARTUP, even when I
didn't use --plain. Moving my export PYTHONSTARTUP line to
/etc/bash.bashrc worked.

So the issue now is that shell --plain doesn't use PYTHONSTARTUP,
which both Deryck and I think it should. :-) I've modified the ticket
to reflect the real state of affairs.

Reply all
Reply to author
Forward
0 new messages