1、 安装 Python extension for VS Code
2、点击选择python解释器, ==> Enter interpreter path ==> Find ... , 找到python解析器的路径 ,
在settings.json中会添加 python.pythonPath字段:
注意:如果未打开工作空间文件夹时 选择解释器,则VS Code会在用户设置中设置python.pythonPath,这通常设置VS Code的默认解释器。 用户设置可确保您始终具有用于Python项目的默认解释器。 通过工作区设置,您可以覆盖用户设置。
查看用户设置和工作区设置 ,点击File > Preferences > Settings
点击右上角的图标可以切换到json文件格式
3、使用linters (用来提示错误)
open the Command Palette (Ctrl+Shift+P) and select the Python: Select Linter command
This command adds "python.linting.<linter>Enabled": true
to your settings, where <linter>
is the name of the chosen linter。 我选择的是flake8, 在settings.json中会自动生成如下配置:
启用linter会提示您在所选环境中为所选linter安装所需的软件包。 需要通过pip install flake8 安装所需的软件包
4、安装 yapf
① pip install yapf
② 在settings.json中添加 "python.formatting.provider": "yapf"
可以用来格式化代码, 右键 --> Format Document , 让代码符合PEP-8 规范。
最后的 settings.json配置文件如下:
{
"python.pythonPath": "c:\\ml\\ml\\Scripts\\python.exe",
"python.linting.pylintEnabled": false,
"python.linting.flake8Enabled": true,
"python.linting.enabled": true,
"python.linting.flake8Args": [
"--max-line-length=248"
], // 设置每行代码的最大长度为248
"python.formatting.provider": "yapf"
}