ImportError:没有名为“编码”的模块

问题:ImportError:没有名为“编码”的模块

我最近重新安装了ubuntu并升级到16.04,无法使用python:

$ python manage.py runserver
Could not find platform independent libraries <prefix>
Could not find platform dependent libraries <exec_prefix>
Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]
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 <prefix>
Could not find platform dependent libraries <exec_prefix>
Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ImportError: No module named 'encodings'
Aborted

甚至这个建议也不再起作用:

unset PYTHONHOME
unset PYTHONPATH

每一次我用一种方式修复它,它都会再次出现。有几个答案有助于暂时修复它,但不是永久性的。我已经重新安装了python和python3几次。我可以从这里做什么?谢谢

I recently reinstalled ubuntu and did upgrade to 16.04 and cannot use python:

$ python manage.py runserver
Could not find platform independent libraries <prefix>
Could not find platform dependent libraries <exec_prefix>
Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ImportError: No module named 'encodings'
Aborted

At this point, python itself doesn’t work

$ python
Could not find platform independent libraries <prefix>
Could not find platform dependent libraries <exec_prefix>
Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ImportError: No module named 'encodings'
Aborted

Even this suggestion is no longer working:

unset PYTHONHOME
unset PYTHONPATH

Every every I fix it one way, it comes back again. Several answers help to fix it temporarily, but not for good. I’ve reinstalled python and python3 several times. What can I do from here? Thank you


回答 0

对于Python-3,请尝试删除虚拟环境文件。并重新设置它。

rm -rf venv
virtualenv -p /usr/bin/python3 venv/
source venv/bin/activate
pip install -r requirements.txt

https://wiki.ubuntu.com/XenialXerus/ReleaseNotes#Python_3 编辑

For Python-3 try removing virtual environment files. And resetting it up.

rm -rf venv
virtualenv -p /usr/bin/python3 venv/
source venv/bin/activate
pip install -r requirements.txt

https://wiki.ubuntu.com/XenialXerus/ReleaseNotes#Python_3 edit fo


回答 1

对于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添加到环境变量”

我只是在这里离开,以防有人遇到类似的问题来这里,以便他们不会浪费很多宝贵的时间来弄清楚。

For Windows10 User.

I was using python3.4 on Windows10. I installed python3.5. I couldn’t find PYTHONPATH, PYTHONHOME env variable. If I command python in CMD console, It kept using python3.4. I deleted python3.4. Whenever I command python in CMD console, it starts showing an error like below.

Fatal Python error: Py_Initialize: Unable to get the locale encoding
ImportError: No module named 'encodings'

I searched to figure out my problem. Solution was simple. When you install python3.5, you can custom install and check Add Python to environment variables in Advanced Options.

I just leave here for case that someone have similar issues visit here so that they don’t waste their precious time much to figure out.


回答 2

我在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 ” 来解决了该问题。

I was facing the same problem under Windows7. The error message looks like that:

Fatal Python error: Py_Initialize: unable to load the file system codec
ModuleNotFoundError: No module named 'encodings'

Current thread 0x000011f4 (most recent call first):

I have installed python 2.7(uninstalled now), and I checked “Add Python to environment variables in Advanced Options” while installing python 3.6. It comes out that the Environment Variable “PYTHONHOME” and “PYTHONPATH” is still python2.7.

Finally I solved it by modify “PYTHONHOME” to python3.6 install path and remove variable “PYTHONPATH“.


回答 3

对于Windows7上的相同问题

如果您的环境变量/系统变量设置不正确,您将看到这样的错误:

Fatal Python error: Py_Initialize: unable to load the file system codec
ImportError: No module named 'encodings'

Current thread 0x00001db4 (most recent call first):

解决这个问题非常简单:

  1. 当您下载Python3.x版本并运行.exe文件时,它为您提供了一个自定义系统中要安装Python位置的选项。例如,我选择了以下位置:C:\ Program Files \ Python36

  2. 然后打开系统属性,然后转到“ 高级 ”选项卡(或者您可以简单地做到这一点:转到“开始”>“搜索” 环境变量 ”>单击“编辑系统环境变量”。)在“高级”选项卡下,查找单击“环境变量”,然后单击它。将会弹出另一个名为“环境变量”的窗口。

  3. 现在,确保您的用户变量具有“路径变量”中列出的正确的Python路径。在这里的示例中,您应该看到C:\ Program Files \ Python36。如果在此处找不到它,则通过选择“路径变量”字段并单击“编辑”来添加它。

  4. 最后一步是在同一窗口中的系统变量下仔细检查PYTHONHOMEPYTHONPATH字段。您应该看到与上述相同的路径。如果没有也添加它。

然后单击“确定”并返回到CMD终端,然后尝试检查python。现在应解决此问题。它为我工作。

For the same issue on Windows7

You will see an error like this if your environment variables/ system variables are incorrectly set:

Fatal Python error: Py_Initialize: unable to load the file system codec
ImportError: No module named 'encodings'

Current thread 0x00001db4 (most recent call first):

Fixing this is really simple:

  1. When you download Python3.x version, and run the .exe file, it gives you an option to customize where in your system you want to install Python. For example, I chose this location: C:\Program Files\Python36

  2. Then open system properties and go to “Advanced” tab (Or you can simply do this: Go to Start > Search for “environment variables” > Click on “Edit the system environment variables”.) Under the “Advanced” tab, look for “Environment Variables” and click it. Another window with name “Environment Variables” will pop up.

  3. Now make sure your user variables have the correct Python path listed in “Path Variable”. In my example here, you should see C:\Program Files\Python36. If you do not find it there, add it, by selecting Path Variable field and clicking Edit.

  4. Last step is to double-check PYTHONHOME and PYTHONPATH fields under System Variables in the same window. You should see the same path as described above. If not add it there too.

Then click OK and go back to CMD terminal, and try checking for python. The issue should now be resolved. It worked for me.


回答 4

在迁移到Ubuntu 17.10的过程中出现了此错误,这解决了问题:

sudo dpkg-reconfigure python3

也许您必须关闭会话并重新连接。

I had this error during migration to Ubuntu 17.10, and this solved the problem :

sudo dpkg-reconfigure python3

Maybe you will have to close your session and reconnect.


回答 5

查看/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 <module>
    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 "<string>", line 1, in <module>
  File "/home/script/script/lib/python3.5/types.py", line 166, in <module>
    import functools as _functools
  File "/home/script/script/lib/python3.5/functools.py", line 23, in <module>
    from weakref import WeakKeyDictionary
  File "/home/script/script/lib/python3.5/weakref.py", line 12, in <module>
    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

Look at /lib/python3.5 and you will see broken links to python libraries. Recreate it to working directory.

Next error –

./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 <module>
    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 "<string>", line 1, in <module>
  File "/home/script/script/lib/python3.5/types.py", line 166, in <module>
    import functools as _functools
  File "/home/script/script/lib/python3.5/functools.py", line 23, in <module>
    from weakref import WeakKeyDictionary
  File "/home/script/script/lib/python3.5/weakref.py", line 12, in <module>
    from _weakref import (
ImportError: cannot import name '_remove_dead_weakref'

fixed like this – https://askubuntu.com/questions/907035/importerror-cannot-import-name-remove-dead-weakref

cd my-virtualenv-directory
virtualenv . --system-site-packages

回答 6

更新到macOS Catalina后,我遇到了此问题“ ModuleNotFoundError:没有名为’encodings的模块”。

我在系统中安装了多个版本的Python。

从macOS系统中删除所有python版本(2.7和3.7.4)并重新安装最新的python 3.8对我来说很有效。

要从macOS中删除python,我已遵循此处的说明如何在Mac OS X 10.6.4上卸载Python 2.7?

上面的链接适用于python 2.7,但是您也可以将其用于3.7。

I was facing this issue “ModuleNotFoundError: No module named ‘encodings” after updating to macOS Catalina.

I was having multiple versions of Python installed in my system.

Removing all the python versions(2.7 and 3.7.4) from macOS system and reinstalling the latest python 3.8 worked for me.

To remove a python from macOS, I’ve followed the instructions from here How to uninstall Python 2.7 on a Mac OS X 10.6.4?

The above link is for python 2.7 and but you can use the same for 3.7 also.


回答 7

我有一个类似的问题。我在计算机上同时安装了anaconda和python,而我的python依赖项则来自Anaconda目录。当我卸载Anaconda时,此错误开始弹出。我加了,PYTHONPATH但还是没去。我检查了python -version一下,发现它仍在沿用Python之路。我不得不手动删除Anaconda3目录,然后python开始从接收依赖PYTHONPATH
问题已解决!

I had a similar issue. I had both anaconda and python installed on my computer and my python dependencies were from the Anaconda directory. When I uninstalled Anaconda, this error started popping. I added PYTHONPATH but it still didn’t go. I checked with python -version and go to know that it was still taking the anaconda path. I had to manually delete Anaconda3 directory and after that python started taking dependencies from PYTHONPATH.
Issue Solved!


回答 8

使用时将我的mac更新到macOS Catalina时遇到了相同的问题pipenv。Pipenv virtualenv为您创建并管理一个,因此@ Anoop-Malav的早期建议是相同的,只是使用pipenv根据当前目录删除虚拟环境并重置它:

pipenv --rm
pipenv shell  # recreate a virtual env with your current Pipfile

Had the same problem when updating my mac to macOS Catalina, while using pipenv. Pipenv creates and manages a virtualenv for you, so the earlier suggestion from @Anoop-Malav is the same, just using pipenv to remove the virtual environment based on the current dir and reset it:

pipenv --rm
pipenv shell  # recreate a virtual env with your current Pipfile

回答 9

在我的情况下,只需更改anaconda文件夹的权限即可:

sudo chmod -R u=rwx,g=rx,o=rx /path/to/anaconda   

In my case just changing the permissions of anaconda folder worked:

sudo chmod -R u=rwx,g=rx,o=rx /path/to/anaconda   

回答 10

因为这是google的第一个结果,所以我只想向其他有监狱问题的人添加以下信息:

Could not find platform independent libraries <prefix>
Could not find platform dependent libraries <exec_prefix>
Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ModuleNotFoundError: No module named 'encodings'

Current thread 0x00007f079b16d740 (most recent call first):
Aborted (core dumped)

尝试将python导入监狱时,您都需要将依赖项和/usr/lib/pythonX.Y链接到[JAIL] / usr / lib /。希望这可以帮助。

Because this is the first result in google I just want to add the following information for anybody else having problems with jails:

Could not find platform independent libraries <prefix>
Could not find platform dependent libraries <exec_prefix>
Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ModuleNotFoundError: No module named 'encodings'

Current thread 0x00007f079b16d740 (most recent call first):
Aborted (core dumped)

When attempting to import python into your jail you both need to link the dependencies and /usr/lib/pythonX.Y to [JAIL]/usr/lib/. Hope this helps.


回答 11

只需转到File-> Settings->在Project选项卡下选择Project Interpreter->单击小齿轮图标-> Add-> System Interpreter->在下拉菜单中选择所需的python版本

这似乎对我有用

Just go to File -> Settings -> select Project Interpreter under Project tab -> click on the small gear icon -> Add -> System Interpreter -> select the python version you want in the drop down menu

this seemed to work for me


回答 12

我也可以解决这个问题。PYTHONPATH和PYTHONHOME是原因。

在终端上运行

   touch ~/.bash_profile
   open ~/.bash_profile

然后删除此文件的所有无用部分,然后保存。我不知道这样做是多么推荐!

I could also fix this. PYTHONPATH and PYTHONHOME were in cause.

run this in a terminal

   touch ~/.bash_profile
   open ~/.bash_profile

and then delete all useless parts of this file, and save. I do not know how recommended it is to do that !