Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Virtual Environment not activated after restarting VScode

I installed the python extension for VSCode, and then created a new virtualenv .venv in my project directory.

If I press CTRL+SHIFT+P and select my environment, then open a new Terminal session, the new environment is automatically activated. However if I close VSCode and re-open it, then open a new Terminal session, the new environment isn't automatically activated. I can again manually activate it, however I would like it to activate automatically every time I open my project.

In my settings.json, I have tried using the following which was automatically filled by the extension:

"python.pythonPath": ".venv\\Scripts\\python.exe"

as well as

"python.pythonPath": "${workspaceFolder}/.venv/Scripts/python.exe"

Additionally, launching python and running the following indicates that it is using my root installation and no virtual environment:

>> import sys
>> sys.prefix
'C:\\Python37'

How can I get VSCode to automatically activate my environment after launch?


1 Answers

If you open the terminal before the Python extension has finished loading -- e.g. immediately after a restart -- then the terminal won't be activated because the extension isn't running yet to tell VS Code to run the command. Once you view a Python file then the extension will be triggered to load, and then once it's done -- you can tell as the message in the status bar about the loading will be gone -- then the terminal will automatically activate the virtual environment.

like image 52
Brett Cannon Avatar answered Jan 22 '26 04:01

Brett Cannon