Using Python Virtual Environment in VSCode - Tech Inscribed (2024)

Using Python Virtual Environment in VSCode - Tech Inscribed (1)

Praveen

2 min read

Using Python Virtual Environment in VSCode - Tech Inscribed (2)

When trying to use a Python virtual environment in VSCode, it was not detecting the virtual environment. The solution was very simple, but I couldn’t find it on Google.

Most of the solutions found on StackOverflow and Github were suggesting to change the pythonPath in VSCode settings.

This article shows how to use a Python virtual environment in VSCode, without having to change the pythonPath in VSCode settings.

Just for the sake of completeness, we will start by installing virtualenv and creating a virtual environment. If you are only interested in the VSCode setup, skip to the VSCode setup section directly.

Install Virtualenv

We will be using virtualenv to create our python virtual environments. We will use pip to install it.

Install virtualenv using pip:

pip install virtualenv

Check your installation:

virtualenv --version

Create a Virtual Environment

First, let us create a folder .virtualenvs in the home directory. This is where we will keep all our virtual environments.

cd ~mkdir .virtualenvscd .virtualenvs

From inside the .virtualenvs directory, create a new virtual environment using virtualenv

virtualenv venv

Now, let us activate the virtual environment

source venv/bin/activate

We will install a package in the virtual environment so that we can test if the setup is working as expected.

pip install excel

We will create test.py that will import from the excel package that we just installed.

from excel import OpenExcel

VSCode Setup

At first, if you open the test.py file, assuming you have Python linter extension installed, VSCode will show an error like this.

Using Python Virtual Environment in VSCode - Tech Inscribed (3)

This is because VSCode is not using the correct python interpreter. There are two ways in which you can fix this.

1. Update Venv Path Settings in VSCode

Open VSCode preferences (Ctrl + ,) and search for “venv”. Add ~/.virtualenvs to the “Venv Path” settings, like so:

Using Python Virtual Environment in VSCode - Tech Inscribed (4)

Restart VSCode and click on the interpreter version on the left-bottom corner.

Using Python Virtual Environment in VSCode - Tech Inscribed (5)

Now, you will be able to see the virtual environment python interpreter in the interpreter list. Select it, now the issue should be resolved.

Using Python Virtual Environment in VSCode - Tech Inscribed (6)

2. Add the Virtual Environment Folder to VSCode.

Add the folder that contains the virtual environment to VSCode, in our case, it is the ~/.virtualenv folder. Once added, you will be able to select the interpreter by clicking on the interpreter version displayed on the left-bottom corner.

That’s all folks 🙂

Using Python Virtual Environment in VSCode - Tech Inscribed (2024)
Top Articles
Latest Posts
Article information

Author: Rev. Leonie Wyman

Last Updated:

Views: 6358

Rating: 4.9 / 5 (59 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Rev. Leonie Wyman

Birthday: 1993-07-01

Address: Suite 763 6272 Lang Bypass, New Xochitlport, VT 72704-3308

Phone: +22014484519944

Job: Banking Officer

Hobby: Sailing, Gaming, Basketball, Calligraphy, Mycology, Astronomy, Juggling

Introduction: My name is Rev. Leonie Wyman, I am a colorful, tasty, splendid, fair, witty, gorgeous, splendid person who loves writing and wants to share my knowledge and understanding with you.