未能找到用于内置发现 python_spec 的解释器
目标:
我正在尝试使用特定 python 版本创建一个新的虚拟环境。
问题
当我输入:
python3 -m virtualenv --python="D:\Python_Versions\python.exe" new_virtualenv
我得到:
运行时错误:无法找到用于内置发现 python_spec='D:\Python_Versions\python.exe' 的解释器
我所做的事情:
- 安装适用于 Windows 的 Ubuntu Bash:
打开 Windows 命令提示符
wsl --install
源: https://learn.microsoft.com/en-us/windows/wsl/install
- 安装 Pip
打开 Windows 命令提示符:
bash
然后
sudo apt-get install python-pip
- 安装 Virtualenv
键入
pip install virtualenv
- 下载 python 版本(此处为 3.6)
转到:https://www.python.org/downloads/release/python-360/
下载:Windows x86-64 可执行安装程序
安装到您选择的文件夹中,即 D:\Python_Versions\
- 尝试创建具有特定 python 版本的虚拟环境(失败)
在命令中提示,转到您的项目文件夹(“cd ...”)并输入:
python3 -m virtualenv --python="D:\Python_Versions\python.exe" new_virtualenv
GOAL:
I am trying to create a new virtual environment with a specific python version.
ISSUE
When I type:
python3 -m virtualenv --python="D:\Python_Versions\python.exe" new_virtualenv
I get:
RuntimeError: failed to find interpreter for Builtin discover of python_spec='D:\Python_Versions\python.exe'
WHAT I HAVE DONE:
- Install Ubuntu Bash for Windows:
Open Windows Command Prompt
wsl --install
Source:
https://learn.microsoft.com/en-us/windows/wsl/install
- Install Pip
Open Windows command prompt:
bash
Then
sudo apt-get install python-pip
- Install Virtualenv
Type
pip install virtualenv
- Download python version (Here 3.6)
Go to: https://www.python.org/downloads/release/python-360/
Download: Windows x86-64 executable installer
Install into folder of your choice, i.e. D:\Python_Versions\
- Attempt to create virtual environment with specific python version (Failed)
In the command prompt, go to your project folder ("cd ...") and type:
python3 -m virtualenv --python="D:\Python_Versions\python.exe" new_virtualenv
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一般来说,您的问题不在于尝试指定不同的 Python 版本,而在于尝试将 Python 可执行文件用于不同的操作系统。在 WSL 中,您实际上是在运行 Linux 二进制文件和库,但您尝试让 Linux Python 使用 Windows Python 可执行文件创建虚拟环境。
有很多原因可以解释为什么这是一个坏主意,您收到的错误说明了其中之一:
换句话说,Linux Python解释器
python3
无法解析路径D:\Python_Versions\python.exe
。WSL 提供了一个很好的“转换”实用程序
wslpath
,可以在 Linux 路径和 Windows 路径之间进行转换。例如:/mnt/d/Python_Versions/python.exe
<->D:\Python_Versions\python.exe
/home/user/username/src/python_project
<->\\wsl$\\home\user\username\src\python_project
但是,这不会帮助您解决这个特定问题。您需要 Linux Python 来转换所有这样的路径,但它不会这样做。
此外,虽然 WSL 可以运行 Windows 可执行文件(例如
python.exe
),但这些可执行文件将无法访问 Linux 库和进程。这不起作用的另一个原因是 Windows Python 无法在伪网络共享驱动器上运行,否则该驱动器允许 Windows 进程访问 WSL/Linux 文件。例如,如果您在 PowerShell 中:
但是(也来自 PowerShell):
假设 Windows Python 在您的 Windows 路径中,您可以,另一方面,在 WSL 中:
但是,您不能激活虚拟环境,因为 Windows Python 不会为 Linux 创建激活脚本。它确实创建了一个 Bash 脚本,但它是针对 Git Bash 的,如果您检查它(该 venv 目录中的
./Scripts/activate
),您仍然会看到它使用Windows路径。确实没有办法解决这个问题:
相反:
旁注:您现在可能已经意识到,
virtualenv
的--python
标志旨在允许您指定与操作系统架构匹配的不同 Python 版本。换句话说,如果您使用的是 Ubuntu/WSL(鉴于您的pip
示例,我不相信您是这样):更新,解决您的附加评论:
由于 Spyder 本身就是一个 Python 应用程序,因此它在“跨平台”通信方面会遇到与 Python
virtualenv
相同的问题。您可以尝试将 Windows Python 可执行文件设置为 WSL Spyder 终端,但实际上无法启动它,因为 Linux Spyder 无法与spyder-kernel
模块进行通信即使您将其安装在 Windows 中。反之亦然。
我只是不知道(并且在我的研究中找不到任何)WSL <-> Spyder 中的 Windows 互操作功能。
您还没有解释需要 Windows 和 Linux Python 环境的用例,但是如果您要使用 Spyder,并且您确实需要 Windows 和 Linux Python 二进制文件,那么我相当有信心您需要安装 Spyder 两次——一次在 Windows 中,一次在 WSL/Linux 中。
据我了解,Conda 是在 Linux 中安装 Spyder 的最佳选择。您也可以查看这个 Ask Ubuntu 答案,但不幸的是,这对我不起作用。
另一种选择
如果您没有完全依赖 Spyder,您可以考虑 Visual Studio Code(Windows 版本)。它确实能够与Windows和Linux/WSL Python解释器交互。
安装“Remote - WSL”扩展(来自 Microsoft)后,它可以通过在 WSL 内设置 Linux 服务器来与 Linux Python 交互。您可以访问 Windows Python 虚拟环境(处于“Windows 模式”时)和 WSL 虚拟环境(处于“远程 - WSL”模式时)。
虽然我自己没有尝试过这些特定功能,但根据我对 VSCode 和 WSL 的了解,我相当确定您可以:
它对两个平台都有调试支持,可以支持变量检查(基于这个答案),并且可能还有很多很多其丰富的 扩展库(当前搜索“Python”返回 652 个扩展)。
绝对值得一试,看看它是否能满足您的需求,因为它是少数可以直接与 WSL 互操作的 Windows IDE 之一。我的理解是 Pycharm 也有 WSL 集成,但我个人还没有探索过。
In general, your problem isn't in trying to specify a different Python version, but in trying to use a Python executable for a different operating system. When in WSL, you are essentially running Linux binaries and libraries, but you are attempting to have the Linux Python create a virtual environment using the Windows Python executable.
There are a number of reasons why this is a bad idea, and the error you are getting demonstrates one of them:
In other words,
python3
, the Linux Python interpreter, can't parse the pathD:\Python_Versions\python.exe
.WSL provides a nice "translation" utility
wslpath
that can convert between Linux paths and Windows paths. For instance:/mnt/d/Python_Versions/python.exe
<->D:\Python_Versions\python.exe
/home/user/username/src/python_project
<->\\wsl$\<distroname>\home\user\username\src\python_project
However, that's not going to help you with this particular problem. You would need the Linux Python to convert all paths like this, which it isn't going to do.
In addition, while WSL can run Windows executables (such as
python.exe
), those executables won't be able to access Linux libraries and processes.And yet another reason this won't work -- Windows Python can't operate on the pseudo-network share drive that otherwise allows Windows processes to access WSL/Linux files. For instance, if you are in PowerShell:
But (also from PowerShell):
Assuming that Windows Python is in your Windows path, you can, on the other hand, in WSL:
However, you can't activate the virtual environment since Windows Python doesn't create activation scripts for Linux. It does create a Bash script, but it's for Git Bash, and if you examine it (
./Scripts/activate
in that venv directory), you'll see it still uses Windows paths.There's really just no way around it:
Conversely:
Side note: As you might realize by now, the
--python
flag forvirtualenv
is designed to allow you to specify a different Python version that matches the OS architecture. In other words, if you are on Ubuntu/WSL (which I don't believe you are, given yourpip
example):Update, addressing your additional comments:
Since Spyder is a Python app itself, it's going to have the same problem communicating "cross platform" that the Python
virtualenv
does. You can try to set your Windows Python executable as the WSL Spyder terminal, but it wouldn't actually be able to launch it since the Linux Spyder won't be able to communicate with thespyder-kernel
module even if you install it in Windows.And vice-versa.
I'm just not aware of (and couldn't find any, in my research) WSL <-> Windows interop features in Spyder.
You haven't explained your use-case for needing both Windows and Linux Python environments, but if you are going to use Spyder, and you really need both Windows and Linux Python binaries, then I'm fairly confident that you'll need to install Spyder twice -- Once in Windows and once in WSL/Linux.
And from what I understand, Conda is the best option for installing Spyder in Linux. You might also check out this Ask Ubuntu answer, but unfortunately that did not work for me.
Another option
If you aren't completely tied to Spyder, you might consider Visual Studio Code (the Windows version). It does have the ability to interact with both Windows and Linux/WSL Python interpreters.
After installing the "Remote - WSL" extension (from Microsoft), it can interact with the Linux Python by setting up a Linux server inside WSL. You can access Windows Python virtual environments (while in "Windows mode") and WSL virtual environments (while in "Remote - WSL" mode).
While I haven't tried these particular features myself, I am fairly certain, based on what I know of VSCode and WSL, that you can:
It has debugging support for both platforms, can support variable inspection (based on this answer), and probably much, much more through its rich extension library (currently 652 extensions returned from a search for "Python").
It's definitely worth checking out to see if it will meet your needs since it's one of the few Windows IDE's that can interoperate with WSL directly. My understanding is that Pycharm also has WSL integration, but I haven't explored that personally.
在MacOS上,如果使用pyenv查找目标python版本二进制的位置。在我的情况下是
〜/.pyenv/versions/3.7.16/bin/python3.7
然后做:
virtualenv venv -p =〜/.pyenv/versions/3.7.16/bin/python3.7
On macOS if using pyenv find the location where the target python version binary is located. In my case it was
~/.pyenv/versions/3.7.16/bin/python3.7
Then do:
virtualenv venv -p=~/.pyenv/versions/3.7.16/bin/python3.7
就我而言,我可以通过安装我的错误提到的特定版本来解决它。
brew list
列出了 v3.12 和 v3.13,但没有列出 v3.11,所以我运行了In my case I was able to resolve it by installing the specific version that my error mentioned.
brew list
listed v3.12 and v3.13 but not v3.11, so I ran