You often need to maintain many different environments for Python; various options are available to do this. Some of the options to create a virtual environment are covered in my blog post: How to set up a virtual environment for Python?
This post covers to ensure you set the virtual environment for Python in VS Code using venv. It details creating and activating a Python venv, and ensuring it’s used in VS Code environments. The steps include opening the VS Code command palette, selecting an interpreter, and navigating to the pyvenv.cfg file.
You can enable an environment in a Terminal inside VS Code for example withPython venv. The following code is an example to create and activate a Python venv.
python3.11 -m venv client-env-3.11
source ./client-env-3.11/bin/activate
But sometimes you maybe notice that even when the Python venv is activated, the Python environment in the VS Code terminal is different.
which python3
Here are the steps to ensure the Python venv is used in the VS Code you activated.
- Open the VS Code command pallette with Command + Shift + P

- Insert
Python: Select interpreterin the VS Code command pallette.

- Use Select at workspace level

- Now use Enter interpreter path …

- Use the option Find …

- Navigate to the
pyvenv.cfgfile in the./client-env-3.11/folder. - Open a new Terminal by selecting the
VS Code - Verify again that the right Python interpreter environment is set.
which python3
Thats all …
I hope this was useful to you and let’s see what’s next?
Greetings,
Thomas
#python, #vscode, #venv, #interpreter, #cheatsheet
Additional it can be useful:
In case of debugging:
1. Deactivate your existing virtual environment:
source deactivate
2. Create a new virtual environment using VS Code
- “Command + Shift + P“

- The created environment

- Restart VS Code and select the virtual environment
3. Activate the new virtual environment
source $(pwd)/.venv/bin/activate

Leave a comment