0

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.

dumb ffuckin picture 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.).

Cortisol
  • 15
  • 5
  • When you try to install a module with PIP in the terminal you have to activate the `venv` otherwise you are installing to the interpreter (which may resolve to different site-package locations). Consider [this thread](https://stackoverflow.com/q/62498127). The full explanation is somewhat complicated, see [PEP 250](https://www.python.org/dev/peps/pep-0250/) and [PEP 370](https://www.python.org/dev/peps/pep-0370/). – bad_coder Feb 05 '21 at 19:20

1 Answers1

2

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.

Marty Cagas
  • 350
  • 5
  • 14
  • Great answer! Although I'm a bit confused, how am I supposed to run `python -m venv .` in the project's root? I tried opening Command Prompt and using `cd C:\Users\solei\PycharmProjects\Will\venv` to run it, but nothing happened. Sorry if this is an obvious question btw, I'm still a beginner, haha. – Cortisol Jun 21 '20 at 02:13
  • If you're just talking about editing the `pyenv.cfg` file, I managed to do that with Notepad, but it still didn't fix my error. – Cortisol Jun 21 '20 at 02:20
  • @Cortisol My apologies for not being clear - `python -m venv {directory}` creates a new virtual environment in the specified directory. I just wanted to remark your setup is a bit unusual, but possible. You don't have to worry about the command itself, as PyCharm manages the virtual environment creation for you. – Marty Cagas Jun 21 '20 at 10:18
  • @Cortisol After a bit of research, I found the intended way of updating virtual environments. I have updated the answer, hopefully it contains the solution to your problem now. If my suggestions still won't work for you, be sure to reply again. – Marty Cagas Jun 21 '20 at 14:20
  • oh thanks for all the help!! Although I just created a new environment for the project which fixed my issues. – Cortisol Jun 22 '20 at 14:55
  • @Cortisol I expected that to be the reliable, albeit more time consuming option. Glad to hear you managed to solve the issue. – Marty Cagas Jun 22 '20 at 15:16