Vscode: Python print unicode generate error in output window

Created on 11 May 2016  ·  11Comments  ·  Source: microsoft/vscode

Steps to Reproduce:
Source:

# -*- coding: utf-8 -*-

unicode_name = u'上海'
utf8_name = unicode_name.encode('utf-8')

print(utf8_name)
print(unicode_name) # This line generate error

Output:
上海
Traceback (most recent call last):
File "d:\Temp\tmp2.py", line 7, in
print(unicode_name) # This line generate error
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)

feature-request

Most helpful comment

Finally I found this page witch solved my problem. https://segmentfault.com/a/1190000005986197

Edit tasks.json like this.

{
    "version": "0.1.0",
    "command": "python",
    "isShellCommand": true,
    "args": ["${file}"],
    "showOutput": "always",
    "options": {
        "env": {
            "PYTHONIOENCODING": "UTF-8"
        }
    }
}

All 11 comments

@bpasero do you think this is the issue with our output decoding or the editor?

This sounds like an error in his python program???

@sun1991 Do you get the same error if you run your python program outside of vscode?

@isidorn Tried the same code in IDLE, no error happen. The way IDLE treated it is:

print(utf8_name) -> output utf-8 bytes interpreted by local non-unicode setting, althrough not useful (not human readable)
print(unicode_name) -> output correct characters, because it is unicode after all...

I think the way IDLE handle makes more sense. After all, for python 2.7, utf8_name is just byte array, if encoding is not know, then it is just some bytes. But unicode_name is... unicode code point, so it should always precisely know what its representation should be.

But VS Code output window did reverse, looks like it interpreted bytes as utf-8 bytes, but report error on unicode.

@sun1991 thanks for providing additional information. What happens when you execute your pything script in the command prompt?

@isidorn This is what looks like in console, the output is the same from what on IDLE.

C:\Users\abc>python
Python 2.7.8 (default, Jun 30 2014, 16:03:49) [MSC v.1500 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> unicode_name = u'上海'
>>> utf8_name = unicode_name.encode('utf-8')
>>> print(utf8_name)
涓婃捣
>>> print(unicode_name)
上海

win8.1/ VSC1.1/python3.5 - bug is reproduced
IDLE - all correct
But VSC is not IDE or сompiler, what are extension for python debugging you use?
Bug perhaps between VSC & extension or extension & python, _if first - MS, we need solve :)_
If you use ext by Don Jayamanne, he is having not closed issue about that [iss#102]

Hi, I have updated the original issue #102 with some additional information. I believe this is a problem lies with how the default encoding is picked up by Python when launched from NodeJs.

I'm closing this issue as this was an issue with the Python extension itself and the issue has now been resolved #102.
If the issue still persists, please re-open the issue in Python repo

Same problem in version 0.5.2, these following code cause the save error:

print('中文')

The default output encoding is "ASCII" while the program needs "Unicode". So I have to add these line to make it work correctly.

import sys
import io

sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf8')

print('中文')

How can I change the default output encoding of VSCode runtime to utf-8 or is there an other solution?

Finally I found this page witch solved my problem. https://segmentfault.com/a/1190000005986197

Edit tasks.json like this.

{
    "version": "0.1.0",
    "command": "python",
    "isShellCommand": true,
    "args": ["${file}"],
    "showOutput": "always",
    "options": {
        "env": {
            "PYTHONIOENCODING": "UTF-8"
        }
    }
}
Was this page helpful?
0 / 5 - 0 ratings