python - 使用 vscode 调试时如何执行设置 python 虚拟环境的 shell 脚本(我需要对 launch.json 进行哪些更改)

标签 python visual-studio-code vscode-debugger vscode-remote vscode-python

所以我在 Visual Studio Code Insider 中使用 vscode-remote,但我认为我在使用 vscode-python 扩展时遇到了问题。

所以我正在尝试调试 python 文件。 所以文件的正常执行如下:

我使用以下命令启用或激活虚拟环境:

源$HOME/.enn/enn.sh

但是我在shell文件中看到的虚拟环境的路径是:/home/ubuntu/venv/enn并且pythonPath是/home/ubuntu/venv/enn/bin/python

因此它是一个 shell 文件,用于激活相关的虚拟环境并设置环境变量,例如 export DOMAIN_ENDPOINT=http://s3.us-west-2.amazonaws.com 等。

然后我 cd 进入相关目录并执行 python 命令:

python session_server.py --config_file=../path/to/file/test_config.hocon

这有效。

但是当我尝试调试时,它开始出现导入错误。 所以我认为在 Debug模式下没有设置环境变量,也没有激活python虚拟环境。如何设置要设置的python虚拟环境以及环境变量。所以我本质上问的是如何在使用 vscode 调试期间执行相同的 shell 命令 - source $HOME/.enn/enn.sh

我的launch.json是

{

    "version": "0.2.0",
    "configurations": [

        {
            "name": "Python: ennsession_server",
            "type": "python",
            "request": "launch",
            "program": "/home/ubuntu/path/to/file/session_server.py",
            "console": "integratedTerminal",
            "pythonPath" : "/home/ubuntu/venv/enn/bin/python",
            "cwd" : "/home/ubuntu/path/to/directory/experiment",
            "args": ["--config_file=../path/to/file/test_config.hocon"]


        }
    ]
} 

而且,由于我在 vscode 的 settings.json 中将 pythonPath 指定为 /home/ubuntu/venv/enn/bin/python,因此在调试为 时,vscode 会调用自动命令源/home/ubuntu/venv/enn/bin/activate。我不想调用它,但我希望在调试或打开新终端时调用命令 source $HOME/.enn/enn.sh 。我该怎么做?

最佳答案

目前还没有办法在 Python 调试器下执行 shell 脚本,因为调试器本身需要启动 Python 代码本身。如果 shell 脚本正在激活虚拟环境和环境变量,那么您可以在调试时让扩展代表您执行此操作。

您可以投票给this feature request以获得您想要的功能。

关于python - 使用 vscode 调试时如何执行设置 python 虚拟环境的 shell 脚本(我需要对 launch.json 进行哪些更改),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56579150/

相关文章:

python - Opencv中分割字符的掩码

python - PyTorch torch.no_grad() 与 requires_grad=False

visual-studio-code - .vscode-server 文件夹占用大量空间

go - vs 代码调试 go 测试未传递标志

python - 在 .vscode 中调试时遇到问题

c++ - 使用VS Code在C++应用程序中查找内存泄漏

python - 如何在 Python RegEx 中组合组?

python - 如何使用 argparse 添加对 Python 脚本多行参数的支持?

javascript - 带有 typescript : what determines how an object is rendered?的vscode调试器

reactjs - Tailwind CSS IntelliSense 没有在 ReactJS 项目中提供建议?