Printing to the console with Python 2 sometimes results in an IOError: [Errno 0] on Windows. This issue seems to appear only on Windows when opening a file for both reading and writing, quote:
When the "r+", "w+", or "a+" access type is specified, both reading and writing are allowed (the file is said to be open for "update"). However, when you switch between reading and writing, there must be an intervening fflush, fsetpos, fseek, or rewind operation. The current position can be specified for the fsetpos or fseek operation, if desired.
Relevant StackOverflow answer here.
Running this script almost certainly causes the error at some point:
# py -2.7 test.py
for i in range(5000):
print('foo')
Output:
[...]
foo
foo
fTraceback (most recent call last):
File "test.py", line 5, in <module>
print('foo')
IOError: [Errno 0] Error
I am using Python 2.7.14 (v2.7.14:84471935ed, Sep 16 2017, 20:25:58) [MSC v.1500 64 bit (AMD64)] on win32. I can not reproduce this with any other terminal (tested Windows default terminal & ConEmu).
It appears as if the standard output was broken or handled incorrectly when running Python from inside a Hyper terminal. To me it seems this must have a root cause unrelated to Python. When that cause is found, feel free to rename the title of this issue.
Can you try to reproduce with our canary release (upcoming v2): https://zeit.co/blog/canary#hyper?
It should be fixed.
Hey there, please try again with the new Hyper v2 release and open a new issue if the problem stills exists. Thank you!
I'm able to confirm that this issue still appears in v2.0.0.
If it helps, we've found that if my_command throws the error then winpty my_command sometimes fixes it (where my_command is whatever you're running).
@danielsamuels would you say that it's a duplicate of https://github.com/zeit/hyper/issues/2888 then?
No, because my session is considered to be a TTY:
16:17 $ if [ -t 1 ] ; then echo terminal; else echo "not a terminal"; fi
terminal
I can also reproduce this issue with Hyper 2.0.0 (stable)
I can reproduce consistently on Hyper: 2.0.0 (stable), Plugins: none, OS: Windows 10, Python: 2.7.
It does NOT happen on Python 3.6 for whatever reason.
The issue is triggered by the frequency of writes. The same test code with a sleep(0.08) inside the loop works for me. At first I thought this was a weird issue with my code, but I've eventually realized it's a problem in Hyper and one that is actually quite critical and forces me not to use it from time to time.
The exact same code works on cmd.
Screenshot of the code running on a hyper console:

Screenshot of the same code running on Windows' cmd console:

Maybe it would be helpful or direct you in right way.
I had similar problem with Python 2.7 which i tried to debug in VS Code.
Problem was in using Integrated terminal.
VS Code uses it by default.
I disabled it in launch configuration file.
I has parameter configurations/console which can be "none", "integratedTerminal" and "externalTerminal". Using "none" or "externalTerminal"solves the problem.
I also use VS Code with none as console, but in this case, that seems unrelated.
@gonzalocasas I found this solution after reading this issue - https://github.com/Microsoft/vscode/issues/36630
Maybe it will help you.
Today, the Windows 10, version 1803 update rolled out, and the issue went away, so, I think this could be closed, since the problem really seems to be on the Windows level (as described in the issue linked by @Pomoinytskyi )
Same issue appears in Pycharm Integrated terminal while using python 2.7.14
PyCharm 2018.1 (Community Edition)
Build #PC-181.4203.547, built on March 26, 2018
JRE: 1.8.0_152-release-1136-b20 amd64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Windows 10 10.0
the only solution for now is: not to use integrated terminal
I also can no longer reproduce this issue (same Hyper version, same Python version). (edit Actually I used Hyper v2.0.0)
Most helpful comment
Maybe it would be helpful or direct you in right way.
I had similar problem with Python 2.7 which i tried to debug in VS Code.
Problem was in using Integrated terminal.
VS Code uses it by default.
I disabled it in launch configuration file.
I has parameter configurations/console which can be "none", "integratedTerminal" and "externalTerminal". Using "none" or "externalTerminal"solves the problem.