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

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

print("您好")

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

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

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

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

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

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

{
    // 使用 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"
        }
    ]
}

猜你喜欢

转载自blog.csdn.net/qq_41997920/article/details/85884150