'Cannot setup a Python SDK' in PyCharm project using virtualenv after OS reinstallation

102,037

Solution 1

What solution is: Check out venv\pyvenv.cfg and provide a valid path to the basic python installation.

What has most probably happend: After reinstalling your OS, you have no base python interpreter reinstalled or you have installed it at different location than before. Thus your virtual environment fails to locate the python installation. Virtual environment implies that all libraries and settings are isolated from other projects. It does not provide an isolated python installation. You still need your base python that had been used for venv creation.

My case: I have a project in a network share and try accessing it from different computers. The base python paths depend on the very PC. The solution above works fine for me. Unfortunatelly, I need to update pyvenv.cfg depending on the PC in use.

P.S. I believe that there is an environment variable to overide the venv config value. I only tried to set PYTHONPATH=C:\Anaconda3\envs\python37 on Windows and then to activate venv. It did no effect and I gave it up.

Solution 2

Here's what solved my problem when I faced the exact same issue

Navigate to Project Interpreter, right side of the selection box, click the gear icon, it will show two options add & show all.

Click show all, if you see the previous existence from the same directory delete that. Click on add or + sign to add a new interpreter and navigate to your project path, navigate down to the virtual environment directory.

venv/bin/{select the python executable with the version code i.e if you are using python3.6 select python3.6}

Once selected, you can now click the notice that says install packaging tools, hit apply and done.

enter image description here

Solution 3

Most probably, some path to Python environment that PyCharm tries to use has become invalid somewhere. There are (at least) two primary suspects:

Path to your virtualenv in PyCharm settings

PyCharm needs to know the path to your environment to run things in it. So, if that path changed, PyCharm's saved path has become invalid.

Go to the interpreter settings for your project in File->Settings...->Project interpreter->(Gear icon)->Show all...:

settings

Then delete and recreate the necessary entries. Or edit them and specify correct paths. E.g. this is what my list looks like after I deleted an Anaconda installation:

interpreter settings

Path in the virtualenv to its base installation

Since virtualenv is not a full installation, it must have a path to its base installation stored somewhere to be able to use files from there.

As of this writing, virtualenv (v16.0.0) in Windows is implemented like this:

  • The real python.exe and several other files are copied into the virtualenv's subtree
  • In Lib\orig-prefix.txt, the path to the base installation is stored. It is used to add the base installation's Lib to sys.path via a custom site.py.

So, if the path in that file becomes invalid, the virtualenv's Python interpreter will be unable to find any standard modules except those few that were copied. Which perfectly fits your symptoms.

Solution 4

I currently have the same issue, that I can't install any package tool in Pycharm and that freak me out. Here is the step I fixed it, just want to share. OuO.

Step 1

Find show all in the right corner of a setting icon  Find show all in the right corner of a setting icon

Step 2

Click the + icon to open Add python Interpreter  Click the + icon to open Add python Interpreter

Step 3

In New environment 's Location Under Virtualenv Environment select file  In New environment 's Location Under Virtualenv Environment select file

Step 4

Find your Pycharm file and make an empty file under it and click OK and keep click OK  Find your Pycharm file and make an empty file under it and click OK and keep click OK

Step 5

Now in Project Interpreter select the empty file you just create and now you should be fine to install Pycharm package.Hope this solve your problem.  Now in Project Interpreter select the empty file you just create and now you should be fine to install Pycharm package.Hope this solve your problem.

Solution 5

Nothing above worked for me

I made a simple change , Hope it works for you too !!

It happens due to conflict in storing python.exe , In my case it was in F:\ drive

Solution :- Pycharm expects python.exe file to be present in some location , you can check that in project interpreter , it will show some default location where pycharm is searching to execute exe file , but issue is the exe file is not present at that location , so create the folder which pycharm was expecting to execute exe file and paste the downloaded exe file

Hope it Works for you !!

Happy Coding

Share:
102,037

Related videos on Youtube

el_pup_le
Author by

el_pup_le

ಠ_ಠ

Updated on May 03, 2022

Comments

  • el_pup_le
    el_pup_le almost 2 years

    I re-installed windows and opened an existing Pycharm project and get the error 'SDK seems invalid' in Settings > Project Interpreter.

    The project interpreter path is pointing to python in the venv:

    MyProject\venv\Scripts\python.exe
    

    enter image description here

    I tried re-adding python.exe:

    enter image description here

    Thats when I get the error:

    enter image description here

    Update: here is an error from idea.log, a lot of other issues for virtual environments seem to be with windows environment variables and system paths:

    2018-09-28 19:50:40,275 [  17601]   INFO - hon.packaging.PyPIPackageCache - Loaded 153296 packages from C:\Users\Matt\.PyCharm2018.2\system\python_packages\pypi-cache.json 
    2018-09-28 19:50:40,816 [  18142]   INFO - rains.python.sdk.PythonSdkType - Fatal Python error: Py_Initialize: unable to load the file system codec
    ModuleNotFoundError: No module named 'encodings'
    
    Current thread 0x00000fa8 (most recent call first):
    
    Exit code -1073740791 
    2018-09-28 19:50:40,816 [  18142]  ERROR - ns.python.sdk.PythonSdkUpdater - Failed to determine Python's sys.path value:
    STDOUT: 
    STDERR: Fatal Python error: Py_Initialize: unable to load the file system codec
    ModuleNotFoundError: No module named 'encodings'
    
    Current thread 0x00000fa8 (most recent call first):
    
    com.jetbrains.python.sdk.InvalidSdkException: Failed to determine Python's sys.path value:
    STDOUT: 
    STDERR: Fatal Python error: Py_Initialize: unable to load the file system codec
    ModuleNotFoundError: No module named 'encodings'
    
    • kabanus
      kabanus over 5 years
    • el_pup_le
      el_pup_le over 5 years
      I think it is though I'm using a virtual environment version of Python not sure if I have to set windows paths for that.
    • ivan_pozdeev
      ivan_pozdeev over 5 years
      Okay, I found another possible cause that fits your symptoms.
    • mistakeNot
      mistakeNot over 5 years
      Could you create a new project with a new venv and then copy the files from the old project into the new project?
  • el_pup_le
    el_pup_le over 5 years
    I re-installed windows though so all the settings would of been deleted, or are they in the project's directory? I just copied and pasted the project folder onto disk then opened the project in PyCharm.
  • ivan_pozdeev
    ivan_pozdeev over 5 years
    @el_pup_le AFAICS interpreter environment settings are global, so they must be in %USERPROFILE%\.PyCharm<version>. If you didn't wipe your user profile during reinstallation, they survived.
  • el_pup_le
    el_pup_le over 5 years
    I re-installed windows so that must not be the solution
  • el_pup_le
    el_pup_le over 5 years
    what about the pycache or .idea folders in the project folder?
  • ivan_pozdeev
    ivan_pozdeev over 5 years
    @el_pup_le I've nothing else to add to my previous message. It doesn't really matter where the settings are, it only matters if they are correct -- and your error suggests they aren't.
  • el_pup_le
    el_pup_le over 5 years
    there is a pyvenv.cfg file in the venv directory, with home = ...\AppData\...\Python36-32, is that created by PyCharm?
  • el_pup_le
    el_pup_le over 5 years
    And I don't understand why its pointing to a global installation shouldn't it be the venv installation of Python?
  • ivan_pozdeev
    ivan_pozdeev over 5 years
    @el_pup_le what exactly is unclear in my post? I suggest to check and fix the settings in PyCharm itself, not hunt files around the disk.
  • ivan_pozdeev
    ivan_pozdeev over 5 years
    @el_pup_le If you're interested where PyCharm keeps its settings or what those files are, that's completly unrelated to the current question. (FWIW see jetbrains.com/help/pycharm/project-and-ide-settings.html on the former, the 1st in google on "where pycharm keeps global and project-specific settings").
  • el_pup_le
    el_pup_le over 5 years
    well i deleted the .idea folder and it didn't work, i just deleted the venv and created a new one now I'll have to install all the packages again
  • el_pup_le
    el_pup_le over 5 years
    I'd still like to know how to fix that problem though
  • Eir Nym
    Eir Nym over 5 years
    Additionally, this also could happen when you move your Virtual Environment around. Yes, it does not designed to be moved at all and you have to recreate a virtual environment which usually is not a big of a hassle.
  • ivan_pozdeev
    ivan_pozdeev over 5 years
    @EirNym that's reasonable 'cuz PyCharm needs to know path to the environment to run things in it, and if you move it, the path becomes invalid.
  • My3
    My3 over 5 years
    First make sure your python path is set. Try executing python from terminal. If that works, then create new virtual environment and that should work.
  • automationleg
    automationleg almost 5 years
    In my case I removed the [invalid] python interpreter from the "show all" view, removed venv folder and created venv again with new interpreter
  • borgr
    borgr almost 4 years
    None of the other methods worked for me. deleted .idea and it worked!
  • Eduardo
    Eduardo over 3 years
    That is why I pay Internet for!