为什么我无法在VS代码中激活虚拟环境



当我在cmd中运行venvScriptsactivate时,我可以使用venv,但在VS代码中我不能使用venv

PS F:PythonPython-InoventaaPython FlaskFlaskProjectFlaskBlogProject> venvScriptsactivate
venvScriptsactivate : 
File F:PythonPython-InoventaaPython FlaskFlaskProjectFlaskBlogProjectvenvScriptsActivate.ps1 cannot be loaded because running scripts is disabled on 
this system. For more information, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170.
At line:1 char:1
+ venvScriptsactivate
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : SecurityError: (:) [], PSSecurityException
+ FullyQualifiedErrorId : UnauthorizedAccess

试试这个:在右侧vs代码的终端上,选择cmd而不是powershell。

请参阅图片:https://i.stack.imgur.com/QxRPS.png

您可以通过将此配置添加到此问题建议的settings.json文件中来解决此问题

通过转到您的settings.json(在windows上(:

%APPDATA%CodeUsersettings.json

C:Users<your user>AppDataRoamingCodeUsersettings.json

并在配置中添加这一行(如果有更多的配置行,请在末尾使用逗号(:

{
"terminal.integrated.shellArgs.windows": ["-ExecutionPolicy", "Bypass"]
}

一旦你打开另一个终端,它就会被解决。

您可以在VS Code的终端上使用此脚本:

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process

有关更多详细信息,您可以访问VS Code官方网站了解Python编程https://code.visualstudio.com/docs/python/python-tutorial

试试这个:在右侧vs代码的终端上,选择cmd而不是powershell。

在vs代码中更改为cmd而不是powershell肯定有效

如前一个答案中所述,这是由于PowerShell终端在VSCode中的执行策略。

要在使用VSCode时绕过执行策略,可以添加一个修改后的PowerShell配置文件,并在JSON设置中将其设置为默认配置文件:

"terminal.integrated.profiles.windows": {
"PowerShell venv": { 
"source": "PowerShell", 
"icon": "terminal-powershell", 
"args": ["-ExecutionPolicy", "Bypass"] }},
"terminal.integrated.defaultProfile.windows": "PowerShell venv",

或者你也可以选择修改默认的PowerShell,@John提到:

"terminal.integrated.profiles.windows": {
"PowerShell": { 
"source": "PowerShell", 
"icon": "terminal-powershell", 
"args": ["-ExecutionPolicy", "Bypass"] }},
"terminal.integrated.defaultProfile.windows": "PowerShell",

最新更新