Search code examples
windowspython-2.7consolepycharmnose

AttributeError: StringIO instance has no attribute 'encoding'


While using PyCharm's interactive console when debugging nosetests, I get the following error:

AttributeError: StringIO instance has no attribute 'encoding'

I found some guidance here, but I don't know how to apply it to my situation.

How can I resolve this issue?


Full Stack Trace:

Traceback (most recent call last):
  File "C:\Program Files (x86)\JetBrains\PyCharm 2016.3.2\helpers\pydev\_pydevd_bundle\pydevd_comm.py", line 1409, in do_it
    result = pydevconsole.console_exec(self.thread_id, self.frame_id, self.expression, dbg)
  File "C:\Program Files (x86)\JetBrains\PyCharm 2016.3.2\helpers\pydev\pydevconsole.py", line 475, in console_exec
    need_more =  exec_code(CodeFragment(expression), updated_globals, frame.f_locals, dbg)
  File "C:\Program Files (x86)\JetBrains\PyCharm 2016.3.2\helpers\pydev\pydevconsole.py", line 392, in exec_code
    interpreterInterface = get_interpreter()
  File "C:\Program Files (x86)\JetBrains\PyCharm 2016.3.2\helpers\pydev\pydevconsole.py", line 372, in get_interpreter
    interpreterInterface = InterpreterInterface(None, None, threading.currentThread())
  File "C:\Program Files (x86)\JetBrains\PyCharm 2016.3.2\helpers\pydev\_pydev_bundle\pydev_ipython_console.py", line 25, in __init__
    self.interpreter = get_pydev_frontend(host, client_port, show_banner=show_banner)
  File "C:\Program Files (x86)\JetBrains\PyCharm 2016.3.2\helpers\pydev\_pydev_bundle\pydev_ipython_console_011.py", line 488, in get_pydev_frontend
    _PyDevFrontEndContainer._instance = _PyDevFrontEnd(show_banner=show_banner)
  File "C:\Program Files (x86)\JetBrains\PyCharm 2016.3.2\helpers\pydev\_pydev_bundle\pydev_ipython_console_011.py", line 318, in __init__
    self.ipython = PyDevTerminalInteractiveShell.instance()
  File "C:\Users\jimgo\Anaconda2\lib\site-packages\traitlets\config\configurable.py", line 412, in instance
    inst = cls(*args, **kwargs)
  File "C:\Users\jimgo\Anaconda2\lib\site-packages\IPython\terminal\interactiveshell.py", line 396, in __init__
    super(TerminalInteractiveShell, self).__init__(*args, **kwargs)
  File "C:\Users\jimgo\Anaconda2\lib\site-packages\IPython\core\interactiveshell.py", line 499, in __init__
    self.init_io()
  File "C:\Users\jimgo\Anaconda2\lib\site-packages\IPython\terminal\interactiveshell.py", line 363, in init_io
    self.enable_win_unicode_console()
  File "C:\Users\jimgo\Anaconda2\lib\site-packages\IPython\terminal\interactiveshell.py", line 357, in enable_win_unicode_console
    stderr=stderr_text_str)
  File "C:\Users\jimgo\Anaconda2\lib\site-packages\win_unicode_console\__init__.py", line 31, in enable
    readline_hook.enable(use_pyreadline=use_pyreadline)
  File "C:\Users\jimgo\Anaconda2\lib\site-packages\win_unicode_console\readline_hook.py", line 134, in enable
    check_encodings()
  File "C:\Users\jimgo\Anaconda2\lib\site-packages\win_unicode_console\readline_hook.py", line 50, in check_encodings
    if sys.stdin.encoding != sys.stdout.encoding:
AttributeError: StringIO instance has no attribute 'encoding'

Solution

  • There's an issue on the JetBrains bug tracker with a solution.

    In PyCharm, go to Run > Edit Configurations > Defaults > Python tests > Nosetests. Under Additional arguments put --nocapture. Then delete all the existing test configurations from the top of the left-hand pane.

    Try to debug your test again. Don't click the re-test button from the debug panel because this seems to reuse the previous configuration. Instead run the test again by right clicking on the test file for instance.

    enter image description here