Search code examples
pythonvisual-studiopytestptvs

py.test from Visual Studio - '_DebuggerOutput' object has no attribute 'closed'


I'm using Python Tools for Visual Studio with py.test. I'm able to use pytest fine if I run the script without debugging, but when I run with debugging, the script crashes. Below is my code and the output that I get in the console window. I've searched for Google for answers but I've come up with nothing that solves this issue; it seems like no one has had this exact problem before. I'm running Visual Studio 2013, with PTVS 2.1 VS 2013.

#Code
import pytest
if __name__ == "__main__":
    pytest.main("--resultlog=resultlog.txt")

#Output
! C:\Python34\lib\site-packages\colorama\ansitowin32.py
 Traceback (most recent call last):
   File "C:\Python34\lib\site-packages\_pytest\main.py", line 80, in wrap_session
     config.do_configure()
   File "C:\Python34\lib\site-packages\_pytest\config.py", line 618, in do_configure
     self.hook.pytest_configure(config=self)
   File "C:\Python34\lib\site-packages\_pytest\core.py", line 521, in __call__
     return self._docall(self.methods, kwargs)
   File "C:\Python34\lib\site-packages\_pytest\core.py", line 528, in _docall
     firstresult=self.firstresult).execute()
   File "C:\Python34\lib\site-packages\_pytest\core.py", line 394, in execute
     res = method(*args)
   File "C:\Python34\lib\site-packages\_pytest\terminal.py", line 41, in pytest_configure
     reporter = TerminalReporter(config, sys.stdout)
   File "C:\Python34\lib\site-packages\_pytest\terminal.py", line 101, in __init__
     self._tw = self.writer = py.io.TerminalWriter(file)
   File "C:\Python34\lib\site-packages\py\_io\terminalwriter.py", line 130, in __init__
     file = colorama.AnsiToWin32(file).stream
   File "C:\Python34\lib\site-packages\colorama\ansitowin32.py", line 68, in __init__
     convert = on_windows and not wrapped.closed and not on_emulated_windows and is_a_tty(wrapped)
 AttributeError: '_DebuggerOutput' object has no attribute 'closed'

Q: How can this error be resolved so that I can debug from inside Visual Studio?



Solution

  • It's a bug in PTVS that has been fixed for the upcoming 2.2 release.

    There's no build with the fix yet, but you can just apply it yourself, as it is in a .py file and does not require recompilation. The file that you need is:

    C:\Users\...\AppData\Local\Microsoft\VisualStudio\12.0Exp\Extensions\Microsoft\Python Tools for Visual Studio\2.1\visualstudio_py_debugger.py

    Find class _DebuggerOutput in it, and add the following at the end of it:

    def __getattr__(self, name):
        return getattr(self.old_out, name)