首页 > 解决方案 > VS Code集成终端尝试激活python虚拟环境时自动适配shell类型

问题描述

前言:git bash 已经可以工作了

我按照这些说明进行操作,一旦我打开 VS-Code 项目工作区,右下角的 python 解释器总是被正确预选:

python-解释器-vscode

此外,以下设置在我的本地处于活动状态.vscode/settings.json

"python.terminal.activateEnvironment": true

当我git bash shell默认打开一个新终端时,或者在打开的终端中打开另一个 git bash shell 时,venv 的来源是正确的:

username@hd1pcms0347 MINGW64 ~/Projects/proj
$ source c:/Users/username/Projects/proj-venv/Scripts/activate

(proj-venv) 
username@hd1pcms0347 MINGW64 ~/Projects/proj

到目前为止一切都很好,但现在是痛点:

每隔一段时间,我就喜欢打开不同的外壳类型,尤其是PowerShell.

不幸的是,当我在 VS-Code 集成终端中将 PowerShell 作为新 shell 打开时,它会执行与source ...git bash shell 中相同的 - 命令,这将不起作用:

source c:/Users/username/Projects/proj-venv/Scripts/activate
source : The term 'source' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the 
path is correct and try again.
At line:1 char:1
+ source c:/Users/username/Projects/proj-venv/Scripts/activate
+ ~~~~~~
    + CategoryInfo          : ObjectNotFound: (source:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

所以,问题是,是否有一种方法可以让 VS-Code 识别刚刚打开的终端类型,以便相应地调整这个 venv-source 命令。

标签: powershellvisual-studio-codegit-bashpython-venvvirtual-environment

解决方案


推荐阅读