简体   繁体   中英

Why am I not able to activate virtual environment in VS Code?

When I run venv\Scripts\activate in cmd, I am able to use venv but in VS Code I can not use venv and I get this error:

PS F:\Python\Python-Inoventaa\Python Flask\FlaskProject\FlaskBlogProject> venv\Scripts\activate
venv\Scripts\activate : 
File F:\Python\Python-Inoventaa\Python Flask\FlaskProject\FlaskBlogProject\venv\Scripts\Activate.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
+ venv\Scripts\activate
+ ~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : SecurityError: (:) [], PSSecurityException
    + FullyQualifiedErrorId : UnauthorizedAccess

Try this: On the terminal of vs-code at the right side selected cmd instead of power shell.

see the image: https://i.stack.imgur.com/QxRPS.png

You can solve it by adding this configuration to your settings.json file suggested from this issue

By going to your settings.json (on windows):

%APPDATA%\\Code\\User\\settings.json

or

C:\\Users\\<your user>\\AppData\\Roaming\\Code\\User\\settings.json

and adding this line in the configuration (use comma at the end if there are more config lines):

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

and it should be solved once you open another Terminal.

You can use this script on VS Code's terminal:

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process

for more detail you can visit official VS Code website about Python programming at https://code.visualstudio.com/docs/python/python-tutorial

更改为 cmd 而不是 powershell 肯定会在 vs code 中工作

As mentioned in the previous answer, it is due to execution policy of PowerShell terminal in VSCode.

To bypass the execution policy while using VSCode, you may add a modified PowerShell profile and set it as default profile in the JSON settings:

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

or you may also choose to modify the default PowerShell, mentioned by @John:

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

试试这个:在右侧的 vs-code 终端上选择 cmd 而不是 power shell。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM