Opened 4 years ago

Closed 4 years ago

#18204 closed Bug (duplicate) 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 claudep)

#17078 introduced the usage of IPython.embed(). This also introduced the bug described in the bottom part of to the IPython shell opened by Django. For example:

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

The issue just solved for normal IPython shell, not for embed calling from function, which is used in current Django, as in django/core/management/commands/

    def ipython(self):
            from IPython import 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 ogier 4 years ago.
embed-ipython-module.diff (647 bytes) - added by 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 claudep

  • Description modified (diff)

comment:3 Changed 4 years ago by 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 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 ogier

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

Changed 4 years ago by ogier

Changed 4 years ago by ogier

comment:6 Changed 4 years ago by jezdez

  • Patch needs improvement set
  • Triage Stage changed from Unreviewed to Accepted

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

comment:7 Changed 4 years ago by ramiro

  • Resolution set to duplicate
  • Status changed from new to closed

Closing this as a duplicate of the now revived #17078

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