1

I was forced to use WSL on Windows 10 to install the Python library Cartopy on my Windows machine correctly. It is way easier in a Linux-distribution, so I chose Ubuntu 20.04 WSL2.

As a consequence, I created a virtual Python environment via WSL in my project folder via python -m venv linux-venv. The problem is now that all binaries/executables were compiled for the WSL-distribution and are thus not selectable from VS Code started from within Windows 10.

Consequently, I installed VS Code on WSL and started it from there. My hope was that I can then manually select the right interpreter path linux-venv/bin/python from my project's root directory. Unfortunately, it does not work either, even though I'm doing this from VS Code running on WSL:

select interpreter path

Clicking on my desired interpreter linux-venv/bin/python, the following error message is displayed:

error message

If I try to "browse to my desired executable" instead, they are not displayed since solely ".exe" - file extensions are allowed:

browse1

Next, I can solely browse ".exe" - files, wherefore I cannot select the UNIX-compiled executables.

browse2


Conclusion:

It does not seem possible at the moment to select a UNIX-compiled interpreter in VS Code properly on a Windows 10 - machine, not even using WSL.

Since the aforementioned Python library Cartopy solely works well on UNIX-systems, I need to install my virtual environment from WSL.

In order to continue with my project, I would need to be able to select the UNIX-compiled interpreter, but I cannot.

6
  • Have you tried WSL extension : code.visualstudio.com/blogs/2019/09/03/wsl2 Commented Mar 12, 2023 at 10:00
  • I have this extension installed. Yet, it does not help solve my above-mentioned problem. In the associated blog posts of your WSL2-link, the "selected interpreter" is always preselected and "just works". It seems to me as if they just use the global Python environment instead a local virtual environment. At least I don't see anything like how it is activated within VS Code via e.g. "select interpreter". Commented Mar 12, 2023 at 10:12
  • Do you use the installed extension ? If yes, you should be able to select any interpreter in WSL, including virtual environments. Commented Mar 12, 2023 at 10:47
  • What version of python are you using? Is this documentation useful? Commented Mar 13, 2023 at 6:01
  • The Python version is 3.9x. The docs are useful, but I just realized that if I select an existing workspace, the remote-connection to WSL vanishes (button in the bottom-left). Probably this happens, when the workspace had been created from within Windows. Could this be? Commented Mar 13, 2023 at 8:30

1 Answer 1

0

Problem Analysis:

I figured out what the actual problems were in order to make it work according to the kindly shared WSL-docs by @Philippe and @JialeDu:

  1. The VS-Code Workspace had been created from within Windows 10.
  2. The Python virtual environment (venv) was installed via a WSL2 (Ubuntu 20.04 LTS) - CLI, yet into a project folder on the C-Drive on Windows 10.

Consequently, starting VS-Code from within WSL or clicking on the "remote button" provided by the WSL-extension was fine, but when I opened the project it was still located on the Windows-C-drive. This silently broke the remote connection to WSL and left me working in VS-Code in Windows just like normally.

Obviously, this made it impossible to search for UNIX-executables and even select them, since I was effectively working on Windows, not on Linux (WSL).

Solution:

  1. Migrate the entire project folder from the C-Drive C:\Users\username\Projects\project_name to the actual Ubuntu-WSL-System in /home/username/projects/project_name.
  2. Create the virtual environment anew in the project folder via python3 -m venv linux-venv.
  3. Install all requirements again in that environment just created.
  4. Install the VS-Code Python Extension in the current WSL-version of VS-Code
  5. Open a Python-file and select the interpreter of the venv just configured

This time, it worked since I was effectively working on a true Linux environment/distribution with all packages and binaries installed within WSL.

Not only did I have to migrate the entire project on the WSL-distribution's directory tree, but I also had to reinstall the requirements and binaries of the virtual environments from there.

Sign up to request clarification or add additional context in comments.

2 Comments

I dont think you have to migrate the entire project folder from the C-Drive to WSL, as you can access /mnt/c/Users/username/Projects/project_name from WSL
In VS Code, the remote connection to WSL always vanished if I chose to open the folder on the C-Drive. Even after removing the workspace file etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.