Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VScode's Python Debug Console doesn't print Unicode Chinese Charactor Properly

Image to show the problemHere is the code to illustrate the problem:

# -*- coding:utf-8 -*-
text = u"严"
print text

If I run the code above in VSCode debug, it will prints "涓" instead of "严", which is the result of the first 2 byte (\xe4\xb8) of u"严" in UTF-8 (\xe4\xb8\xa5), decoded in gbk codec. \xe4\xb8 in gbk is "涓".

However if I run the same code in pycharm it prints "严" exactly as I expected. And it is the same If I run the code in powershell.

Wired the VSCode python debugger behaves different with python interpreter. How can I get the print result correct, I do not think add a decode("gbk") in the end of every text would be a good idea.

My Environment data

  • VS Code version: 1.21
  • VSCode Python Extension version : 2018.2.1
  • OS and version: Windows 10
  • Python version : 2.7.14
  • Type of virtual environment used : No
like image 309
Marquez.Z Avatar asked May 01 '26 22:05

Marquez.Z


1 Answers

  1. For Windows users, in your System Variables, add PYTHONIOENCODING Variables,change its value to UTF-8, then restart vscode, this worked on my pc.

  2. Modify task.json file in vscode, I am not sure if it will still work on version 2.0. You can find it here:Changing the encoding for a task output or here in github: Tasks should support specifying the output encoding

  3. add this before you start a py script: import io import sys sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='utf8')

like image 59
Jay Avatar answered May 03 '26 12:05

Jay



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!