重新安装操作系统后,使用 virtualenv 在 PyCharm 项目中“无法设置 Python SDK”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/52404529/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
'Cannot setup a Python SDK' in PyCharm project using virtualenv after OS reinstallation
提问by el_pup_le
I re-installed windows and opened an existing Pycharm project and get the error 'SDK seems invalid' in Settings > Project Interpreter.
我重新安装了 Windows 并打开了一个现有的 Pycharm 项目,并在“设置”>“项目解释器”中收到错误“SDK 似乎无效”。
The project interpreter path is pointing to python in the venv:
项目解释器路径指向 venv 中的 python:
MyProject\venv\Scripts\python.exe
I tried re-adding python.exe:
我尝试重新添加 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:
更新:这是idea.log中的一个错误,虚拟环境的许多其他问题似乎与Windows环境变量和系统路径有关:
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'
回答by silverFoxA
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.
导航到Project Interpreter
选择框右侧的 ,单击齿轮图标,它将显示两个选项添加和显示全部。
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.
单击全部显示,如果您从同一目录中看到以前的存在,请删除它。单击添加或+
签名以添加新的解释器并导航到您的project path
,向下导航到虚拟环境目录。
venv/bin/{select the python executable with the version code i.e if you are using python3.6 select python3.6}
venv/bin/{选择带有版本代码的python可执行文件,即如果您使用的是python3.6,请选择python3.6}
Once selected, you can now click the notice that says install packaging tools
, hit apply and done.
选择后,您现在可以单击通知install packaging tools
,点击应用并完成。
回答by pch
What solution is:Check out venv\pyvenv.cfg
and provide a valid path to the basic python installation.
什么解决方案是:检查venv\pyvenv.cfg
并提供基本python安装的有效路径。
What has most probably happend:After reinstalling your OS, you have no basepython 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 notprovide an isolated python installation. You still need your base python that had been used for venv creation.
最有可能发生的情况:重新安装操作系统后,您没有重新安装基本的Python 解释器,或者您已将其安装在与以前不同的位置。因此您的虚拟环境无法找到 python 安装。虚拟环境意味着所有库和设置都与其他项目隔离。它不提供隔离的 python 安装。您仍然需要用于创建 venv 的基础 Python。
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.
我的情况:我在网络共享中有一个项目,并尝试从不同的计算机访问它。基本的python 路径取决于PC。上面的解决方案对我来说很好。不幸的是,我需要pyvenv.cfg
根据使用的 PC进行更新。
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.
PS我相信有一个环境变量可以覆盖 venv 配置值。我只尝试set PYTHONPATH=C:\Anaconda3\envs\python37
在 Windows 上然后激活 venv。它没有效果,我放弃了。
回答by ivan_pozdeev
Most probably, some path to Python environment that PyCharm tries to use has become invalid somewhere. There are (at least) two primary suspects:
最有可能的是,PyCharm 尝试使用的 Python 环境的某些路径在某处变得无效。有(至少)两个主要嫌疑人:
Path to your virtualenv in PyCharm settings
PyCharm 设置中 virtualenv 的路径
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.
PyCharm 需要知道环境的路径才能在其中运行东西。因此,如果该路径发生更改,PyCharm 保存的路径将失效。
Go to the interpreter settings for your project in File->Settings...->Project interpreter->(Gear icon)->Show all...
:
转到您的项目的解释器设置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:
然后删除并重新创建必要的条目。或者编辑它们并指定正确的路径。例如,这是我删除 Anaconda 安装后列表的样子:
Path in the virtualenv to its base installation
virtualenv 中到其基本安装的路径
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.
由于 virtualenv 不是完整安装,它必须有一个存储在某个地方的基本安装的路径,以便能够使用那里的文件。
As of this writing, virtualenv
(v16.0.0
) in Windows is implemented like this:
在撰写本文时,Windows 中的virtualenv
( v16.0.0
) 是这样实现的:
- 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'sLib
tosys.path
via a customsite.py
.
- real
python.exe
和其他几个文件被复制到 virtualenv 的子树中 - 在 中
Lib\orig-prefix.txt
,存储了基本安装的路径。它用于基础设施的添加Lib
到sys.path
通过自定义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.
因此,如果该文件中的路径无效,则 virtualenv 的 Python 解释器将无法找到任何标准模块,除了那些被复制的少数模块。这完全符合你的症状。
回答by Koen
Well, i'm pritty new to Python, and I did too had a re-install of my os after a crash
好吧,我是 Python 的新手,而且我在崩溃后也重新安装了我的操作系统
Old setup: In the old system setup I used python 3.7.4. I made al my (practice)projects with that, and each project had a venv/scripts/python3.7.exe in it.... my undertanding is/was that all the files in these virtual environment where 'stand alone', so sufficient to run a python 3.7.4 for that specific projec files, and not depending on files of the home-python-dir. The install-directory of python was c:\Program Files (x86)\python 37-32
旧设置:在旧系统设置中,我使用了 python 3.7.4。我用它制作了我的(实践)项目,每个项目都有一个 venv/scripts/python3.7.exe ......我的理解是/是这些虚拟环境中的所有文件都是“独立”的,足以为该特定项目文件运行 python 3.7.4,而不依赖于 home-python-dir 的文件。python的安装目录是c:\Program Files (x86)\python 37-32
Each project had a file [project-path]/venv/pyvenv.cfg, and in this file there was the line home = C:\Program Files (x86)\Python37-32
每个项目都有一个文件 [project-path]/venv/pyvenv.cfg,在这个文件中有一行 home = C:\Program Files (x86)\Python37-32
then - my system crashed - reinstalled windows 10, and downloaded again python, but this time python 3.8, and it installed in C:\Program Files (x86)\Python38-32
然后 - 我的系统崩溃了 - 重新安装了 Windows 10,并再次下载了 python,但这次是 python 3.8,它安装在 C:\Program Files (x86)\Python38-32
New setup: So, after some hairs lost - I also installed the old python version 3.7.4 in the specific path stated in the pyenv.cfg file (C:\Program Files (x86)\Python37-32). So, i had 2 python versions installed on my new system, one in .../python38-32 and one in .../python37-32
新设置:因此,在丢失了一些头发之后 - 我还在 pyenv.cfg 文件 (C:\Program Files (x86)\Python37-32) 中指定的特定路径中安装了旧的 python 版本 3.7.4。所以,我在我的新系统上安装了 2 个 python 版本,一个在 .../python38-32 中,一个在 .../python37-32 中
And that worked, so when I selected in 'add interpreter/existing interpreter' and pointed to the [project]/venv/python3.7.exe it worked like a charm.
这奏效了,所以当我在“添加解释器/现有解释器”中选择并指向 [project]/venv/python3.7.exe 时,它就像一个魅力。
So, apearantly there are files in each version-specific home-directory that pycharm/python needs - i was under the impression that the files in the .venv directory would be all it needed....
因此,显然每个版本特定的主目录中都有 pycharm/python 需要的文件 - 我的印象是 .venv 目录中的文件就是它所需要的......
回答by bbqckwin
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.
我目前有同样的问题,我无法在 Pycharm 中安装任何打包工具,这让我很害怕。这是我修复它的步骤,只是想分享。欧。
Step 1
第1步
Find show all in the right corner of a setting icon
在设置图标的右上角找到显示全部
Step 2
第2步
Click the + icon to open Add python Interpreter
单击 + 图标打开添加 python 解释器
Step 3
第 3 步
In New environment 's Location Under Virtualenv Environment select file
在新环境的位置 Virtualenv Environment 下选择文件
Step 4
第四步
Find your Pycharm file and make an empty file under it and click OK and keep click OK
找到您的 Pycharm 文件并在其下创建一个空文件,然后单击确定并保持单击确定
Step 5
第 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.
现在在 Project Interpreter 中选择您刚刚创建的空文件,现在您应该可以安装 Pycharm 包了。希望这能解决您的问题。