Visual studio code cmd error: Cannot be loaded because running scripts is disabled on this system
Inside of visual studio code, I'm trying to execute a script.bat from the command line, but I'm getting the following error:
File C:\Theses_Repo\train-cnn\environment\Scripts\activate.ps1 cannot be loaded because running scripts is disabled on this system.
After reading this I tried to run the visual studio code in administrator mode, thinking that the problem was a matter of privileges. But the error is throwing anyway.
I found out here that you can add to your visual studio code settings the following and the problem will vanish: For visual studio code settings, go to File -> Preferences -> Settings -> Extensions -> Scroll down and find "Edit in settings.json". Do not forget to restart the visual studio code
"terminal.integrated.shellArgs.windows": ["-ExecutionPolicy", "Bypass"]
The reason is that, in build command line integrations like visual studio code, you need to set the command line policies by your self. By setting the above configurations, the visual studio code will do that for you.
(read this to understand better the command line policies)
Update August 2021
It seems that terminal.integrated.shellArgs
is deprecated. This answer shows how to do it in new versions.
For more simplicity, I want to add the vs code settings path in addition to Ricardo's answer. you can get it like this:
File -> Preferences -> Settings and in the search bar write "automation".
After that, by looking your operating system enter "edit in settings.json".
Finally, add the following b/n the braces:
"terminal.integrated.shellArgs.windows": ["-ExecutionPolicy", "Bypass"]
the issue is that PowerShell by default restricts running scripts unless we change the execution policies.
we can change the execution policies by adding -ExecutionPolicy Bypass
args. in vscode you will have to create a shell profile with these args by adding below in settings.json (ctrl + shift + p and type "settings.json")
"terminal.integrated.profiles.windows": {
"PowerShell": {
"source": "PowerShell",
"icon": "terminal-powershell",
"args": ["-ExecutionPolicy", "Bypass"]
}
},
"terminal.integrated.defaultProfile.windows": "PowerShell",
Need to restart VS Code and the Terminal.
Note: terminal.integrated.shellArgs
settings have been deprecated.