- ‘Cannot setup a Python SDK’ in PyCharm project using virtualenv after OS reinstallation
- Solution 2
- Solution 3
- Path to your virtualenv in PyCharm settings
- Path in the virtualenv to its base installation
- Solution 4
- Solution 5
- Related videos on Youtube
- el_pup_le
- Comments
- Issue
- The problem
- The search for solution
- What I tried
- Solution
‘Cannot setup a Python SDK’ in PyCharm project using virtualenv after OS reinstallation
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.
Once selected, you can now click the notice that says install packaging tools , hit apply and done.
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. :
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:
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.
Find show all in the right corner of a setting icon
Click the + icon to open Add python Interpreter
In New environment ‘s Location Under Virtualenv Environment select file
Find your Pycharm file and make an empty file under it and click OK and keep click OK
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
Happy Coding
Related videos on Youtube
el_pup_le
Comments
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
I tried re-adding python.exe:
Thats when I get the error:
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'
Issue
Some time ago I seriously crashed my Windows computer while using PyCharm — I remember some errors about memory and then a hard crash with no blue screen — just black with some thin vertical lines and reboot to Windows installation / fixing screen. Since then, I had this problem, with no way I found online to fix this.
Edit : Apparently, this has nothing to do with the problem.
The problem
Whenever I open a project, or create a new one, an error appears with the Invalid Python SDK error message.
**Invalid Python SDK** Cannot set up a Python SDKat Python 3.9 (%projectName%) (%projectPath%).The SDK seems to be invalid.
Also, this is what the work environment looks like the moment I close this message. In the Project window, the venv directory (and every directory under it) is marked as an Exclusion, and in the code, the print(f’Hi, ‘) function is marked as an unresolved reference error shown below. The program, however, executes flawlessly.
What’s more, when I go to Python Interpreter settings at File -> Settings -> Project -> Python Interpreter there’s a yellow bar on the bottom which says:
which after some time says:
Python packaging tools not found.
Upon installing, nothing changes, and I can’t add packages from this screen (the ‘+’ button is greyed out):
When I try to check Python interpreter paths, there are no paths shown, and I don’t know what that means:
In short, all of the default Python functions like print are marked as errors, even though they work when executed. This makes coding extremely confusing, as I can’t quickly distinguish between real errors and ‘errors’.
The search for solution
Normally this would be a problem with interpreter set-up or path, but I’ve tried most of the methods proposed in other answers to similar questions. To name a few :
- PyCharm shows unresolved references error for valid code
- ‘Cannot setup a Python SDK’ in PyCharm project using virtualenv after OS reinstallation
- Why do I get an ‘SDK seems invalid’ error when setting up my Project Interpreter in PyCharm?
- Invalid Python SDK Error while using python 3.4 on PyCharm
- Invalid Python SDK when setting a venv
There were supposed to be links, but I don’t have enough reputation on Stack Overflow to post them with the questions. These, however, can be easily looked up in Google, all of them are posted to Stack Overflow.
What I tried
I should mention that the first things I tried were removing and installing PyCharm, all user configurations and Python itself as well. I installed Python from the official site, and from the PyCharm application, both methods ended with the same result.
- File -> Invalidate Caches. -> Invalidate and restart. Didn’t work.
- Checking file interpreter in Edit Configurations . Don’t know what to make of it. The result:
- Removing the interpreter and adding it again. No result.
- Deleting the .idea folder. No result.
- Deleting PyCharm user preferences under %homepath%/.PyCharm50 . I don’t have that folder though.
- Switching interpreter back and forth. No result.
- Creating a new interpreter in a different location. No result.
- Marking project directory as root ProjectName -> Mark Directory as -> Sources Root and unmarking other directories as Excluded. No result.
- Using no interpreter. Yeah, it doesn’t mark non-errors as errors anymore. But the code doesn’t work. That’s not a solution for me.
- Checking if venv/pyvenv.cfg has paths set correctly. These look fine to me.
- Checking Windows environment variables — Path variable. It was in the user section, but wasn’t in the system section. I added it, restarted but still no result.
- Changing account name in Windows. My account name was ‘username’ and that’s how my User folder is called `C:\Users\username’, but I later connected it to Microsoft account and my user name is now User Name with a space and I can’t really change it. My folder stayed the same. Not sure if I can fix it that way.
To the two last things I tried I should also add that I changed my Windows username from ‘username’ to ‘user name’ with a space, but that wasn’t until recently.
I’m attaching the idea.log file for you to check. I replaced my real username with ‘User Name’ to highlight the existence of a space.
Solution
OK, that was a lucky one! I’m thus posting my comment as an answer:
The problem is caused by the non-ASCII characters in the path, and the solution is to remove them. As indicated by @TheLazyScripter this is a known issue.