AttributeError:StringIO实例没有属性'encoding'

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了AttributeError:StringIO实例没有属性'encoding'相关的知识,希望对你有一定的参考价值。

在调试nosetests时使用PyCharm的交互式控制台时,我收到以下错误:

AttributeError: StringIO instance has no attribute 'encoding'

我找到了一些指导here,但我不知道如何将它应用到我的情况。

我该如何解决这个问题?


完整堆栈跟踪:

Traceback (most recent call last):
  File "C:Program Files (x86)JetBrainsPyCharm 2016.3.2helperspydev\_pydevd_bundlepydevd_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)JetBrainsPyCharm 2016.3.2helperspydevpydevconsole.py", line 475, in console_exec
    need_more =  exec_code(CodeFragment(expression), updated_globals, frame.f_locals, dbg)
  File "C:Program Files (x86)JetBrainsPyCharm 2016.3.2helperspydevpydevconsole.py", line 392, in exec_code
    interpreterInterface = get_interpreter()
  File "C:Program Files (x86)JetBrainsPyCharm 2016.3.2helperspydevpydevconsole.py", line 372, in get_interpreter
    interpreterInterface = InterpreterInterface(None, None, threading.currentThread())
  File "C:Program Files (x86)JetBrainsPyCharm 2016.3.2helperspydev\_pydev_bundlepydev_ipython_console.py", line 25, in __init__
    self.interpreter = get_pydev_frontend(host, client_port, show_banner=show_banner)
  File "C:Program Files (x86)JetBrainsPyCharm 2016.3.2helperspydev\_pydev_bundlepydev_ipython_console_011.py", line 488, in get_pydev_frontend
    _PyDevFrontEndContainer._instance = _PyDevFrontEnd(show_banner=show_banner)
  File "C:Program Files (x86)JetBrainsPyCharm 2016.3.2helperspydev\_pydev_bundlepydev_ipython_console_011.py", line 318, in __init__
    self.ipython = PyDevTerminalInteractiveShell.instance()
  File "C:UsersjimgoAnaconda2libsite-packages	raitletsconfigconfigurable.py", line 412, in instance
    inst = cls(*args, **kwargs)
  File "C:UsersjimgoAnaconda2libsite-packagesIPython	erminalinteractiveshell.py", line 396, in __init__
    super(TerminalInteractiveShell, self).__init__(*args, **kwargs)
  File "C:UsersjimgoAnaconda2libsite-packagesIPythoncoreinteractiveshell.py", line 499, in __init__
    self.init_io()
  File "C:UsersjimgoAnaconda2libsite-packagesIPython	erminalinteractiveshell.py", line 363, in init_io
    self.enable_win_unicode_console()
  File "C:UsersjimgoAnaconda2libsite-packagesIPython	erminalinteractiveshell.py", line 357, in enable_win_unicode_console
    stderr=stderr_text_str)
  File "C:UsersjimgoAnaconda2libsite-packageswin_unicode_console\__init__.py", line 31, in enable
    readline_hook.enable(use_pyreadline=use_pyreadline)
  File "C:UsersjimgoAnaconda2libsite-packageswin_unicode_console
eadline_hook.py", line 134, in enable
    check_encodings()
  File "C:UsersjimgoAnaconda2libsite-packageswin_unicode_console
eadline_hook.py", line 50, in check_encodings
    if sys.stdin.encoding != sys.stdout.encoding:
AttributeError: StringIO instance has no attribute 'encoding'
答案

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

在PyCharm,去Run > Edit Configurations > Defaults > Python tests > Nosetests。在Additional arguments下放--nocapture。然后从左侧窗格的顶部删除所有现有的测试配置。

尝试再次调试您的测试。不要单击调试面板中的重新测试按钮,因为这似乎重用了以前的配置。而是通过右键单击测试文件再次运行测试。

enter image description here

另一答案

万一其他人出现在这里...我有一个类似的堆栈跟踪只是通过使用:

import pdb; pdb.set_trace()

在我的情况下,这是因为我有Django配置:

TEST_RUNNER = 'xmlrunner.extra.djangotestrunner.XMLTestRunner'

哪个偷了我的stdout。

以上是关于AttributeError:StringIO实例没有属性'encoding'的主要内容,如果未能解决你的问题,请参考以下文章

python模块之StringIO/cStringIO(内存文件)

有没有办法让 Roo 接受 StringIO 对象代替文件?

AttributeError:SQLContext 实例没有属性“createDataFrame”

AttributeError:'tuple'对象没有属性'write',实例分割python

AttributeError : 类实例没有 __call__ 方法

访问实例属性时,修补类会产生“AttributeError:Mock object has no attribute”