简体   繁体   中英

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.

愚蠢的他妈的图片 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. It does work when I make a new environment, though, so at least I know that Python installed properly. 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 . 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. In your case it is located in 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.

  • The virtual environment interpreter is a copy of another interpreter created using the venv package from the Python standard library. 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. 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.

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. 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.

This also explains why your virtual environment files weren't affected by your upgrade.


Now, I am unfortunately no Python expert. I had to spend some time examining how Python handles virtual environments on Windows and 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.

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. 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.

  1. Close 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

    ('64bit', 'WindowsPE')

    If your output is different, you'll need to prefix the absolute path to the Python executable in step 4).

  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. 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

    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

    ... or simply try to run pip directly from the command line. Note you need to first activate the virtual environment by running the Scripts\activate.bat batch file.


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. See this PyCharm documentation for reference. 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.

Disclaimer

I tested only the Python's behavior alone on a fresh Windows installation inside the Windows Sandbox. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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