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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With