Visual Studio Code — Debug (multiple) Python with Conda Env in WSL

Stephen Cow Chau
2 min readMar 17, 2021

Background

I have this Python project that run multiple web services on Flask. I used to debug very inefficiently with adding pdb set_trace lines or print message to trace, but with Visual Studio Code as my major IDE (mainly for Javascript), I would like to leverage it to make my Python programming more efficient.

Challenges

I use Windows, but I prefer Linux when I develop, so I use WSL (Window Linux Subsystems)

I use Conda to manage my Python package, and the environments are all in file system inside WSL.

When using Visual Studio Code, it can detect the WSL environment for terminal shell, but when it comes to debug launch.json setup, it does not recognize the virtual environments in my WSL.

Solution

Idea is having Visual Studio Code to run the workspace in WSL (remotely), and setup the Python Extension and launch.json

For how to setup Visual Studio Code to remote to WSL and setup extension, follow the official document, it’s very clear.

I would like to share my launch.json (simplified):

{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Web Service 1",
"type": "python",
"request": "launch",
"program": "app.py",
"console": "integratedTerminal",
"cwd": "/path/to/web service 1/app.py folder/",
"python": "/path/to/miniconda3/envs/envName/bin/python",
"args": ["8964"]
},
{
"name": "Python: Web Service 2",
"type": "python",
"request": "launch",
"program": "app.py",
"console": "integratedTerminal",
"cwd": "/path/to/web service 2/app.py folder/",
"python": "/path/to/miniconda3/envs/envName/bin/python",
"args": ["9413"]
}
]
}

Note that assuming the command to run web service 1 is “python app.py 8964”

  • the “cwd” (change working directory) point to the app.py folder
  • the “python” key point to the conda env python binary full path
  • the “program” key point to the “app.py” need to be run by python
  • the “args” add the args after the file, which is port number for my case

This would setup multiple entry routes for run and debug

And upon run, it would spawn a new process with a new debug console (which means I can run multiple web services on single machine to debug)

Console “2: Python Debug Console” is the one that spawn by running debug on web service 2

--

--