Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem of VSCode configuring python Environment and Chinese

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Xiaobian to share with you how to solve VSCode configuration python environment and Chinese problems, I believe most people still do not know how to share this article for your reference, I hope you read this article after a great harvest, let us go to understand it!

1 Configuration environment

setting.json·is to set the language environment, launch.json is to set the execution environment to execute the code, tasks.json is to set the instruction compilation code

1.1 setting.json

Configure python interpreter, press ctrl+shift+p key in vscode interface, input python, select python interpreter (python belongs to interpreted language, does not need to be compiled into binary intermediate language, it is dependent interpreter, interpret one line to run one line)

Then select python interpreter path, click OK, it will generate a.vscode folder in the currently selected folder and there is a setting.json file

这只是生成一个setting.json模板,可以按照自己需求添加,如下

{ "python.pythonPath": "D:\\Anaconda3\\envs\\python3", "workbench.colorTheme": "Monokai", "window.zoomLevel": 0, "explorer.confirmDelete": false, "editor.accessibilitySupport": "off", "editor.formatOnPaste": true, "editor.formatOnSave": false, "editor.formatOnType": false, "editor.showFoldingControls": "mouseover", // 控制编辑器是否显示缩进参考线。 "editor.renderIndentGuides": true, "editor.multiCursorModifier": "ctrlCmd", # 将原来的cmd.exe 替换为bash.exe 因为更喜欢bash.exe的操作 "terminal.integrated.shell.windows": "C:\\Program Files\\Git\\bin\\bash.exe", "terminal.integrated.rendererType": "dom", "workbench.activityBar.visible": true, "python.jediEnabled": false}1.2 launch.json

在vscode页面点击运行和调试窗口,点击创建launch.json

就会自动创建一个launch.json文件

{ "version": "0.2.0", "configurations": [ { "name": "Python: 当前文件", "type": "python", "request": "launch", "program": "${file}", "console": "integratedTerminal" } ]}

此时也是在.vscode文件夹下生成的

或者再次模板上添加

{ // Use IntelliSense to learn about possible attributes. // Hover to view descriptions of existing attributes. // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "name": "Python", "type": "python", "request": "launch", "stopOnEntry": false, "program": "${file}", "cwd": "${workspaceRoot}", "env": {}, "envFile": "${workspaceRoot}/.env", "debugOptions": [ "WaitOnAbnormalExit", "WaitOnNormalExit", "RedirectOutput" ] } ]}1.3 task.json(可能不需要配置)

在vscode面板内选中 终端->配置任务...->选择 使用模板创建 tasks.json 文件

选择Other

tasks.json文件生成完毕

2 print打印中文乱码

由于使用的python 3+版本,默认编译是utf8,在dos窗口里面可以打印中文,但是在vscode中就不能打印

2.1 方法一

以 windows 系统为例,添加系统变量,此法可以一劳永逸

PYTHONIOENCODING=UTF8

win10不用配置会自带有此命令,如下:

但是win10可能报错:UnicodeDecodeError:'utf8'

win10中python遇到

UnicodeDecodeError:'utf8' codec can't decode byte 0xd1 in in position 0:invalid的报错的解决办法。

解决办法:

修改win10系统字符集

控制面板>时钟和区域>区域>管理>更该系统区域设置>勾选Beta版: 使用 Unicode UTF-8 提供全球语言支持

但是,此法可能会让其他软件显示乱码,如果win10 vscode能输出中文就不要用此法了

2.2 方法二

修改task.json配置

调出vscode控制命令面板,选择配置任务,点击进去

在task.json中添加如下信息:

"options": { "env":{ "PYTHONIOENCODING": "UTF-8" }}

在每个需要中文的 python 文件增加信息

import ioimport sys#改变标准输出的默认编码sys.stdout=io.TextIOWrapper(sys.stdout.buffer,encoding='utf8')以上是"如何解决VSCode配置python环境及中文问题"这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report