简体   繁体   English

如何将项目重新路由到 PyCharm 中的另一个 Python 版本? (错误:'C:\Users\...\python.exe' 处没有 Python)

[英]How do I reroute a project to another Python version in PyCharm? (Error: No Python at 'C:\Users\…\python.exe')

I wanted to replace Python 3.8 32-bit with the 64-bit version to install the face_recognition module, so I deleted the previous version and tried to re-route the project to the new Python version by going to File > Settings > Project Interpreter > Show all > Show Paths for Selected Interpreter, and adding all the Python files from the new folder and getting rid of the old ones.我想用 64 位版本替换 Python 3.8 32 位以安装 face_recognition 模块,所以我删除了以前的版本并尝试通过转到文件 > 设置 > 项目解释器 > 将项目重新路由到新的 Python 版本Show all > Show Paths for Selected Interpreter,并从新文件夹中添加所有 Python 文件并删除旧文件。

愚蠢的他妈的图片 smh

However, it's still showing me this error when I try to install the module:但是,当我尝试安装模块时,它仍然向我显示此错误:

(Will2.0) C:\Users\solei\PycharmProjects\Will>pip install face_recognition
No Python at 'C:\Users\solei\AppData\Local\Programs\Python\Python38-32\python.exe'

I've also tried going to the Windows System Properties and changing everything that says "Python38-32" there, but it's still not working.我还尝试转到 Windows 系统属性并更改那里所有显示“Python38-32”的内容,但它仍然无法正常工作。 It does work when I make a new environment, though, so at least I know that Python installed properly.不过,当我创建一个新环境时它确实有效,所以至少我知道 Python 安装正确。 It's just this one environment that is tripping me up (I'd prefer not to make a new project for this, btw. I've already installed a lot of modules in it.).只是这个环境让我感到困惑(顺便说一句,我不想为此创建一个新项目。我已经在其中安装了很多模块。)。

Your selected interpreter is not the system interpreter you've replaced with the 64-bit version, but your project's virtual environment interpreter .您选择的解释器不是您已替换为 64 位版本的系统解释器,而是您项目的虚拟环境解释器 The virtual environment's files weren't changed in that process and need to be updated before you can use that environment again.虚拟环境的文件在该过程中未更改,需要更新才能再次使用该环境。

  • The system interpreter is your Python interpreter installed using the installation executable.系统解释器是使用安装可执行文件安装的 Python 解释器。 In your case it is located in C:\Users\solei\AppData\Local\Programs\Python\Python38\ .在您的情况下,它位于C:\Users\solei\AppData\Local\Programs\Python\Python38\ You can have multiple system interpreters installed, such as having Python 2.7, Python 3.7 and Python 3.8 side-by-side.您可以安装多个系统解释器,例如在旁边安装 Python 2.7、Python 3.7 和 Python 3.8。

  • The virtual environment interpreter is a copy of another interpreter created using the venv package from the Python standard library.虚拟环境解释器是使用来自 Python 标准库的venv package 创建的另一个解释器的副本。 You can have many virtual environments interpreters in the system (one or more for every project, for example)您可以在系统中拥有许多虚拟环境解释器(例如,每个项目一个或多个)

  • The base interpreter is the interpreter that was used as a template for the venv package.基本解释器是用作venv package 模板的解释器。 Every virtual environment interpreter has its base interpreter (usually a system interpreter) that it requires to run.每个虚拟环境解释器都有其运行所需的基本解释器(通常是系统解释器)。 Changing or upgrading the base interpreter requires updating the virtual environment.更改或升级基本解释器需要更新虚拟环境。

If we take a quick look at the documentation , a virtual environment is described as如果我们快速浏览一下文档,虚拟环境被描述为

a self-contained directory tree that contains a Python installation for a particular version of Python, plus a number of additional packages.一个自包含的目录树,其中包含特定版本的 Python 的 Python 安装,以及许多附加包。

That means you can setup an individual environment for every project, which will contain its own packages.这意味着您可以为每个项目设置一个单独的环境,其中包含自己的包。 The environment is a very efficient way of managing project packages, that's why PyCharm suggests a creation of such environment over the system interpreter by default.环境是管理项目包的一种非常有效的方式,这就是为什么PyCharm 默认建议在系统解释器上创建这样的环境。 In short, it allows you to have two different versions of the same package used by two different projects, without the packages conflicting with each other.简而言之,它允许您拥有两个不同项目使用的同一个 package 的两个不同版本,而包不会相互冲突。

This also explains why your virtual environment files weren't affected by your upgrade.这也解释了为什么您的虚拟环境文件不受升级影响。


Now, I am unfortunately no Python expert.现在,不幸的是,我不是 Python 专家。 I had to spend some time examining how Python handles virtual environments on Windows and Ubuntu.我不得不花一些时间研究 Python 如何处理 Windows 和 Ubuntu 上的虚拟环境。 It seems the environment always requires the base system interpreter present in the system.似乎环境总是需要系统中存在的基本系统解释器。 If you remove or change the location of the base interpreter, the environment will fail to function.如果您删除或更改基本解释器的位置,环境将失败为 function。

As I mentioned before editing this answer, you can in theory simply edit the pyenv.cfg file located in the root folder of the virtual environment.正如我在编辑此答案之前提到的,理论上您可以简单地编辑位于虚拟环境根文件夹中的pyenv.cfg文件。 In practice, that will only work in simple cases and it is not the intended way of updating virtual environments.在实践中,这只适用于简单的情况,它不是更新虚拟环境的预期方式。

You need to upgrade your virtual environment's files to work with your new system interpreter.您需要升级虚拟环境的文件以使用新的系统解释器。 That can mean the 64-bit version over the 32-bit version, or even a newer version of Python - such us upgrading from 3.7 to 3.8.这可能意味着 64 位版本超过 32 位版本,甚至是 Python 的更新版本 - 例如我们从 3.7 升级到 3.8。

  1. Close PyCharm关闭 PyCharm

  2. Check if the system interpreter you want to upgrade to is on the system Path检查您要升级到的系统解释器是否在系统路径上

    You can quickly check by running您可以通过运行快速检查

    python -c "import platform; print(platform.architecture())"

    For you, the output should look like this对你来说,output 应该是这样的

    ('64bit', 'WindowsPE')

    If your output is different, you'll need to prefix the absolute path to the Python executable in step 4).如果您的 output 不同,则需要在步骤 4 中为 Python 可执行文件的绝对路径添加前缀)。

  3. Navigate to the virtual environment's directory导航到虚拟环境的目录

    The directory you're looking for contains the Include , Lib and Scripts directories and the pyenv.cfg file.您要查找的目录包含IncludeLibScripts目录以及pyenv.cfg文件。 From your screenshots, it seems this directory is your project's root directory, so in your case:从您的屏幕截图中,该目录似乎是您项目的根目录,因此在您的情况下:

     cd C:\Users\solei\PycharmProjects\Will2.0\
  4. Upgrade the virtual environment升级虚拟环境

    python -m venv --upgrade.

    ... or if Python is not on your path ...或者如果 Python 不在您的路径上

    C:\Users\solei\AppData\Local\Programs\Python\Python38\python.exe -m venv --upgrade.

    The . . in the commands refers to the current directory.在命令中指的是当前目录。

  5. Open PyCharm and verify your environment is working correctly打开 PyCharm 并验证您的环境是否正常工作

    ... or simply try to run pip directly from the command line. ...或者只是尝试直接从命令行运行pip Note you need to first activate the virtual environment by running the Scripts\activate.bat batch file.请注意,您需要首先通过运行Scripts\activate.bat批处理文件来激活虚拟环境


If the above-mentioned method doesn't work, you might have to create a new virtual environment.如果上述方法不起作用,您可能需要创建一个新的虚拟环境。 You can create one easily without making a new PyCharm project.您可以轻松创建一个,而无需创建新的 PyCharm 项目。 See this PyCharm documentation for reference.请参阅此PyCharm 文档以供参考。 However, you'll still need to redownload all the required packages again.但是,您仍然需要再次重新下载所有必需的软件包。

For the simplicity, I recommend creating the new virtual environment in a .venv folder located in the project's root.为简单起见,我建议在项目根目录下的.venv文件夹中创建新的虚拟环境。

Disclaimer免责声明

I tested only the Python's behavior alone on a fresh Windows installation inside the Windows Sandbox.我仅在 Windows 沙箱内的新 Windows 安装上单独测试了 Python 的行为。 I was able to install the 32-bit Python, create a virtual environment, replace Python with the 64-bit version and upgrade the virtual environment to have it launch correctly again.我能够安装 32 位 Python,创建虚拟环境,用 64 位版本替换 Python 并升级虚拟环境以使其再次正确启动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM