WSL: Setup VS Code for Python Development

WSL: Setup VS Code for Python Development

Visual Studio code has gained a lot of popularity recently as an IDE. You can work on almost any language / script using Visual Studio code. It has nice plugins to support syntax highlighting and intellisense.

In this article, let’s have a look at how visual studio code can be used with WSL. For the demo, we will try to setup Visual Studio Code for Python development. But you can use similar steps to setup any framework and continue your development on Linux using WSL and Visual Studio Code.

Prerequisites

There are only two prerequisites mentioned below.

Even if you do not have installed these prerequisites, it should not take very long to install them now.

Let’s start !

So, I assume we have all the ingredients required for this demo.

Now, start the WSL prompt by running WSL command (and optionally providing the Linux distribution name in the command line).

Then, navigate to any path on the Linux file system – which you want to open in Visual Studio code. Optionally you can also stay to the same path or you can navigate to Windows path mounted in Linux e.g. /mnt/c

Server

Next, run the command Code . in the Linux terminal.

If you have executed Code . for the first time, then this command may fetch some components from the web which are required to run VS code in WSL. This might take few seconds.

Linux Terminal - Instantiating VS Code Server
Linux Terminal – Instantiating VS Code Server

As you can see in above snapshot, it installs the VS Code Server component on WSL. So, the whole VS Code is divided into two parts – client and server. WSL will use client installed on windows and server from the Linux to facilitate the development on Linux.

Client

Once this is done, an instance of Visual Studio code will open. This is the VS Code which you have installed on Windows. But if you look at the status bar (bottom left corner), it would say WSL - ubuntu.

Not only this, but if you try to open any folder / file in this VS code instance, it would open a dialog from where you can select folders / files from the Linux file system as shown in below snapshot.

Visual Studio Code Client Opened by Windows
Visual Studio Code Client Opened by Windows

If you have more questions about this client-server architecture in Visual Studio, you can refer this documentation page.

Python App

If you have Ubuntu-18.04 LTS installed in WSL, then it has the Python 3.8 already installed. So, ensure that python is installed by using python3 --version command.

Installations

You can also run below script to update the python version and then install pip package manager and venv environments manager.

## Check if Phython is already installed
python3 –version
## If not installed Python, then update Ubuntu
sudo apt update && sudo apt upgrade
## Update the version of Python
sudo apt upgrade python3
## Install package manager for Phython
sudo apt install python3-pip
## Install VENV, standard module
## to create and manage the lightweight virtual environments
sudo apt install python3-venv
view raw python-dev.sh hosted with ❤ by GitHub

Create Virtual Environment

Now, create a HelloWorld folder, in which the new app will be created.

Then in the terminal, navigate to the newly created HelloWorld folder and then run below commands to create and activate the virtual environment with name .venv.

## Make sure you are inside HelloWorld Folder
## Create the virtual environment with name .venv
python3 -m venv .venv
## Activate the virtual environment
source .venv/bin/activate
## Create the python script file
touch python.py
## After your work is done, deactivate it by running
deactivate
view raw venv.sh hosted with ❤ by GitHub

Visual Studio Code Extension

Open the Visual Studio code as explained above (i.e. by running Code . on the Linux terminal).

Then once the VS Code is open, go to the Extensions from VS Code left navigation, and search for Python, an extension (ms-python.python) by Microsoft. You should see the extension as shown below. Click on install (green button) to install it.

Visual Studio Code: Install Python Extension by Microsoft
Visual Studio Code: Install Python Extension by Microsoft

After installation is done, you will need to reload (blue button in place of install button) the VS Code.

Next, you can open the terminal inside it by using View > Terminal option and navigate to HelloWorld directory.

If you already have executed the activate command on terminal, then the VS Code terminal would also show the .venv environment active in its terminal.

Hello Python

Next, you can run touch python.py command to create new file under HelloWorld folder. The file should be visible in the VS Code now.

Add the print statement as shown in below snippet.

print("Hello Python")
view raw Python.py hosted with ❤ by GitHub

Next, right click on file and select Run Python File in Terminal option to run the python file. This would run the python file and show the output in the terminal.

Visual Studio Code: Run Python Script
Visual Studio Code: Run Python Script

So, we have successfully ran the Python script on the Linux distro installed on WSL. I hope you find this demo useful. Let me know your thoughts.

Leave a Reply

This Post Has 3 Comments

  1. ADITYA NATH

    The Python Extension is not installing in the WSL env. I need serious help!

    1. Manoj Choudhari

      Not sure what is the exact issue. But probably, you can try restarting the WSL env (using wsl –shutdown) .