Forums

Python3/Django1.6 shell problems - can't start shell after switching to MySQL

Hi! Has anyone had the following problem?

After cd'ing into the proper directory, I run

python3 manage.py shell

as I always do. However, after installing the official MySQL connector for Python, whenever I run this I get the following:

File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python3.3/dist-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python3.3/dist-packages/django/core/management/__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python3.3/dist-packages/django/core/management/base.py", line 242, in run_from_argv
self.execute(*args, **options.__dict__)
File "/usr/local/lib/python3.3/dist-packages/django/core/management/base.py", line 285, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python3.3/dist-packages/django/core/management/base.py", line 415, in handle
return self.handle_noargs(**options)
File "/usr/local/lib/python3.3/dist-packages/django/core/management/commands/shell.py", line 81, in handle_noargs
self.run_shell(shell=interface)
File "/usr/local/lib/python3.3/dist-packages/django/core/management/commands/shell.py", line 61, in run_shell
return getattr(self, shell)()
File "/usr/local/lib/python3.3/dist-packages/django/core/management/commands/shell.py", line 44, in ipython
ip()
File "/usr/local/lib/python3.3/dist-packages/django/core/management/commands/shell.py", line 38, in _ipython
start_ipython(argv=[])
File "/usr/local/lib/python3.3/dist-packages/IPython/__init__.py", line 118, in start_ipython
return launch_new_instance(argv=argv, **kwargs)
File "/usr/local/lib/python3.3/dist-packages/IPython/config/application.py", line 538, in launch_instance
app.initialize(argv)
File "<string>", line 2, in initialize
File "/usr/local/lib/python3.3/dist-packages/IPython/config/application.py", line 89, in catch_config_error
return method(app, *args, **kwargs)
File "/usr/local/lib/python3.3/dist-packages/IPython/terminal/ipapp.py", line 321, in initialize
self.init_shell()
File "/usr/local/lib/python3.3/dist-packages/IPython/terminal/ipapp.py", line 337, in init_shell
ipython_dir=self.ipython_dir)
File "/usr/local/lib/python3.3/dist-packages/IPython/config/configurable.py", line 349, in instance
inst = cls(*args, **kwargs)
File "/usr/local/lib/python3.3/dist-packages/IPython/terminal/interactiveshell.py", line 320, in __init__
**kwargs
File "/usr/local/lib/python3.3/dist-packages/IPython/core/interactiveshell.py", line 486, in __init__
self.init_completer()
File "/usr/local/lib/python3.3/dist-packages/IPython/core/interactiveshell.py", line 1923, in init_completer
from IPython.core.completerlib import (module_completer,
File "/usr/local/lib/python3.3/dist-packages/IPython/core/completerlib.py", line 53, in <module>
r'|'.join(re.escape(s[0]) for s in imp.get_suffixes()))
File "/usr/lib/python3.3/imp.py", line 58, in get_suffixes
DeprecationWarning, 2)
DeprecationWarning: imp.get_suffixes() is deprecated; use the constants defined on importlib.machinery instead

If you suspect this is an IPython bug, please report it at:
https://github.com/ipython/ipython/issues
or send an email to the mailing list at ipython-dev@scipy.org

You can print a more detailed traceback right now with "%tb", or use "%debug"
to interactively debug it.

Extra-detailed tracebacks for bug-reporting purposes can be enabled via:
c.Application.verbose_crash=True

Exception ResourceWarning: ResourceWarning("unclosed file <_io.TextIOWrapper name='/dev/null' mode='a' encoding='UTF-8'>",) in <_io.FileIO name='/dev/null' mode='ab'> ignored

This started to happen after I switched my default DB to MySQL by installing the official connector. In settings.py this looks like "mysql.connector.django". Not that this is necessarily the cause, but I suspect it might be related.

Moreover, when I run

python manage.py shell

I get a bunch of stuff that ends with

django.core.exceptions.ImproperlyConfigured: 'mysql.connector.django' isn't an available database backend.
Try using django.db.backends.XXX, where XXX is one of:
    'dummy', 'mysql', 'oracle', 'postgresql', 'postgresql_psycopg2', 'sqlite3'
Error was: No module named django.base

(I can post the other rest if that would be helpful.)

Unfortunately, I have no clue what's up here. Would someone be able to elucidate this for me? Thanks!

Hmm, I'd expect the second one -- that is, just

python manage.py shell

...with no version number after the python to fail. This is because python with no version number always defaults to Python 2.7, and I suspect (but I'm not sure) that the error you're seeing when you run it could come from a Python version mismatch -- you've installed the official MySQL connector for Python 3.3, not for 2.7.

For the case when you're running

python3 manage.py shell

...things are a bit odder. Do you get a prompt after all of the errors? Or does it dump you back at a bash prompt?

Hi Giles, thanks for the feedback.

Yeah I didn't really expect the python2.7 command to work, I just gave it a try.

As for the python3 command, what I posted is what came out in its entirety, and I was placed back at the bash prompt afterwards.

I have good news, however! Fortunately, I just tried running this command again to double-check the output and the shell is now working! I swear I changed nothing relevant (I think), so I don't know what's up. The problem persisted the entire day yesterday, and I tried the command several times. If this happens again in the future, should I just post to this thread or do something else?

Well, I guess that's good news -- but very strange! I can't think of anything that would have that effect temporarily.

I guess yes, if it happens again then post here and we'll look at it ASAP. If you could post the URL of a console showing the problem, that might help too -- no-one apart from PythonAnywhere admin staff will be able to access the console, even with the URL.

I am having a similar problem in the console.

python3 manage.py shell

gives me the above response.

other python3 manage.py commands work fine?!?

I think there's something going on with IPython building the completion dictionaries. Could you create a file in /home/<username>/.ipython/profile_default/ipython_config.py and put this code in it:

c = get_config()
c.Application.verbose_crash=True

and post the result here

Here you go!

Rather long but this is the complete contents of the file.

***************************************************************************

IPython post-mortem report

{'codename': 'An Afternoon Hack',
 'commit_hash': '02da31c',
 'commit_source': 'installation',
 'default_encoding': 'UTF-8',
 'ipython_path': '/usr/local/lib/python3.3/dist-packages/IPython',
 'ipython_version': '1.0.0',
 'os_name': 'posix',
 'platform': 'Linux-3.11.0-19-generic-x86_64-with-Ubuntu-13.10-saucy',
 'sys_executable': '/usr/bin/python3',
 'sys_platform': 'linux',
 'sys_version': '3.3.2+ (default, Feb 28 2014, 00:52:16) \n[GCC 4.8.1]'}

***************************************************************************



***************************************************************************

Crash traceback:

---------------------------------------------------------------------------
DeprecationWarning                          Python 3.3.2+: /usr/bin/python3
                                                   Thu May 22 09:12:57 2014
A problem occured executing Python code.  Here is the sequence of function
calls leading up to the error, with the most recent (innermost) call last.
/home/teamh/reverbal/manage.py in <module>()
      1 #!/usr/bin/env python3.3
      2 import os
      3 import sys
      4 
      5 if __name__ == "__main__":
      6     os.environ.setdefault("DJANGO_SETTINGS_MODULE", "reverbal.settings")
      7 
      8     from django.core.management import execute_from_command_line
      9 
---> 10     execute_from_command_line(sys.argv)
        global execute_from_command_line = <function execute_from_command_line at 0x7f1ba0686b00>
        global sys.argv = ['manage.py', 'shell']

/usr/local/lib/python3.3/dist-packages/django/core/management/__init__.py in execute_from_command_line(argv=['manage.py', 'shell'])
    384         # 'django-admin.py --help' to work, for backwards compatibility.
    385         elif self.argv[1:] == ['--version']:
    386             # LaxOptionParser already takes care of printing the version.
    387             pass
    388         elif self.argv[1:] in (['--help'], ['-h']):
    389             parser.print_lax_help()
    390             sys.stdout.write(self.main_help_text() + '\n')
    391         else:
    392             self.fetch_command(subcommand).run_from_argv(self.argv)
    393 
    394 def execute_from_command_line(argv=None):
    395     """
    396     A simple method that runs a ManagementUtility.
    397     """
    398     utility = ManagementUtility(argv)
--> 399     utility.execute()
        utility.execute = <bound method ManagementUtility.execute of <django.core.management.ManagementUtility object at 0x7f1ba0acc4d0>>

/usr/local/lib/python3.3/dist-packages/django/core/management/__init__.py in execute(self=<django.core.management.ManagementUtility object>)
    377             elif args[2] == '--commands':
    378                 sys.stdout.write(self.main_help_text(commands_only=True) + '\n')
    379             else:
    380                 self.fetch_command(args[2]).print_help(self.prog_name, args[2])
    381         elif subcommand == 'version':
    382             sys.stdout.write(parser.get_version() + '\n')
    383         # Special-cases: We want 'django-admin.py --version' and
    384         # 'django-admin.py --help' to work, for backwards compatibility.
    385         elif self.argv[1:] == ['--version']:
    386             # LaxOptionParser already takes care of printing the version.
    387             pass
    388         elif self.argv[1:] in (['--help'], ['-h']):
    389             parser.print_lax_help()
    390             sys.stdout.write(self.main_help_text() + '\n')
    391         else:
--> 392             self.fetch_command(subcommand).run_from_argv(self.argv)
        self.fetch_command = <bound method ManagementUtility.fetch_command of <django.core.management.ManagementUtility object at 0x7f1ba0acc4d0>>
        subcommand.run_from_argv = undefined
        self.argv = ['manage.py', 'shell']
    393 
    394 def execute_from_command_line(argv=None):
    395     """
    396     A simple method that runs a ManagementUtility.
    397     """
    398     utility = ManagementUtility(argv)
    399     utility.execute()

/usr/local/lib/python3.3/dist-packages/django/core/management/base.py in run_from_argv(self=<django.core.management.commands.shell.Command object>, argv=['manage.py', 'shell'])
    227         parser = self.create_parser(prog_name, subcommand)
    228         parser.print_help()
    229 
    230     def run_from_argv(self, argv):
    231         """
    232         Set up any environment changes requested (e.g., Python path
    233         and Django settings), then run this command. If the
    234         command raises a ``CommandError``, intercept it and print it sensibly
    235         to stderr. If the ``--traceback`` option is present or the raised
    236         ``Exception`` is not ``CommandError``, raise it.
    237         """
    238         parser = self.create_parser(argv[0], argv[1])
    239         options, args = parser.parse_args(argv[2:])
    240         handle_default_options(options)
    241         try:
--> 242             self.execute(*args, **options.__dict__)
        self.execute = <bound method Command.execute of <django.core.management.commands.shell.Command object at 0x7f1ba09a6a50>>
        args = []
        options.__dict__ = {'interface': None, 'pythonpath': None, 'plain': None, 'no_startup': None, 'verbosity': '1', 'traceback': None, 'settings': None}
    243         except Exception as e:
    244             if options.traceback or not isinstance(e, CommandError):
    245                 raise
    246 
    247             # self.stderr is not guaranteed to be set here
    248             stderr = getattr(self, 'stderr', OutputWrapper(sys.stderr, self.style.ERROR))
    249             stderr.write('%s: %s' % (e.__class__.__name__, e))
    250             sys.exit(1)
    251 
    252     def execute(self, *args, **options):
    253         """
    254         Try to execute this command, performing model validation if
    255         needed (as controlled by the attribute
    256         ``self.requires_model_validation``, except if force-skipped).
    257         """

/usr/local/lib/python3.3/dist-packages/django/core/management/base.py in execute(self=<django.core.management.commands.shell.Command object>, *args=(), **options={'interface':     None, 'no_startup': None, 'plain': None, 'pythonpath': None, 'settings': None, 'traceback': None, 'verbosity': '1'})
    270             if not self.can_import_settings:
    271                 raise CommandError("Incompatible values of 'leave_locale_alone' "
    272                                    "(%s) and 'can_import_settings' (%s) command "
    273                                    "options." % (self.leave_locale_alone,
    274                                                  self.can_import_settings))
    275             # Switch to US English, because django-admin.py creates database
    276             # content like permissions, and those shouldn't contain any
    277             # translations.
    278             from django.utils import translation
    279             saved_locale = translation.get_language()
    280             translation.activate('en-us')
    281 
    282         try:
    283             if self.requires_model_validation and not options.get('skip_validation'):
    284                 self.validate()
--> 285             output = self.handle(*args, **options)
        output = undefined
        self.handle = <bound method Command.handle of <django.core.management.commands.shell.Command object at 0x7f1ba09a6a50>>
        args = ()
        options = {'interface': None, 'pythonpath': None, 'plain': None, 'no_startup': None, 'verbosity': '1', 'traceback': None, 'settings': None}
    286             if output:
    287                 if self.output_transaction:
    288                     # This needs to be imported here, because it relies on
    289                     # settings.
    290                     from django.db import connections, DEFAULT_DB_ALIAS
    291                     connection = connections[options.get('database', DEFAULT_DB_ALIAS)]
    292                     if connection.ops.start_transaction_sql():
    293                         self.stdout.write(self.style.SQL_KEYWORD(connection.ops.start_transaction_sql()))
    294                 self.stdout.write(output)
    295                 if self.output_transaction:
    296                     self.stdout.write('\n' + self.style.SQL_KEYWORD("COMMIT;"))
    297         finally:
    298             if saved_locale is not None:
    299                 translation.activate(saved_locale)
    300

/usr/local/lib/python3.3/dist-packages/django/core/management/base.py in handle(self=<django.core.management.commands.shell.Command object>, *args=(), **options={'interface':     None, 'no_startup': None, 'plain': None, 'pythonpath': None, 'settings': None, 'traceback': None, 'verbosity': '1'})
    400     """
    401     A command which takes no arguments on the command line.
    402 
    403     Rather than implementing ``handle()``, subclasses must implement
    404     ``handle_noargs()``; ``handle()`` itself is overridden to ensure
    405     no arguments are passed to the command.
    406 
    407     Attempting to pass arguments will raise ``CommandError``.
    408 
    409     """
    410     args = ''
    411 
    412     def handle(self, *args, **options):
    413         if args:
    414             raise CommandError("Command doesn't accept any arguments")
--> 415         return self.handle_noargs(**options)
        self.handle_noargs = <bound method Command.handle_noargs of <django.core.management.commands.shell.Command object at 0x7f1ba09a6a50>>
        options = {'interface': None, 'pythonpath': None, 'plain': None, 'no_startup': None, 'verbosity': '1', 'traceback': None, 'settings': None}
    416 
    417     def handle_noargs(self, **options):
    418         """
    419         Perform this command's actions.
    420 
    421         """
    422         raise NotImplementedError()

/usr/local/lib/python3.3/dist-packages/django/core/management/commands/shell.py in handle_noargs(self=<django.core.management.commands.shell.Command object>,     **options={'interface': None, 'no_startup': None, 'plain': None, 'pythonpath': None, 'settings': None, 'traceback': None, 'verbosity': '1'})
     66     def handle_noargs(self, **options):
     67         # XXX: (Temporary) workaround for ticket #1796: force early loading of all
     68         # models from installed apps.
     69         from django.db.models.loading import get_models
     70         get_models()
     71 
     72         use_plain = options.get('plain', False)
     73         no_startup = options.get('no_startup', False)
     74         interface = options.get('interface', None)
     75 
     76         try:
     77             if use_plain:
     78                 # Don't bother loading IPython, because the user wants plain Python.
     79                 raise ImportError
     80 
---> 81             self.run_shell(shell=interface)
        self.run_shell = <bound method Command.run_shell of <django.core.management.commands.shell.Command object at 0x7f1ba09a6a50>>
        global shell = undefined
        interface = None
     82         except ImportError:
     83             import code
     84             # Set up a dictionary to serve as the environment for the shell, so
     85             # that tab completion works on objects that are imported at runtime.
     86             # See ticket 5082.
     87             imported_objects = {}
     88             try:  # Try activating rlcompleter, because it's handy.
     89                 import readline
     90             except ImportError:
     91                 pass
     92             else:
     93                 # We don't have to wrap the following import in a 'try', because
     94                 # we already know 'readline' was imported successfully.
     95                 import rlcompleter
     96                 readline.set_completer(rlcompleter.Completer(imported_objects).complete)

/usr/local/lib/python3.3/dist-packages/django/core/management/commands/shell.py in run_shell(self=<django.core.management.commands.shell.Command object>, shell='ipython')
     46                 pass
     47             else:
     48                 return
     49         # no IPython, raise ImportError
     50         raise ImportError("No IPython")
     51 
     52     def bpython(self):
     53         import bpython
     54         bpython.embed()
     55 
     56     def run_shell(self, shell=None):
     57         available_shells = [shell] if shell else self.shells
     58 
     59         for shell in available_shells:
     60             try:
---> 61                 return getattr(self, shell)()
        global getattr = undefined
        self = <django.core.management.commands.shell.Command object at 0x7f1ba09a6a50>
        shell = 'ipython'
     62             except ImportError:
     63                 pass
     64         raise ImportError
     65 
     66     def handle_noargs(self, **options):
     67         # XXX: (Temporary) workaround for ticket #1796: force early loading of all
     68         # models from installed apps.
     69         from django.db.models.loading import get_models
     70         get_models()
     71 
     72         use_plain = options.get('plain', False)
     73         no_startup = options.get('no_startup', False)
     74         interface = options.get('interface', None)
     75 
     76         try:

/usr/local/lib/python3.3/dist-packages/django/core/management/commands/shell.py in ipython(self=<django.core.management.commands.shell.Command object>)
     29         """Start IPython pre-1.0.0"""
     30         from IPython.frontend.terminal.ipapp import TerminalIPythonApp
     31         app = TerminalIPythonApp.instance()
     32         app.initialize(argv=[])
     33         app.start()
     34 
     35     def _ipython(self):
     36         """Start IPython >= 1.0"""
     37         from IPython import start_ipython
     38         start_ipython(argv=[])
     39 
     40     def ipython(self):
     41         """Start any version of IPython"""
     42         for ip in (self._ipython, self._ipython_pre_100, self._ipython_pre_011):
     43             try:
---> 44                 ip()
        ip = <bound method Command._ipython of <django.core.management.commands.shell.Command object at 0x7f1ba09a6a50>>
     45             except ImportError:
     46                 pass
     47             else:
     48                 return
     49         # no IPython, raise ImportError
     50         raise ImportError("No IPython")
     51 
     52     def bpython(self):
     53         import bpython
     54         bpython.embed()
     55 
     56     def run_shell(self, shell=None):
     57         available_shells = [shell] if shell else self.shells
     58 
     59         for shell in available_shells:

/usr/local/lib/python3.3/dist-packages/django/core/management/commands/shell.py in _ipython(self=<django.core.management.commands.shell.Command object>)
     23         """Start IPython pre-0.11"""
     24         from IPython.Shell import IPShell
     25         shell = IPShell(argv=[])
     26         shell.mainloop()
     27 
     28     def _ipython_pre_100(self):
     29         """Start IPython pre-1.0.0"""
     30         from IPython.frontend.terminal.ipapp import TerminalIPythonApp
     31         app = TerminalIPythonApp.instance()
     32         app.initialize(argv=[])
     33         app.start()
     34 
     35     def _ipython(self):
     36         """Start IPython >= 1.0"""
     37         from IPython import start_ipython
---> 38         start_ipython(argv=[])
        start_ipython = <function start_ipython at 0x7f1b9cc590e0>
        global argv = undefined
     39 
     40     def ipython(self):
     41         """Start any version of IPython"""
     42         for ip in (self._ipython, self._ipython_pre_100, self._ipython_pre_011):
     43             try:
     44                 ip()
     45             except ImportError:
     46                 pass
     47             else:
     48                 return
     49         # no IPython, raise ImportError
     50         raise ImportError("No IPython")
     51 
     52     def bpython(self):
     53         import bpython

/usr/local/lib/python3.3/dist-packages/IPython/__init__.py in start_ipython(argv=[], **kwargs={})
    103     This is a public API method, and will survive implementation changes.
    104     
    105     Parameters
    106     ----------
    107     
    108     argv : list or None, optional
    109         If unspecified or None, IPython will parse command-line options from sys.argv.
    110         To prevent any command-line parsing, pass an empty list: `argv=[]`.
    111     user_ns : dict, optional
    112         specify this dictionary to initialize the IPython user namespace with particular values.
    113     kwargs : various, optional
    114         Any other kwargs will be passed to the Application constructor,
    115         such as `config`.
    116     """
    117     from IPython.terminal.ipapp import launch_new_instance
--> 118     return launch_new_instance(argv=argv, **kwargs)
        launch_new_instance = <bound method MetaHasTraits.launch_instance of <class 'IPython.terminal.ipapp.TerminalIPythonApp'>>
        argv = []
        kwargs = {}
    119 
    120 def start_kernel(argv=None, **kwargs):
    121     """Launch a normal IPython kernel instance (as opposed to embedded)
    122     
    123     `IPython.embed_kernel()` puts a shell in a particular calling scope,
    124     such as a function or method for debugging purposes,
    125     which is often not desirable.
    126     
    127     `start_kernel()` does full, regular IPython initialization,
    128     including loading startup files, configuration, etc.
    129     much of which is skipped by `embed()`.
    130     
    131     Parameters
    132     ----------
    133

/usr/local/lib/python3.3/dist-packages/IPython/config/application.py in launch_instance(cls=<class 'IPython.terminal.ipapp.TerminalIPythonApp'>, argv=[], **kwargs={})
    523         for cls in self.classes:
    524             lines.append(cls.class_config_section())
    525         return '\n'.join(lines)
    526 
    527     def exit(self, exit_status=0):
    528         self.log.debug("Exiting application: %s" % self.name)
    529         sys.exit(exit_status)
    530 
    531     @classmethod
    532     def launch_instance(cls, argv=None, **kwargs):
    533         """Launch a global instance of this Application
    534         
    535         If a global instance already exists, this reinitializes and starts it
    536         """
    537         app = cls.instance(**kwargs)
--> 538         app.initialize(argv)
        app.initialize = <bound method TerminalIPythonApp.initialize of <IPython.terminal.ipapp.TerminalIPythonApp object at 0x7f1b9dc4ab90>>
        argv = []
    539         app.start()
    540 
    541 #-----------------------------------------------------------------------------
    542 # utility functions, for convenience
    543 #-----------------------------------------------------------------------------
    544 
    545 def boolean_flag(name, configurable, set_help='', unset_help=''):
    546     """Helper for building basic --trait, --no-trait flags.
    547 
    548     Parameters
    549     ----------
    550 
    551     name : str
    552         The name of the flag.
    553     configurable : str

/usr/local/lib/python3.3/dist-packages/IPython/terminal/ipapp.py in initialize(self=<IPython.terminal.ipapp.TerminalIPythonApp object>, argv=[])

/usr/local/lib/python3.3/dist-packages/IPython/config/application.py in catch_config_error(method=<function TerminalIPythonApp.initialize>, app=<IPython.terminal.ipapp.    TerminalIPythonApp object>, *args=([],), **kwargs={})
     74 
     75 #-----------------------------------------------------------------------------
     76 # Application class
     77 #-----------------------------------------------------------------------------
     78 
     79 @decorator
     80 def catch_config_error(method, app, *args, **kwargs):
     81     """Method decorator for catching invalid config (Trait/ArgumentErrors) during init.
     82 
     83     On a TraitError (generally caused by bad config), this will print the trait's
     84     message, and exit the app.
     85     
     86     For use on init methods, to prevent invoking excepthook on invalid input.
     87     """
     88     try:
---> 89         return method(app, *args, **kwargs)
        method = <function TerminalIPythonApp.initialize at 0x7f1b9cc567a0>
        app = <IPython.terminal.ipapp.TerminalIPythonApp object at 0x7f1b9dc4ab90>
        args = ([],)
        kwargs = {}
     90     except (TraitError, ArgumentError) as e:
     91         app.print_help()
     92         app.log.fatal("Bad config encountered during initialization:")
     93         app.log.fatal(str(e))
     94         app.log.debug("Config at the time: %s", app.config)
     95         app.exit(1)
     96 
     97 
     98 class ApplicationError(Exception):
     99     pass
    100 
    101 class LevelFormatter(logging.Formatter):
    102     """Formatter with additional `highlevel` record
    103     
    104     This field is empty if log level is less than highlevel_limit,

/usr/local/lib/python3.3/dist-packages/IPython/terminal/ipapp.py in initialize(self=<IPython.terminal.ipapp.TerminalIPythonApp object>, argv=[])
    306     
    307     @catch_config_error
    308     def initialize(self, argv=None):
    309         """Do actions after construct, but before starting the app."""
    310         super(TerminalIPythonApp, self).initialize(argv)
    311         if self.subapp is not None:
    312             # don't bother initializing further, starting subapp
    313             return
    314         if not self.ignore_old_config:
    315             check_for_old_config(self.ipython_dir)
    316         # print self.extra_args
    317         if self.extra_args and not self.something_to_run:
    318             self.file_to_run = self.extra_args[0]
    319         self.init_path()
    320         # create the shell
--> 321         self.init_shell()
        self.init_shell = <bound method TerminalIPythonApp.init_shell of <IPython.terminal.ipapp.TerminalIPythonApp object at 0x7f1b9dc4ab90>>
    322         # and draw the banner
    323         self.init_banner()
    324         # Now a variety of things that happen after the banner is printed.
    325         self.init_gui_pylab()
    326         self.init_extensions()
    327         self.init_code()
    328 
    329     def init_shell(self):
    330         """initialize the InteractiveShell instance"""
    331         # Create an InteractiveShell instance.
    332         # shell.display_banner should always be False for the terminal
    333         # based app, because we call shell.show_banner() by hand below
    334         # so the banner shows *before* all extension loading stuff.
    335         self.shell = TerminalInteractiveShell.instance(parent=self,
    336                         display_banner=False, profile_dir=self.profile_dir,

/usr/local/lib/python3.3/dist-packages/IPython/terminal/ipapp.py in init_shell(self=<IPython.terminal.ipapp.TerminalIPythonApp object>)
    322         # and draw the banner
    323         self.init_banner()
    324         # Now a variety of things that happen after the banner is printed.
    325         self.init_gui_pylab()
    326         self.init_extensions()
    327         self.init_code()
    328 
    329     def init_shell(self):
    330         """initialize the InteractiveShell instance"""
    331         # Create an InteractiveShell instance.
    332         # shell.display_banner should always be False for the terminal
    333         # based app, because we call shell.show_banner() by hand below
    334         # so the banner shows *before* all extension loading stuff.
    335         self.shell = TerminalInteractiveShell.instance(parent=self,
    336                         display_banner=False, profile_dir=self.profile_dir,
--> 337                         ipython_dir=self.ipython_dir)
        global ipython_dir = undefined
        self.ipython_dir = '/home/teamh/.ipython'
    338         self.shell.configurables.append(self)
    339 
    340     def init_banner(self):
    341         """optionally display the banner"""
    342         if self.display_banner and self.interact:
    343             self.shell.show_banner()
    344         # Make sure there is a space below the banner.
    345         if self.log_level <= logging.INFO: print()
    346 
    347     def _pylab_changed(self, name, old, new):
    348         """Replace --pylab='inline' with --pylab='auto'"""
    349         if new == 'inline':
    350             warn.warn("'inline' not available as pylab backend, "
    351                       "using 'auto' instead.")
    352             self.pylab = 'auto'

/usr/local/lib/python3.3/dist-packages/IPython/config/configurable.py in instance(cls=<class 'IPython.terminal.interactiveshell.TerminalInteractiveShell'>, *args=(),     **kwargs={'display_banner': False, 'ipython_dir': '/home/teamh/.ipython', 'parent': <IPython.terminal.ipapp.TerminalIPythonApp object>, 'profile_dir': <IPython.core.profiledir.    ProfileDir object>})
    334             >>> class Foo(SingletonConfigurable): pass
    335             >>> foo = Foo.instance()
    336             >>> foo == Foo.instance()
    337             True
    338 
    339         Create a subclass that is retrived using the base class instance::
    340 
    341             >>> class Bar(SingletonConfigurable): pass
    342             >>> class Bam(Bar): pass
    343             >>> bam = Bam.instance()
    344             >>> bam == Bar.instance()
    345             True
    346         """
    347         # Create and save the instance
    348         if cls._instance is None:
--> 349             inst = cls(*args, **kwargs)
        inst = undefined
        cls = <class 'IPython.terminal.interactiveshell.TerminalInteractiveShell'>
        args = ()
        kwargs = {'display_banner': False, 'ipython_dir': '/home/teamh/.ipython', 'profile_dir': <IPython.core.profiledir.ProfileDir object at 0x7f1b9cc60950>, 'parent': <    IPython.terminal.ipapp.TerminalIPythonApp object at 0x7f1b9dc4ab90>}
    350             # Now make sure that the instance will also be returned by
    351             # parent classes' _instance attribute.
    352             for subclass in cls._walk_mro():
    353                 subclass._instance = inst
    354 
    355         if isinstance(cls._instance, cls):
    356             return cls._instance
    357         else:
    358             raise MultipleInstanceError(
    359                 'Multiple incompatible subclass instances of '
    360                 '%s are being created.' % cls.__name__
    361             )
    362 
    363     @classmethod
    364     def initialized(cls):

/usr/local/lib/python3.3/dist-packages/IPython/terminal/interactiveshell.py in __init__(self=<IPython.terminal.interactiveshell.TerminalInteractiveShell object>, config=None,     ipython_dir='/home/teamh/.ipython', profile_dir=<IPython.core.profiledir.ProfileDir object>, user_ns=None, user_module=None, custom_exceptions=((), None), usage=None,     banner1=None, banner2=None, display_banner=False, **kwargs={'parent': <IPython.terminal.ipapp.TerminalIPythonApp object>})
    305         # Deferred import
    306         from IPython.lib.inputhook import enable_gui as real_enable_gui
    307         try:
    308             return real_enable_gui(gui, app)
    309         except ValueError as e:
    310             raise UsageError("%s" % e)
    311     
    312     def __init__(self, config=None, ipython_dir=None, profile_dir=None,
    313                  user_ns=None, user_module=None, custom_exceptions=((),None),
    314                  usage=None, banner1=None, banner2=None, display_banner=None,
    315                  **kwargs):
    316 
    317         super(TerminalInteractiveShell, self).__init__(
    318             config=config, ipython_dir=ipython_dir, profile_dir=profile_dir, user_ns=user_ns,
    319             user_module=user_module, custom_exceptions=custom_exceptions,
--> 320             **kwargs
        kwargs = {'parent': <IPython.terminal.ipapp.TerminalIPythonApp object at 0x7f1b9dc4ab90>}
    321         )
    322         # use os.system instead of utils.process.system by default,
    323         # because piped system doesn't make sense in the Terminal:
    324         self.system = self.system_raw
    325 
    326         self.init_term_title()
    327         self.init_usage(usage)
    328         self.init_banner(banner1, banner2, display_banner)
    329 
    330     #-------------------------------------------------------------------------
    331     # Overrides of init stages
    332     #-------------------------------------------------------------------------
    333 
    334     def init_display_formatter(self):
    335         super(TerminalInteractiveShell, self).init_display_formatter()

/usr/local/lib/python3.3/dist-packages/IPython/core/interactiveshell.py in __init__(self=<IPython.terminal.interactiveshell.TerminalInteractiveShell object>,     ipython_dir='/home/teamh/.ipython', profile_dir=<IPython.core.profiledir.ProfileDir object>, user_module=None, user_ns=None, custom_exceptions=((), None), **kwargs={'config':     None, 'parent': <IPython.terminal.ipapp.TerminalIPythonApp object>})
    471         self.init_inspector()
    472         # init_readline() must come before init_io(), because init_io uses
    473         # readline related things.
    474         self.init_readline()
    475         # We save this here in case user code replaces raw_input, but it needs
    476         # to be after init_readline(), because PyPy's readline works by replacing
    477         # raw_input.
    478         if py3compat.PY3:
    479             self.raw_input_original = input
    480         else:
    481             self.raw_input_original = raw_input
    482         # init_completer must come after init_readline, because it needs to
    483         # know whether readline is present or not system-wide to configure the
    484         # completers, since the completion machinery can now operate
    485         # independently of readline (e.g. over the network)
--> 486         self.init_completer()
        self.init_completer = <bound method TerminalInteractiveShell.init_completer of <IPython.terminal.interactiveshell.TerminalInteractiveShell object at 0x7f1b9d497f10>>
    487         # TODO: init_io() needs to happen before init_traceback handlers
    488         # because the traceback handlers hardcode the stdout/stderr streams.
    489         # This logic in in debugger.Pdb and should eventually be changed.
    490         self.init_io()
    491         self.init_traceback_handlers(custom_exceptions)
    492         self.init_prompts()
    493         self.init_display_formatter()
    494         self.init_display_pub()
    495         self.init_data_pub()
    496         self.init_displayhook()
    497         self.init_latextool()
    498         self.init_magics()
    499         self.init_logstart()
    500         self.init_pdb()
    501         self.init_extension_manager()

/usr/local/lib/python3.3/dist-packages/IPython/core/interactiveshell.py in init_completer(self=<IPython.terminal.interactiveshell.TerminalInteractiveShell object>)
   1908         return self.input_splitter.indent_spaces * ' '
   1909 
   1910     #-------------------------------------------------------------------------
   1911     # Things related to text completion
   1912     #-------------------------------------------------------------------------
   1913 
   1914     def init_completer(self):
   1915         """Initialize the completion machinery.
   1916 
   1917         This creates completion machinery that can be used by client code,
   1918         either interactively in-process (typically triggered by the readline
   1919         library), programatically (such as in test suites) or out-of-prcess
   1920         (typically over the network by remote frontends).
   1921         """
   1922         from IPython.core.completer import IPCompleter
-> 1923         from IPython.core.completerlib import (module_completer,
        global IPython.core.completerlib = undefined
        module_completer = undefined
        magic_run_completer = undefined
        cd_completer = undefined
        reset_completer = undefined
   1924                 magic_run_completer, cd_completer, reset_completer)
   1925 
   1926         self.Completer = IPCompleter(shell=self,
   1927                                      namespace=self.user_ns,
   1928                                      global_namespace=self.user_global_ns,
   1929                                      alias_table=self.alias_manager.alias_table,
   1930                                      use_readline=self.has_readline,
   1931                                      parent=self,
   1932                                      )
   1933         self.configurables.append(self.Completer)
   1934 
   1935         # Add custom completers to the basic ones built into IPCompleter
   1936         sdisp = self.strdispatchers.get('complete_command', StrDispatch())
   1937         self.strdispatchers['complete_command'] = sdisp
   1938         self.Completer.custom_completers = sdisp

/usr/local/lib/python3.3/dist-packages/IPython/core/completerlib.py in <module>()
     38 #-----------------------------------------------------------------------------
     39 # Globals and constants
     40 #-----------------------------------------------------------------------------
     41 
     42 # Time in seconds after which the rootmodules will be stored permanently in the
     43 # ipython ip.db database (kept in the user's .ipython dir).
     44 TIMEOUT_STORAGE = 2
     45 
     46 # Time in seconds after which we give up
     47 TIMEOUT_GIVEUP = 20
     48 
     49 # Regular expression for the python import statement
     50 import_re = re.compile(r'(?P<name>[a-zA-Z_][a-zA-Z0-9_]*?)'
     51                        r'(?P<package>[/\\]__init__)?'
     52                        r'(?P<suffix>%s)$' %
---> 53                        r'|'.join(re.escape(s[0]) for s in imp.get_suffixes()))
        global join = undefined
        global re.escape = <function escape at 0x7f1ba0ae3290>
        global s = undefined
        global imp.get_suffixes = <function get_suffixes at 0x7f1ba0a29680>
     54 
     55 # RE for the ipython %run command (python + ipython scripts)
     56 magic_run_re = re.compile(r'.*(\.ipy|\.py[w]?)$')
     57 
     58 #-----------------------------------------------------------------------------
     59 # Local utilities
     60 #-----------------------------------------------------------------------------
     61 
     62 def module_list(path):
     63     """
     64     Return the list containing the names of the modules available in the given
     65     folder.
     66     """
     67     # sys.path has the cwd as an empty string, but isdir/listdir need it as '.'
     68     if path == '':

/usr/lib/python3.3/imp.py in get_suffixes()
     43 
     44 
     45 def get_magic():
     46     """Return the magic number for .pyc or .pyo files."""
     47     return _bootstrap._MAGIC_BYTES
     48 
     49 
     50 def get_tag():
     51     """Return the magic tag for .pyc or .pyo files."""
     52     return sys.implementation.cache_tag
     53 
     54 
     55 def get_suffixes():
     56     warnings.warn('imp.get_suffixes() is deprecated; use the constants '
     57                   'defined on importlib.machinery instead',
---> 58                   DeprecationWarning, 2)
        global DeprecationWarning = undefined
     59     extensions = [(s, 'rb', C_EXTENSION) for s in machinery.EXTENSION_SUFFIXES]
     60     source = [(s, 'U', PY_SOURCE) for s in machinery.SOURCE_SUFFIXES]
     61     bytecode = [(s, 'rb', PY_COMPILED) for s in machinery.BYTECODE_SUFFIXES]
     62 
     63     return extensions + source + bytecode
     64 
     65 
     66 class NullImporter:
     67 
     68     """Null import object."""
     69 
     70     def __init__(self, path):
     71         if path == '':
     72             raise ImportError('empty pathname', path='')
     73         elif os.path.isdir(path):

DeprecationWarning: imp.get_suffixes() is deprecated; use the constants defined on importlib.machinery instead

***************************************************************************

History of session input:

Yeesh. That is long. I have managed to reproduce the issue on a Django project and it looks to me like the DeprecationWarning is actually masking the real issue. The next step is for me to try to reproduce this in a development environment where I can mess with the shared Python code to try to tease out what's happening.

In the meantime you can use:

./manage.py shell --plain

which will give you a plain Python shell without the IPython goodness. I know that's not a great solution, but I have no idea how long it will take me to debug this.

OK Much appreciated!

Right. We have a fix. It looks like an upgrade of IPython fixes the issue. So do a:

pip3 install --user -U ipython

and then try again. We'll look at upgrading IPython for everyone on our next deploy.

Works great! Thank you!

Well not quite...

I get the following Warning on exiting the shell:

Exception ResourceWarning: ResourceWarning("unclosed file <_io.TextIOWrapper name='/dev/null' mode='w' encoding='UTF-8'>",) in <_io.FileIO name='/dev/null' mode='wb'> ignored

Doesn't seem to be a problem, but anyhow...

I have the same exact output as above. Doesn't appear to be causing an issue though.