VSCode Python开发环境配置

本文详细介绍了如何在VSCode中安装和配置Python插件(如Pylance、BlackFormatter等),包括代码格式化、Git插件、调试工具(launch.json)及解决测试代码导入问题的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1 插件安装

1.1 基础编译插件,Python、Pylance
在这里插入图片描述
1.2 修改语言服务器类型,进入用户配置页面搜索Python: Language Server,选择Pylance(一定要修改可以提供很多语法提示)
在这里插入图片描述
在这里插入图片描述
1.3 代码格式化插件,Black Formatter、isort,安装后修改VSCode配置,在setting.json文件中添加如下配置可以配置保存时自动格式化代码

"[python]": {
    "editor.defaultFormatter": "ms-python.black-formatter",
    "editor.formatOnSave": true,
    "editor.codeActionsOnSave": {
        "source.organizeImports": true
    },
  },
"isort.args":["--profile", "black"],

1.4 Git插件,GitLens、Git History,可以方便的查看Git提交记录和文件修改历史
在这里插入图片描述

1.5 推荐一款VSCode主题插件,Eva Theme,个人觉得最好看的一款!!!
在这里插入图片描述

2 Debug和测试配置

2.1 在./vscode目录创建launch.json文件,配置如下

{
    "version": "1.0.0",
    "configurations": [
        {
            "name": "Python: pytest",
            "python": "${command:python.interpreterPath}",
            "type": "python",
            "request": "launch",
            "module": "pytest",
            "args": [
                // "-c",
                // 有配置tox.ini文件可添加
                // "${workspaceFolder}/tox.ini",
                "--color=yes",
                "-v",
                "-s",
                // 执行单个测试文件
                "${file}",
                // 执行所有测试用例
                // "${workspaceFolder}/tests",
            ],
             // 环境变量配置
            "env": {
                "PYTHONPATH": "${workspaceFolder}",
                "PYTEST_ADDOPTS": "-p no:cacheprovider",
                "PYTEST_ASYNCIO_MODE": "auto",
                "MANUAL_TEST": "enabled",
            },
            "console": "integratedTerminal"
        },
        {
            "name": "Python Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "cwd": "${workspaceFolder}",
            "console": "integratedTerminal"
        }
    ]
}

2.2 输入Ctrl+Shift+D快捷键,进入调试窗口,修改执行配置为Python: pytest执行测试代码
2.3 选好执行配置后,按下F5即可执行当前窗口的测试代码

3 Setting.json配置

{
    "files.autoSave": "onFocusChange",
    "editor.rulers": [120],
    "editor.codeActionsOnSave": {
        "source.organizeImports": "explicit"
    },
    "multiDiffEditor.experimental.enabled": true,
    "remote.SSH.remotePlatform": {
        "127.0.0.1": "linux"
    },
    "workbench.colorTheme": "Eva Dark",
    "editor.fontSize": 16,
    "editor.fontWeight": "normal",
    "debug.console.fontSize": 16,
    "files.exclude": {
        "**/.vscode": false
    },
    "workbench.settings.applyToAllProfiles": [
    ],
    "flake8.path": [
        "C:\\develope\\python39\\python.exe"
    ],
    "editor.fontLigatures": false,
    "python.languageServer": "Pylance",
    "python.analysis.autoImportCompletions": true,
    "python.autoComplete.addBrackets": true,
    "python.analysis.extraPaths": [
        "${workspaceFolder}"
    ],
    "python.autoComplete.extraPaths": [
        "/data/home/src/myenv/lib/python3.9/site-packages"
    ],
    "path-intellisense.mappings": {
        "@": "${workspaceRoot}/src",
        "/": "${workspaceRoot}/"
    },
    "git.enableSmartCommit": true,
    "json.schemas": [],
    "gitlens.ai.experimental.provider": "openai",
    "gitlens.ai.experimental.openai.model": "gpt-4-1106-preview",
    "python.analysis.completeFunctionParens": true,
    "gitlens.graph.minimap.enabled": false,
    "[python]": {
        "editor.defaultFormatter": "ms-python.black-formatter",
        "editor.formatOnSave": true,
        "editor.codeActionsOnSave": {
            "source.organizeImports": "explicit"
        },
      },
    "isort.args":["--profile", "black"],
    /* ===集成终端配置=== */
    "terminal.integrated.cursorBlinking": true,  // 光标是否闪烁
    "terminal.integrated.cursorWidth": 2,  // 光标大小
    "terminal.integrated.copyOnSelection": true,  // 是否选中即自动复制
    "terminal.integrated.cursorStyle": "line",  // 光标样式
    "terminal.integrated.defaultProfile.linux": "fish",  // Linux系统默认终端
    // "terminal.integrated.defaultProfile.windows": "Git Bash",  // Windows系统默认终端 Git Bash
    "terminal.integrated.defaultProfile.windows": "PowerShell",  // Windows系统默认终端 PowerShell
    "terminal.integrated.enableBell": true,  // 出错提示音
    "terminal.integrated.fontFamily": "MesloLGM Nerd Font",  // 字体类别(重要!最好选有特殊图标的Nerd字体)
    "terminal.integrated.fontSize": 16,  // 字体大小
    "terminal.integrated.shellIntegration.enabled": true,
    "update.enableWindowsBackgroundUpdates": false,
    "update.mode": "manual",
    "go.toolsManagement.autoUpdate": true,
    "translation.provider": "baidu",
    "github.copilot.editor.enableAutoCompletions": true,
    "go.formatTool": "goimports",
    "Codegeex.Privacy": true,
    "settingsSync.ignoredExtensions": [
    ], // Shell集成
}

常见问题

  1. 测试代码导包结构存在问题,可以修改VSCode配置文件setting.json指定工作目录
"python.analysis.extraPaths": [
    "./your_project_work_path"
],
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值