How to make UNIX-Python-executable work in VS Code via "select interpreter" on a Windows machine?

huangapple go评论88阅读模式
英文:

How to make UNIX-Python-executable work in VS Code via "select interpreter" on a Windows machine?

问题

我被迫在Windows 10上使用WSL(Windows Subsystem for Linux)来正确安装Python库Cartopy。在Linux发行版上更容易,所以我选择了Ubuntu 20.04 WSL2。

因此,我通过WSL在我的项目文件夹中创建了一个虚拟Python环境,命令是python -m venv linux-venv
问题是现在所有的二进制/可执行文件都是为WSL分发编译的,因此无法从在Windows 10中启动的VS Code中选择。

因此,我在WSL上安装了VS Code,并从那里启动它。
我希望我可以从我的项目根目录手动选择正确的解释器路径linux-venv/bin/python
不幸的是,即使我是在WSL上运行的VS Code中执行这个操作,也不起作用:

How to make UNIX-Python-executable work in VS Code via "select interpreter" on a Windows machine?

单击我想要的解释器linux-venv/bin/python,会显示以下错误消息:

How to make UNIX-Python-executable work in VS Code via "select interpreter" on a Windows machine?

如果我尝试“浏览到所需的可执行文件”,它们没有显示,因为只允许“.exe”文件扩展名:

How to make UNIX-Python-executable work in VS Code via "select interpreter" on a Windows machine?

接下来,我只能浏览“.exe”文件,因此无法选择UNIX编译的可执行文件。

How to make UNIX-Python-executable work in VS Code via "select interpreter" on a Windows machine?


结论:

目前似乎无法在Windows 10机器上正确选择UNIX编译的解释器,甚至在使用WSL时也不行。

由于上述Python库Cartopy只能在UNIX系统上正常工作,我需要在WSL中安装我的虚拟环境。

为了继续我的项目,我需要能够选择UNIX编译的解释器,但我无法选择。

英文:

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:

How to make UNIX-Python-executable work in VS Code via "select interpreter" on a Windows machine?

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

How to make UNIX-Python-executable work in VS Code via "select interpreter" on a Windows machine?

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

How to make UNIX-Python-executable work in VS Code via "select interpreter" on a Windows machine?

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

How to make UNIX-Python-executable work in VS Code via "select interpreter" on a Windows machine?


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.

答案1

得分: 0

问题分析:

我已经弄清楚了实际问题,以便按照@Philippe和@JialeDu友好分享的WSL文档中的方法进行操作:

  1. VS Code工作区是在Windows 10内创建的。
  2. Python虚拟环境 (venv)是通过WSL2 (Ubuntu 20.04 LTS)的CLI安装的,但安装在Windows 10上C驱动器上的项目文件夹中。

因此,从WSL内部启动VS Code或单击由WSL扩展提供的"远程按钮"是可以的,但当我打开项目时,它仍位于Windows C驱动器上。这会静默中断与WSL的远程连接,使我像通常一样在Windows的VS Code中工作。

显然,这使得在Windows上工作而不是在Linux (WSL)上工作,因此不可能搜索UNIX可执行文件,甚至选择它们。

解决方案:

  1. 将整个项目文件夹从C驱动器C:\Users\username\Projects\project_name迁移到实际的Ubuntu-WSL系统中的/home/username/projects/project_name
  2. 通过python3 -m venv linux-venv在项目文件夹中重新创建虚拟环境。
  3. 在刚创建的环境中再次安装所有要求
  4. 在当前WSL版的VS Code中安装VS Code Python扩展
  5. 打开一个Python文件并选择刚刚配置的虚拟环境的解释器。

这一次,它起作用了,因为我实际上是在真正的Linux环境/发行版上工作,所有的包和二进制文件都安装在WSL内。

我不仅需要将整个项目迁移到WSL发行版的目录树中,还需要从那里重新安装虚拟环境的要求和二进制文件。

英文:

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.

huangapple
  • 本文由 发表于 2023年3月12日 17:10:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75712094.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定