首页 > 解决方案 > 刚刚在 Mac 上安装了 Python,但出现“ModuleNotFoundError: No module named 'encodings'”错误

问题描述

我想在我的 Mac High Sierra 系统上使用 Python 3。我使用以下方法安装了 brew ...

localhost:tmp davea$ brew reinstall python3
==> Reinstalling python 
==> Downloading https://homebrew.bintray.com/bottles/python-3.7.6_1.high_sierra.bottle.tar.gz
Already downloaded: /Users/davea/Library/Caches/Homebrew/downloads/3d94ccb6613548e55aed20c7ee59f0e0b7fb045a5d9c8885aa3504ea31f8ab0e--python-3.7.6_1.high_sierra.bottle.tar.gz
==> Pouring python-3.7.6_1.high_sierra.bottle.tar.gz
==> /usr/local/Cellar/python/3.7.6_1/bin/python3 -s setup.py --no-user-cfg install --force --verbose --install-scripts=/usr/local/Cellar/pyt
==> /usr/local/Cellar/python/3.7.6_1/bin/python3 -s setup.py --no-user-cfg install --force --verbose --install-scripts=/usr/local/Cellar/pyt
==> /usr/local/Cellar/python/3.7.6_1/bin/python3 -s setup.py --no-user-cfg install --force --verbose --install-scripts=/usr/local/Cellar/pyt
==> Caveats
Python has been installed as
  /usr/local/bin/python3

Unversioned symlinks `python`, `python-config`, `pip` etc. pointing to
`python3`, `python3-config`, `pip3` etc., respectively, have been installed into
  /usr/local/opt/python/libexec/bin

You can install Python packages with
  pip3 install <package>
They will install into the site-package directory
  /usr/local/lib/python3.7/site-packages

See: https://docs.brew.sh/Homebrew-and-Python
==> Summary
  /usr/local/Cellar/python/3.7.6_1: 3,977 files, 60.8MB

但是,当我尝试运行一个简单的命令时,我收到“ModuleNotFoundError: No module named 'encodings'”错误。我只是想解析一些 JSON。我还需要做什么才能正确安装 Python?

localhost:tmp davea$ echo $json
{"id": "abc", "name": "dave"}
localhost:tmp davea$ echo $json | python3 -c 'import json,sys;obj=json.load(sys.stdin);print(obj["id"])'
Fatal Python error: initfsencoding: unable to load the file system codec
ModuleNotFoundError: No module named 'encodings'

Current thread 0x00007fff9f066380 (most recent call first):
Abort trap: 6
localhost:tmp davea$ echo $json | python -c 'import json,sys;obj=json.load(sys.stdin);print(obj["id"])'
Fatal Python error: initfsencoding: unable to load the file system codec
ModuleNotFoundError: No module named 'encodings'

Current thread 0x00007fff9f066380 (most recent call first):
Abort trap: 6:

标签: pythonpython-3.xmacoshomebrew

解决方案


推荐阅读