您的位置:首页 > 编程语言 > Python开发

python3在Visual Studio Code环境下终端出现乱码,控制台输出正常

2019-01-05 23:28 2211 查看

在编译过程中,print打印中文时在终端出现乱码,但在控制台可以正常输出中文字符。为了避免是程序本身的问题导致错误,程序只写一句打印,依然出现中文乱码。

[code]print("您好")

判断是编译器编码的问题。

通过查找资料VS code可以自动识别编码,但下载自识别编码的扩展文件以后仍然出现乱码,再通过添加如下代码段

[code]# -*-coding:utf-8 -*-
import io
import sys
#改变标准输出的默认编码
sys.stdout=io.TextIOWrapper(sys.stdout.buffer,encoding='utf8')
print(“中文”)

依然出现乱码,心态崩了。。。

尝试过修改环境变量,修改自识别编码功能均解决不了问题。

最终通过修改launch.json文件,解决了乱码问题。以下是launch.json文件完整代码,复制到launch.json即可。

[code]{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",

"configurations":[
{
"name": "Python: Current File (Integrated Terminal)",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"env": {
"PYTHONIOENCODING": "GBK"
}
},
{
"name": "Python: Attach",
"type": "python",
"request": "attach",
"port": 5678,
"host": "localhost"
},
{
"name": "Python: Module",
"type": "python",
"request": "launch",
"module": "enter-your-module-name-here",
"console": "integratedTerminal"
},
{
"name": "Python: Django",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}/manage.py",
"console": "integratedTerminal",
"args": [
"runserver",
"--noreload",
"--nothreading"
],
"django": true
},
{
"name": "Python: Flask",
"type": "python",
"request": "launch",
"module": "flask",
"env": {
"FLASK_APP": "app.py"
},
"args": [
"run",
"--no-debugger",
"--no-reload"
],
"jinja": true
},
{
"name": "Python: Current File (External Terminal)",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "externalTerminal"
}
]
}

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: