Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Visual Studio Code working with python installed on WSL

I installed python3.6 on wsl and set my VSC integrated terminal to bash in the settings.json

If I set python.pythonPath: "python3" the VSC then warn me to select python environment and shows only the python installations on Windows. Is there a way I can add the python3 I installed on wsl to the python environment list in VSC or get rid of the warning?

Also, when I try to Run Python File in Terminal, it uses absolute path python3 c:/Users/xxx/Code/test.py which can't open the file in bash since there's no such file or directory. What do I need to change in the VSC settings so it will use python3 /mnt/c/Users/xxx/Code/test.py?

Similar issue reported on github regarding path translation in VSC.

Or is it better if I just run the python file from bash manually to avoid all the incompatibility issue with VSC and WSL?

like image 813
Ricek Avatar asked Nov 10 '17 05:11

Ricek


People also ask

Does Visual Studio work with WSL?

Visual Studio's WSL 2 toolset allows you to use Visual Studio to build and debug C++ code on WSL 2 distros without adding a SSH connection. You can already build and debug C++ code on WSL 1 distros using the native WSL 1 toolset introduced in Visual Studio 2019 version 16.1.

Where is Python path in WSL?

If you want to navigate to a file on your Windows file system from within this window, look in, e.g., /mnt/c/Users/<USER_NAME> . Show activity on this post. It will show you where is the python placed.


2 Answers

I was able to find a work around from similar issues submitted on Microsoft's vscode and WSL Github repository about php.

To Run Python File in Terminal in VSC, you will need to switch back to cmd as your integrated terminal in the settings.json

Create a batch file python3.bat as follow:

@echo off
set v_params=%*
set v_params=%v_params:\=/%
set v_params=%v_params:c:=/mnt/c%
set v_params=%v_params:"=\"%
bash.exe -c "python3 %v_params%"

Change set v_params=%v_params:c:=/mnt/c% accordingly based on your home. (ex. If you are running python file located in D: change this line to set v_params=%v_params:d:=/mnt/d%

In settings.json add/change as follow:

"python.pythonPath": "C:\\path\\to\\bat\\python3"

To test your settings, create a test.py file as follow:

import sys

print(sys.executable)

and right click in VSC to Run Python File in Terminal, the output should be /usr/bin/python3

Note: although the bat script worked but in VSC it will still warn you to Select Python Environment

like image 127
Ricek Avatar answered Oct 16 '22 03:10

Ricek


No, You are trying to run a linux program in a Windows program. This doesn't make sense.

You can use the WSL shell in vscode though. Add this to your user settings or remove the previous one and add this if there is already one.

"terminal.integrated.shell.windows": "C:\\Windows\\System32\\bash.exe"
like image 37
Anwar Hossain Avatar answered Oct 16 '22 03:10

Anwar Hossain