-1

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...

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.

  • based on the terminal VSC select 1 of the 3 activate scripts, most likely your default terminal is not `cmd`, close VSC, open VSC, press `Ctrl+backtick`, do you get powershell or cmd – rioV8 Feb 18 '23 at 14:11
  • why don't you enable running of scripts in Powershell? – rioV8 Feb 18 '23 at 14:12
  • @rioV8 I get cmd by following the steps. Also, I checked Terminal › Integrated › Default Profile: Windows, and it is set as a command prompt. If I allow running scripts in Powershell, won't it enable the feature for all sources? I thought it was disabled for a reason, so I didn't attempt that. – Shashwat G Feb 18 '23 at 14:32
  • Also, I found that closing the terminal only and opening it again runs the correct command, but it is not the case at startup. – Shashwat G Feb 18 '23 at 14:55

1 Answers1

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,

enter image description here

The shell command that activates the virtual environment is based on your terminal automatic change:

  • PowerShell is & e:/workspace/py12/.venv/Scripts/Activate.ps1

    enter image description here

  • CMD is e:/workspace/py12/.venv/Scripts/activate.bat

    enter image description here

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",

enter image description here

JialeDu
  • 6,021
  • 2
  • 5
  • 24
  • That's the thing, whenever I create a new terminal, it runs the correct command but when I open the project in a new vs code window, it opens a new cmd terminal as I set it as default but tries to run the PowerShell command to activate the environment. I can attach images if it helps in explanation. Can this be some sort of bug? – Shashwat G Feb 20 '23 at 12:53
  • Do you mean that your machine uses the PowerShell command to activate the virtual environment in CMD? Then there was an error message and failed? If you don't mind, describe your operation steps step by step. It is better to show screenshots. – JialeDu Feb 21 '23 at 01:40
  • Yes that is whats going on. I have added the steps and screenshots. Do these help understand the issue? – Shashwat G Feb 23 '23 at 17:53
  • Did you use the `***.psl` command before opening a new vscode window? Adding the `"terminal.integrated.enablePersistentSessions": false,` [configuration](https://imgur.com/ymp9XXj.png) to your *settings.json* should do the trick. – JialeDu Feb 24 '23 at 03:03