Vscode 开发 Python 的一些基础设置
1、单独安装 不同版本的 Python 解释器,或 Anaconda;
2、安装 VSCode,及中文、相关Python 开发的插件;
重点有: (1)虚拟环境配置插件。
3、配置 settings.json ,这个文件主要配置 编辑器、文件等相关的(工作空间 WorkSpace、即项目文件夹)设置项。
用户(User)设置项 是VSCode 全局性的。示例如下
{
"python-envs.defaultEnvManager": "ms-python.python:system",
"python-envs.pythonProjects": [],
"editor.fontSize": 18, ------ 改变编辑器字体大小
"editor.formatOnSave": true,
"editor.formatOnPaste": true
}
4、运行与调试 配置:launch.json, 以便在运行和调试代码时提供特定的参数和配置。 这个比较麻烦。 参考官方文档:Python debugging in VS Code
点击侧边栏的“运行和调试”图标,有 一个 “创建 launch.json 文件” 的连接按钮,可以生成配置文件 launch.json。 示例如下,保障能用!
它定义了调试器如何启动和运行程序,最重要的就是四块内容:文件路径、传入参数、虚拟环境、工作路径。
{
"version": "0.2.0", ----- 版本,非必需
"configurations": [
{
"name": "Python Debugger: Python File 主文件", ------ 名称 ,好像没有很大影响,你看着办
"type": "debugpy", ------ 新的变化 debugpy,以前是:python
"request": "launch", ------ 有两个选择:设置为
"launch"
以启动程序,或者 "attach"
以附加到已运行的进程。 "program": "${file}", ------ 要调试的 Python 脚本的路径。你可以使用
${file}
来指定当前打开的文件, 或者指定具体文件的路径。例如,
"program": "${workspaceFolder}/script.py"
。 或 "module": "xxxbb" ------ 要调试的 Python 模块的路径。 program 与 module,二选一。
"console": "integratedTerminal", ------ 指定调试器使用的控制台类型。例如,
"integratedTerminal"
是使用 VSCode 的集成终端。 "args": [ ------ 为程序传递的命令行参数。例如,
"args": ["arg1", "arg2"], "args": "${command:pickArgs}"
。 "arg1",
"arg2"
],
"env": { ----- 设置环境变量。例如,"env": {"VAR1": "value1"}。
"VAR1": "value1"
},
"cwd": "${workspaceFolder}" ----- 设置工作目录。例如,
"cwd": "${workspaceFolder}"
}
]
}
VSCode 中的 Predefined variables :The following predefined variables are supported:
Variable | Description |
---|---|
${userHome} | Path of the user's home folder |
${workspaceFolder} | Path of the folder opened in VS Code |
${workspaceFolderBasename} | Name of the folder opened in VS Code without any slashes (/) |
${file} | Currently opened file |
${fileWorkspaceFolder} | Currently opened file's workspace folder |
${relativeFile} | Currently opened file relative to workspaceFolder |
${relativeFileDirname} | Currently opened file's dirname relative to workspaceFolder |
${fileBasename} | Currently opened file's basename |
${fileBasenameNoExtension} | Currently opened file's basename with no file extension |
${fileExtname} | Currently opened file's extension |
${fileDirname} | Currently opened file's folder path |
${fileDirnameBasename} | Currently opened file's folder name |
${cwd} | Task runner's current working directory upon the startup of VS Code |
${lineNumber} | Currently selected line number in the active file |
${columnNumber} | Currently selected column number in the active file |
${selectedText} | Currently selected text in the active file |
${execPath} | Path to the running VS Code executable |
${defaultBuildTask} | Name of the default build task |
${pathSeparator} | Character used by the operating system to separate components in file paths |
${/} | Shorthand for ${pathSeparator} |
Environment variables :You can reference environment variables with the ${env:Name} syntax. For example, ${env:USERNAME} references the USERNAME
environment variable.
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceFolder}/app.js",
"cwd": "${workspaceFolder}",
"args": ["${env:USERNAME}"]
}