new ipython / old django

new ipython / old django

  • Written by
    Walter Doekes
  • Published on

IPython after version 0.10 is not friends with older Django (e.g. 1.1.x) versions anymore.

    shell = IPython.Shell.IPShell(argv=[])
AttributeError: 'module' object has no attribute 'Shell'

This is fixed in newer Django’s, but this isn’t backported.

Here, a patch. (Nothing more than a diff between the old and the new Django version.)

--- django/core/management/commands/shell.py  2012-03-28 16:10:28.000000000 +0200
+++ django/core/management/commands/shell.py  2012-01-24 10:27:50.405338739 +0100
@@ -8,9 +8,38 @@
             help='Tells Django to use plain Python, not IPython.'),
     )
     help = "Runs a Python interactive interpreter. Tries to use IPython, if it's available."
-
+    shells = ['ipython', 'bpython']
     requires_model_validation = False

+    def ipython(self):
+        try:
+            from IPython.frontend.terminal.embed import TerminalInteractiveShell
+            shell = TerminalInteractiveShell()
+            shell.mainloop()
+        except ImportError:
+            # IPython < 0.11
+            # Explicitly pass an empty list as arguments, because otherwise
+            # IPython would use sys.argv from this script.
+            try:
+                from IPython.Shell import IPShell
+                shell = IPShell(argv=[])
+                shell.mainloop()
+            except ImportError:
+                # IPython not found at all, raise ImportError
+                raise
+
+    def bpython(self):
+        import bpython
+        bpython.embed()
+
+    def run_shell(self):
+        for shell in self.shells:
+            try:
+                return getattr(self, shell)()
+            except ImportError:
+                pass
+        raise ImportError
+
     def handle_noargs(self, **options):
         # XXX: (Temporary) workaround for ticket #1796: force early loading of all
         # models from installed apps.
@@ -23,11 +52,7 @@
             if use_plain:
                 # Don't bother loading IPython, because the user wants plain Python.
                 raise ImportError
-            import IPython
-            # Explicitly pass an empty list as arguments, because otherwise IPython
-            # would use sys.argv from this script.
-            shell = IPython.Shell.IPShell(argv=[])
-            shell.mainloop()
+            self.run_shell()
         except ImportError:
             import code
             # Set up a dictionary to serve as the environment for the shell, so

And another patch to get the classic mode to work here too. (The TerminalInteractiveShell() would require a config=load_default_config() from IPython.frontend.terminal.ipapp.)

--- django/core/management/commands/shell.py  2012-07-09 10:30:12.423405004 +0200
+++ django/core/management/commands/shell.py  2012-07-09 10:45:23.391960205 +0200
@@ -13,9 +13,8 @@

     def ipython(self):
         try:
-            from IPython.frontend.terminal.embed import TerminalInteractiveShell
-            shell = TerminalInteractiveShell()
-            shell.mainloop()
+            from IPython import embed
+            embed()
         except ImportError:
             # IPython < 0.11
             # Explicitly pass an empty list as arguments, because otherwise

Back to overview Newer post: postfix / submission / smtpd_client_restrictions / sleep Older post: serialize json date / microsoft extension