Search code examples
pythonvisual-studio-codepython-venvvirtual-environment

How can I change the default virtual environment command in Visual Studio Code?


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.


Solution

  • 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