A lot of my colleagues run into problems with their virtual environments in Visual Studio Code. We use venv as environment manager for Python projects and sometimes it feels like VS Code really doesn’t do what we expect of it. Here’s what you need to do to change virtual environments in VS Code on a Windows machine.
You can create a new environment in Code, by running the following command (usually, I simply use “.env” as name of the environment):
python -m venv .name_of_environment
If you are using PowerShell as terminal in Visual Studio Code, you’ll have to run the ps1 file.
If you are using the command prompt as terminal in VS Code, you’ll have to run the bat file.
In the bottom left corner, you can find the following icons. You should click on the Python 3.X.X and see if the active executable is pointing to the python.exe in your virtual environment folder.
If that’s not the case, you’ll be able to select it from the dropdown.
This should now be indicated in the bottom left of VS Code.
Oftentimes, this doesn’t work. It seems to happen when the virtual environment has been created outside the folder from your workspace. In this case, VS Code doesn’t pick it up automatically. Here’s what you can do to fix it.
Navigate to the settings of your Workspace.
Once you’re in this editor, you add the following lines to manually point to the python executable in your virtual environment.
Finally, if VS Code still hasn’t changed, it usually helps to close VS Code and open the folder in a new Workspace. Then go through the same steps again.
I don’t have any words to appreciate this post…..I am really impressed ….the person who created this post surely knew the subject well..thanks for sharing this with us.
Very helpful! A quick search to find just what I was looking for at the moment.
Thanks much. On OSX python.pythonPath worked as well:
$ python -m venv my_directory
$ which python
$ mkdir `.vscode`
$ touch `.vscode/settings.json`
Content of `settings.json`: