Opened 4 years ago

Closed 4 years ago

#18204 closed Bug (duplicate)

manage.py shell w/ IPython embed does not work well

Reported by: liokm Owned by: nobody
Component: Core (Management commands) Version: 1.4
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description (last modified by Claude Paroz)

#17078 introduced the usage of IPython.embed(). This also introduced the bug described in the bottom part of https://github.com/ipython/ipython/issues/62 to the IPython shell opened by Django. For example:

    >>> def foo():
    ...     print foo
    >>> foo()
    ...
    NameError: global name 'foo' is not defined

The issue https://github.com/ipython/ipython/issues/62 just solved for normal IPython shell, not for embed calling from function, which is used in current Django, as in django/core/management/commands/shell.py

    def ipython(self):
        ...
            from IPython import embed
            embed()
        ...

Some quotes of takluyver from IPython team in the github link above

It's possible for Django to start IPython in a way that won't cause this problem, but that's not the way we currently make easy. The problem occurs when IPython starts with separate local and global namespaces. There's no reason that Django requires separate local and global namespaces, but that's what calling embed() inside a function implies.

My env: Django 1.4 and 1.5 alpha w/ IPython 0.12 and 0.12.1

Attachments (2)

embed-ipython-module.2.diff (647 bytes) - added by Alex Ogier 4 years ago.
embed-ipython-module.diff (647 bytes) - added by Alex Ogier 4 years ago.

Download all attachments as: .zip

Change History (9)

comment:1 Changed 4 years ago by liokm <liokmkoil@…>

Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset

Ah, sorry for bad formatting w/o preview, could someone help to fix it?

comment:2 Changed 4 years ago by Claude Paroz

Description: modified (diff)

comment:3 Changed 4 years ago by Alex Ogier

Has patch: set

Here is a stab at fixing this. The workaround is to put the embed code in its own module, so that it has its own namespace. It's ugly to have to put it in source form in the utils tree.

Errr, while typing this, I had a brainwave. Give me a sec.

comment:4 Changed 4 years ago by Alex Ogier

Much better way of fixing this. Basically, execute embed() in a new global context via the "exec ... in ..." construct.

I will try bpython as well, to see if they have the same issue.

comment:5 Changed 4 years ago by Alex Ogier

Nope, bpython seems immune. They probably do something like this internally.

Changed 4 years ago by Alex Ogier

Attachment: embed-ipython-module.2.diff added

Changed 4 years ago by Alex Ogier

Attachment: embed-ipython-module.diff added

comment:6 Changed 4 years ago by Jannis Leidel

Patch needs improvement: set
Triage Stage: UnreviewedAccepted

I'd rather use an own module to handle this, instead of using exec or eval.

comment:7 Changed 4 years ago by Ramiro Morales

Resolution: duplicate
Status: newclosed

Closing this as a duplicate of the now revived #17078

Note: See TracTickets for help on using tickets.
Back to Top