英文:
How can I change the default virtual environment command in Visual Studio Code?
问题
我在项目文件夹中使用命令面板创建了一个虚拟环境:Python: Create Environment: .venv。所以每当我在那个文件夹中打开vs code时,它会尝试使用 .venv/Scripts/Activate.ps1 文件启动环境,但由于对PowerShell禁用了运行脚本,这会失败。所以我想将默认终端切换到命令提示符,并默认执行 .venv/Scripts/Activate.bat。
我成功将默认终端切换到cmd,但vs code仍然尝试运行 .venv/Scripts/Activate.ps1,而不是 .venv/Scripts/Activate.bat。我如何将默认命令从
& <file-path>/.venv/Scripts/Activate.ps1
改为
<file-path>/.venv/Scripts/Activate.bat
编辑:
所以我想更清楚地说明这个问题...
-
激活环境已设置为true
-
默认终端设置为命令提示符
-
"terminal.integrated.defaultProfile.windows": "Command Prompt"
-
当我在vscode中打开我的项目并创建一个新终端,无论是cmd还是powershell,vscode都会运行正确的命令来正确运行虚拟环境。
-
但是,当我使用文件->打开文件夹打开项目或在该文件夹中启动vscode时,vscode会自动启动一个终端,并尝试运行虚拟环境。然而,由于默认终端是cmd,命令提示符会启动,但vscode会尝试使用powershell命令运行环境。我必须手动运行该命令或打开一个新终端才能运行正确的命令。
我没有问题手动再次运行该命令或创建一个新终端,但这个错误真的让我很困扰。希望这能帮到您理清问题。
英文:
I have setup a virtual environment in my project folder using Command Palette: Python: Create Environment: .venv
So whenever I open vs code in that folder it attempts to start the environment using .venv/Scripts/Activate.ps1 file which fails as running scripts is disabled for Powershell. So I want to switch to command prompt as default terminal and execute .venv/Scripts/Activate.bat by default instead.
I am able to switch default terminal to cmd but vs code still tries to run the .venv/Scripts/Activate.ps1 instead of .venv/Scripts/Activate.bat. How do I switch this default command from
& <file-path>/.venv/Scripts/Activate.ps1
to
<file-path>/.venv/Scripts/Activate.bat
Edit:
So I want to be more clear about the issue...
-
The activate environment is set as true
-
And the default terminal is set as Command Prompt
-
"terminal.integrated.defaultProfile.windows": "Command Prompt"
-
When I have my project opened in vscode and create a new terminal whether cmd or powershell, vscode runs the proper command to run the virtual environment properly.
-
New terminal created by either +'plus' icon or View->Terminal
-
However, when I open the project using File->Open Folder or start vscode in that folder, then vscode automatically starts a terminal and tries to run the virtual environment. However, as the default terminal is cmd, Command Prompt starts but vscode tries to run the environment using the powershell command. I have to run the command manually or open a new terminal for it to run the proper command
I have no problem running the command again manually or creating a new terminal, but this error really bugs me. Hope this helps clear the problem.
答案1
得分: 0
抱歉,我无法执行代码或直接返回代码的翻译部分。
英文:
When you choose a virtual environment interpreter, vscode will automatically activate the environment every time you build a new terminal. This is controlled by the following settings, and the default value is true.
"python.terminal.activateEnvironment": true,
The shell command that activates the virtual environment is based on your terminal automatic change:
-
PowerShell is
& e:/workspace/py12/.venv/Scripts/Activate.ps1
-
CMD is
e:/workspace/py12/.venv/Scripts/activate.bat
As for you every time you build a new terminal, you will create a new PowerShell or CMD, which is controlled by the following settings
"terminal.integrated.defaultProfile.windows": "PowerShell",
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论