首页 > 解决方案 > 如何在 VSCode 的非 python 调试会话期间临时激活 virtualenv?

问题描述

所以......你知道那些你向另一个人解释你的问题并突然得到答案的时候吗?这是其中之一。下面是我的原始帖子,距离提交仅片刻之遥,当时我有一个想法可以解决这个问题!继续阅读...


我正在开发一个在 VSCode 中调试的基于 Electron 的项目。该代码的一部分产生了几个用 Python 编写的外部服务。这些服python_pip

问题是我需要一种在调试会话开始时激活 virtualenv 的方法,就像在 bash shell 中启动了电子一样(我在我的 .bashrc 中激活了 virtualenv)。所以我的问题是:如何在调试会话期间激活 virtualenv?

为了完整起见,这是我当前的启动配置:

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "Launch Electron Shell",
            "cwd": "${workspaceFolder}/dist",
            "runtimeExecutable": "${workspaceFolder}/dist/node_modules/electron/dist/electron",
            "runtimeArgs": ["app.js", "--remote-debugging-port=9222"],
            "protocol": "inspector",
        },
        {
            "type": "chrome",
            "request": "attach",
            "name": "Attach to Electron Shell",
            "port": 9222,
            "webRoot": "${workspaceFolder}/src",
            "timeout": 30000
        }
    ],
    "compounds": [
        {
            "name": "Debug Angular In Electron Shell",
            "configurations": [
                "Attach to Electron Shell",
                "Launch Electron Shell",
            ]
        }
    ]
}

标签: visual-studio-codevirtualenvvscode-debugger

解决方案


答案是只做activate脚本正在做的事情: modifyPATH和 unset PYTHONHOME。像这样:

        {
            "type": "node",
            "request": "launch",
            "name": "Launch Electron Shell",
            "cwd": "${workspaceFolder}/dist",
            "runtimeExecutable": "${workspaceFolder}/dist/node_modules/electron/dist/electron",
            "runtimeArgs": ["app.js", "--remote-debugging-port=9222"],
            "protocol": "inspector",
            "env": {
                "PATH": "/path/to/virtualenv/folder/bin:${env:PATH}",
                "PYTHONHOME": null
            }
        },

推荐阅读