python-ImportError:没有名为“ encodings”的模块
我最近重新安装了ubuntu并升级到16.04,无法使用python:
$ python manage.py runserver
Could not find platform independent libraries
Could not find platform dependent libraries
Consider setting $PYTHONHOME to [:]
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ImportError: No module named 'encodings'
Aborted
在这一点上,python本身不起作用
$ python
Could not find platform independent libraries
Could not find platform dependent libraries
Consider setting $PYTHONHOME to [:]
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ImportError: No module named 'encodings'
Aborted
甚至这个建议也不再起作用:
unset PYTHONHOME
unset PYTHONPATH
每一次我用一种方式修复它,它都会再次出现。 有几个答案有助于暂时修复它,但不是永久性的。 我已经重新安装了python和python3几次。 我可以从这里做什么? 谢谢
7个解决方案
60 votes
对于Python-3,请尝试删除虚拟环境文件。 并重新设置它。
rm -rf venv
virtualenv -p /usr/bin/python3 venv/
source env/bin/activate
pip install -r requirements.txt
[https://wiki.ubuntu.com/XenialXerus/ReleaseNotes#Python_3]
Anoop Malav answered 2019-10-03T08:10:39Z
11 votes
对于Windows10用户。
我在Windows10上使用python3.4。 我安装了python3.5。 我找不到PYTHONPATH,PYTHONHOME env变量。 如果我在CMD控制台中命令python,它将继续使用python3.4。 我删除了python3.4。 每当我在CMD控制台中命令python时,它就会开始显示如下错误。
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ImportError: No module named 'encodings'
我搜寻以找出我的问题。 解决方案很简单。 安装python3.5时,可以自定义安装并在“高级选项”中选中“将Python添加到环境变量”。
我只是在这里离开,以防有人遇到类似的问题来这里,以便他们不会浪费很多宝贵的时间来弄清楚。
Jayground answered 2019-10-03T08:11:25Z
6 votes
我在Windows7下也面临同样的问题。 错误消息如下所示:
Fatal Python error: Py_Initialize: unable to load the file system codec
ModuleNotFoundError: No module named 'encodings'
Current thread 0x000011f4 (most recent call first):
我已经安装了python 2.7(现在已卸载),并且在安装python 3.6时选中了“将Python添加到高级选项中的环境变量”。 结果表明,环境变量“ PYTHONHOME”和“ PYTHONPATH”仍然是python2.7。
最后,我通过将“ PYTHONHOME”修改为python3.6安装路径并删除了变量“ PYTHONPATH”来解决了该问题。
Li Qiao answered 2019-10-03T08:12:10Z
5 votes
在迁移到Ubuntu 17.10的过程中出现了此错误,这解决了问题:
sudo dpkg-reconfigure python3
也许您必须关闭会话并重新连接。
numahell answered 2019-10-03T08:12:43Z
4 votes
对于Windows 7上的相同问题
如果您的环境变量/系统变量设置不正确,您将看到这样的错误:
Fatal Python error: Py_Initialize: unable to load the file system codec
ImportError: No module named 'encodings'
Current thread 0x00001db4 (most recent call first):
解决这个问题非常简单:
当您下载Python3.x版本并运行.exe文件时,它为您提供了一个自定义系统中要安装Python位置的选项。 例如,我选择以下位置:C:\ Program Files \ Python36
然后打开系统属性,然后转到“高级”选项卡(或者您可以简单地做到这一点:转到“开始”>“搜索”环境变量”>单击“编辑系统环境变量”。)在“高级”选项卡下,查找“ 环境变量”,然后单击它。 将会弹出另一个名为“环境变量”的窗口。
现在,确保您的用户变量具有“路径变量”中列出的正确的Python路径。 在这里的示例中,您应该看到C:\ Program Files \ Python36。 如果在此处找不到它,则通过选择“路径变量”字段并单击“编辑”来添加它。
最后一步是在同一窗口中的“系统变量”下再次检查PYTHONHOME和PYTHONPATH字段。 您应该看到与上述相同的路径。 如果没有,也添加它。
然后单击“确定”并返回到CMD终端,然后尝试检查python。 现在应解决此问题。 它为我工作。
dotpy_novice answered 2019-10-03T08:13:55Z
0 votes
查看/lib/python3.5,您会看到指向python库的断开链接。 将其重新创建到工作目录。
下一个错误-
./script/bin/pip3
Failed to import the site module
Traceback (most recent call last):
File "/home/script/script/lib/python3.5/site.py", line 703, in
main()
File "/home/script/script/lib/python3.5/site.py", line 683, in main
paths_in_sys = addsitepackages(paths_in_sys)
File "/home/script/script/lib/python3.5/site.py", line 282, in addsitepackages
addsitedir(sitedir, known_paths)
File "/home/script/script/lib/python3.5/site.py", line 204, in addsitedir
addpackage(sitedir, name, known_paths)
File "/home/script/script/lib/python3.5/site.py", line 173, in addpackage
exec(line)
File "", line 1, in
File "/home/script/script/lib/python3.5/types.py", line 166, in
import functools as _functools
File "/home/script/script/lib/python3.5/functools.py", line 23, in
from weakref import WeakKeyDictionary
File "/home/script/script/lib/python3.5/weakref.py", line 12, in
from _weakref import (
ImportError: cannot import name '_remove_dead_weakref'
像这样固定-[https://askubuntu.com/questions/907035/importerror-cannot-import-name-remove-dead-weakref]
cd my-virtualenv-directory
virtualenv . --system-site-packages
IvanTheFirst answered 2019-10-03T08:14:45Z
-3 votes
我也可以解决这个问题。 PYTHONPATH和PYTHONHOME是原因。
在终端上运行
touch ~/.bash_profile
open ~/.bash_profile
然后删除此文件的所有无用部分,然后保存。我不知道这样做是多么推荐!
Pber6154 answered 2019-10-03T08:15:22Z