当 运行 脚本在系统中被禁用时,如何在 Vscode 中激活虚拟环境?

How to activate virtual environment in Vscode when running scripts are disabled in system?

我在 vscode 中创建了一个名为 server 的文件夹中的虚拟环境,方法是键入:

python -m venv env 

然后我打开了服务器文件夹,select interpreter Python 3.8.1 64-bit('env':venv)

然后我得到以下错误:

我找不到任何解决方案,我被困了几个小时。

非常感谢

好像是要通过powershell脚本激活环境。并且 运行 这样的脚本默认是关闭的。此外,通常通过 cmd 和 .bat 脚本激活虚拟环境。您可以打开 运行 powershell 脚本或通过 cmd 和 .bat 文件使 VS Code 激活环境。

第一种方式——用cmd代替Powershell

我刚刚在我的 PC 上检查过,VS Code 根本不使用 Powershell。它使用 cmd 而不是 Powershell 激活环境。可能值得检查 VS Code 设置,将 cmd 设置为默认终端。主要settings.json里面大概就是这样一个选项(可以通过ctrl+shift+p打开输入'open settings (JSON)'):"terminal.integrated.shell.windows": "C:\Windows\System32\cmd.exe",.

第二种方式——改变Powershell执行策略

为了更改 Powershell 执行策略,您可以将 "terminal.integrated.shellArgs.windows": ["-ExecutionPolicy", "Bypass"] 添加到您的主要 VS 代码设置中。 您也可以以管理员身份打开 Powershell window 并键入以下内容:

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

然后回答 y 任何问题。

关于在 VS Code 中使用命令提示符而不是 Powershell 的更新

settings.json 中实施 "terminal.integrated.shell.windows": "C:\Windows\System32\cmd.exe" 时,它发出通知:

This is deprecated, the new recommended way to configure your default shell is by creating a terminal profile in #terminal.integrated.profiles.windows# and setting its profile name as the default in #terminal.integrated.defaultProfile.windows#. This will currently take priority over the new profiles settings but that will change in the future

查看 documentation 后,我发现正确的替代方法是包含如下所示的一行。

"terminal.integrated.defaultProfile.windows": "Command Prompt"

或者,您也可以使用 documentation 中所示的 GUI。那里还显示了更多高级设置。