我有一个名为
src
的文件夹,其中有一个名为 venv
的 python 环境。
当
src
在 VSCode 中打开时,我需要在启动终端时立即激活此环境。
在
src/.vscode/settings.json
我已经设置了
"python.terminal.activateEnvironment": true
当
venv
设置为 terminal.integrated.defaultProfile.linux
时,可正确激活 bash
。然而,当它设置为 zsh
时,使用的 python 解释器是全局解释器 (/usr/bin/python3
),我必须在终端中手动运行 source venv/bin/activate
,以便它使用 src/venv/bin/python3
来代替。
我还尝试了以下方法:
"terminal.integrated.profiles.linux": {
"venv": {
"path": "/bin/zsh", // works with "/bin/bash"
"source": "venv/bin/activate",
"args": []
}
},
"terminal.integrated.defaultProfile.linux": "venv"
但我得到了相同的结果。
在此文件夹中,zsh 始终将其放在行尾 。我不确定这到底意味着什么,但我怀疑它可以提供正在发生的事情的线索。
我也尝试禁用所有 zsh 插件,但同样的结果。
我找到了一个解决方法(灵感来自这里)。 zsh 中的 -c 参数允许在调用时运行任何命令。
由于我们可以将参数与
terminal.integrated.profiles.linux
一起使用,因此放入 settings.json
的简单解决方案是:
"terminal.integrated.profiles.linux": {
"venv": {
"path": "zsh", // works with "bash" too
"args": ["-c", "source venv/bin/activate; zsh -i"]
}
},
"terminal.integrated.defaultProfile.linux": "venv"
注意:最后必须写
zsh -i
,否则zsh终端会立即关闭。