写出一个不是code的bug,很烦恼,解决了挺长时间,都翻到外文来看,不过还是解决了,只尝试了一种简单可观的方法,希望对大家有用
我正在使用Django与Keras(tensorflow)来训练一个模型来识别python中的图像。在python中使用print()时,raw write()返回无效的长度,
即使我想把print()注释掉,也时不时的蹦出来,错误如下:
Traceback (most recent call last): File "D:AIPython35libsite-packagesdjangocorehandlersexception.py", line 35, in inner response = get_response(request) File "D:AIPython35libsite-packagesdjangocorehandlersase.py", line 128, in _get_response response = self.process_exception_by_middleware(e, request) File "D:AIPython35libsite-packagesdjangocorehandlersase.py", line 126, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "E:PyCharmWorkPythonWebAppApiPicDjangoSitePicWebviews.py", line 94, in trainPic bankTrain.Cnn_bank_run(lineData,linefile) File "E:PyCharmWorkPythonWebAppApiPicDjangoSitePicWebCnn_Bank_train.py", line 70, in Cnn_bank_run modelTrain.modelTrain_ByType(kwargs[1],X_train,Y_train,X_test,Y_test) File "E:PyCharmWorkPythonWebAppApiPicDjangoSitePicWeb rainModel_init.py", line 113, in modelTrain_ByType verbose=1, validation_data=(kwgs[3], kwgs[4])) File "D:AIPython35libsite-packageskerasmodels.py", line 960, in fit validation_steps=validation_steps) File "D:AIPython35libsite-packageskerasengine raining.py", line 1650, in fit validation_steps=validation_steps) File "D:AIPython35libsite-packageskerasengine raining.py", line 1233, in _fit_loop callbacks.on_epoch_end(epoch, epoch_logs) File "D:AIPython35libsite-packageskerascallbacks.py", line 73, in on_epoch_end callback.on_epoch_end(epoch, logs) File "D:AIPython35libsite-packageskerascallbacks.py", line 306, in on_epoch_end self.progbar.update(self.seen, self.log_values, force=True) File "D:AIPython35libsite-packageskerasutilsgeneric_utils.py", line 369, in update sys.stdout.write(info) OSError: raw write() returned invalid length 254 (should have been between 0 and 127)
错误都很有规律可循:
Error like this: OSError: raw write() returned invalid length 318 (should have been between 0 and 159) OSError: raw write() returned invalid length 8 (should have been between 0 and 4) OSError: raw write() returned invalid length 130(should have been between 0 and 65) Please note: 318=159x2 8=4x2 130=65x2
原因:可能与最近的Windows 10更新有关。我有版本1709(操作系统版本16299.64),
我认为这可能是unicode字符的返回值(正常长度的两倍)?
我实际上可以在外部powershell.exe中重现此错误,所以这本身不是Code相关的。
总的来说,与win10系统、版本有关系,踩中雷区了(本人win10,版本 10.0.16299 版本 16299)
可以cmd一下,输入msinfo32,就能看到版本情况
解决方案:写入以下代码到报错提示的python的中,写在开头
事先要看你是否pip win_unicode_console,没有,就赶快动手
import win_unicode_console win_unicode_console.enable()
如下:
参考:https://github.com/Microsoft/vscode/issues/39149
https://qiita.com/non0/items/4014b81f5a2d232ae22f
https://stackoverflow.com/questions/47356993/oserror-raw-write-returned-invalid-length-when-using-print-in-python