VScode Python extension: set up interpreter, enable venv conda virtualization, autorun
Visual Studio Code, or VSCode for short, is the most popular IDE of all time, created by Microsoft and grown by countless extension developers around the world. VSCode supports any language through extensions, is flexible and open. It's fast, and best of all, it's free.
Of all the languages, Vscode's support for Python is one of the most reliable, and it's easy to find useful extensions. Some of them, like PyCharm, are not found in IDEs built exclusively for Python. New extensions are popping up all the time to catch the attention of developers.
With all these advantages, you only need to install one extension to get started with Python development in vscode.
This is the Python extension shown below.
1. Installing the vscode Python Extension
Click on the Extensions tab in vscode, search for python
and install the first listed extension.
Make sure it's from Microsoft. Once installed, the Extension Preferences tab will open automatically.
Finally, the Jupyter Notebook extension is also installed. Let's start by setting the Python language.
2. Set up the Python interpreter
Click the Select Python Interpreter button to see a list of Python interpreters installed on your computer. Select the version you want.
For more information on installing Python, see the Download and Install (Mac) or the Download and Install (Win).
If the Python interpreter you want isn't listed, add the path to it using the Enter interpreter path entry at the top of the list.
If you didn't set it up on this page, you can find it by opening the command palette and searching for `python interpreter'.
Now open your Python file and make sure you've set it to the version of Python you want. To do this, we'll run a Python file, and thanks to the official Python extensions, running a Python file is easy.
3. Running a Python file
The code below prints out the absolute path and version of the Python interpreter that is currently running.
Click the Play button in the top right corner, or press F5
on your keyboard.
If it's good code, you should see the results of execution in a terminal window inside Vscode. I chose Mac native Python and got the following results
4. Enable the venv and conda virtual environments
Each of the different Python interpreters installed on our machine has its own virtual environment. The name makes it sound like a Docker or a VM, but it's really just a way of calling an interpreter and its associated packages together.
For example, let's say we use the same interpreter for two projects. If we install all the packages used by both projects in one VM, we'll end up with a lot of useless packages for each project. So I create a virtual machine for each project to install only the packages I really need.
Vscode supports two ways of creating a virtual environment.
The first is to use the venv
package included in the standard Python library, and the second is to use the Conda package manager included in the Anaconda or Miniconda distributions.
Let's go through them one by one.
4.1. Creating a virtual machine with venv
First, open the project directory where you want to create the venv with open folder in Vscode.
Then open the command palette and search for create python environment or python environment. You will see an entry like the one below.
If you click on it, a window will open with a choice between venv
and conda
. Choose venv
.
Finally, select the interpreter you want to use in the virtual environment from the list.
Vscode will install the new virtual environment and minimal packages by itself.
Once it's finished loading, you'll see a .venv
folder created and a dedicated interpreter set up in the bottom right corner of Vscode.
.venv folder | Interpreter |
---|---|
![]() | ![]() |
4.2. Creating a virtual environment with CONDA
Creating a virtual environment is the same as with venv.
Open your project folder, search for Create Python Environment
in the command palette, and click on it.
Since we will be using Conda, select conda
.
If you haven't already connected Vscode and Conda, you'll see an error message like this
To connect Vscode and Conda, you need to find the path to the Conda that your machine uses. You can also use Mamba, the community version, instead of Conda.
If you installed Micromamba instead of Mambaforge, you need to download and install the separate Vscode extension Micromamba (opens in a new tab) and install it.
The instructions are also slightly different. Create a virtual environment using the `Micromamba: create environment' action in the command palette.
In the terminal, type the command which conda
or which mamba
.
It's time to enter the paths you found into Vscode.
Open Settings (UI) in the command palette and search for conda
.
In the Python: Conda Path entry, enter the path you got above.
I used Mamba instead of Conda.
Now go back to Create Environment and click on the conda entry, it will take you to the next step and ask you to choose an interpreter version.
After selecting the desired version, a Conda-based virtual environment will be created for you. Check the .conda
folder and the interpreter version in the bottom right corner.
.conda folder | Interpreter |
---|---|
![]() | ![]() |
4.3. Automatically enabling a virtual environment from the Vscode terminal
One of the main reasons to use a virtual environment is to install packages.
Package installation is done in the terminal with the pip
command (using venv) or conda
command (using Conda).
Previously, you had to manually enable the VM by typing the following commands in the terminal to install packages.
venv
:source .venv/bin/activate
Conda/Mamba
:conda activate ./.conda/envs/default
(Depending on the name of the virtual environment, this may not bedefault
).
For now, Vscode will automatically detect your virtual environment and activate it for you whenever you create a new terminal. All you need to do is set the following Search for Settings (UI) in the command palette to open a window.
Type python terminal
in the settings search box and you should see something like this.
There are two settings to check: Enable Env In Current Terminal and Enable Environment.
You can do this for both user and workspace.
Now when you go back to your Python file and open a new terminal, it will automatically activate the interpreter for the current virtual environment.
The new terminal must be opened from a Python file ending in .py
for Vscode to recognize it well.
From the terminal, you should now be able to install a package that is only used by the currently active virtualization.
5. Using Jupyter Notebooks
With Microsoft's support for Jupyter Notebook as an extension,
you can now use notebooks within vscode without opening a browser.
The Jupyter extension is installed with the Python extension.
If you don't have it installed, search for Jupyter
and install all extensions supported by Microsoft.
Now let's create a new notebook, search for jupyter
in the command palette and create a new notebook.
Once the new notebook is open, you need to set the kernel for the current notebook.
The kernel is the engine that waits to execute the code in the blocks of code you write in your notebook.
You currently have two options
If you install extensions like Docker
or CodeSpace
in Vscode, your options will increase.
The Python environment... refers to the virtual environment we created above.
When we select the virtual environment, the IPyKernal
package is executed by itself to start the kernel.
The Existing Jupyter server... option can be selected if you already have a server running. For now, select the one we created.
If all goes well, the kernel name will be printed in the top right corner of the notebook.
Now you can install the required packages with pip
or conda
and use the notebook.
6. Conclusion.
Vscode continues to make Python development easy and intuitive for us through its official support. Thanks to this, even if you're new to Python development in Vscode, it wasn't hard to follow along from setting up the basics, creating a virtual environment, and using Jupyter notebooks.
But it doesn't stop there - vscode is waiting to do tons of work for you during your Python development journey. If you pay attention to the features of the IDEA you're using as you develop, the effort will pay off in increased development productivity. I'll be sure to keep posting my thoughts.
