1.报错:
Fatal Python error: init_fs_encoding: failed to get the Python codec of the filesystem encoding
Python runtime state: core initialized
ModuleNotFoundError: No module named 'encodings'
python2.x是没有encodings模块的。
python3.x才开始引入该模块。
奇怪的是我使用python3运行的程序,但是还是找不到'encodings'模块!
2.查看当前python库中是否有encodings模块
<1>.查看当前python版本:pip
# pip --versionpip 22.0.2 from /usr/lib/python3/dist-packages/pip (python 3.10)
说明pip默认是python3.10,并且所有的python库在/usr/lib/python3/dist-packages/pip路径。
# cd /usr/lib/python3/dist-packages/pip# cat /usr/lib/python3/dist-packages/pip/_internal/utils/encoding.pyimport codecsimport localeimport reimport sysfrom typing import List, TupleBOMS: List[Tuple[bytes, str]] = [ (codecs.BOM_UTF8, "utf-8"), (codecs.BOM_UTF16, "utf-16"), (codecs.BOM_UTF16_BE, "utf-16-be"), (codecs.BOM_UTF16_LE, "utf-16-le"), (codecs.BOM_UTF32, "utf-32"), (codecs.BOM_UTF32_BE, "utf-32-be"), (codecs.BOM_UTF32_LE, "utf-32-le"),]ENCODING_RE = re.compile(br"coding[:=]\s*([-\w.]+)")def auto_decode(data: bytes) -> str: """Check a bytes string for a BOM to correctly detect the encoding Fallback to locale.getpreferredencoding(False) like open() on Python3""" for bom, encoding in BOMS: if data.startswith(bom): return data[len(bom) :].decode(encoding) # Lets check the first two lines as in PEP263 for line in data.split(b"\n")[:2]: if line[0:1] == b"#" and ENCODING_RE.search(line): result = ENCODING_RE.search(line) assert result is not None encoding = result.groups()[0].decode("ascii") return data.decode(encoding) return data.decode( locale.getpreferredencoding(False) or sys.getdefaultencoding(), )
在/usr/lib/python3/dist-packages/pip/_internal/utils/encoding.py目录下找到encodings模块。说明没问题,是有encodings模块的。
<2>.查看当前python3版本:pip3
# pip3 --versionpip 22.0.4 from /usr/local/lib/python3.9/site-packages/pip (python 3.9)
也是有/usr/local/lib/python3.9/site-packages/pip/_internal/utils/encoding.py的,说明没问题,是有encodings模块的。
说明pip和pip3默认软链接的都是python3.x版本。
2.写个示例验证下
<1>.引用encoding模块
import encodingsimport os# 定义一个字符串string = "Hello, 你好!"# 将字符串编码为 UTF-8encoded_string = string.encode("utf-8")# 将编码后的字符串解码为utf-8decoded_string = encoded_string.decode("utf-8")# 打印结果print("编码后:" + str(encoded_string))print("解码后:" + decoded_string)
依然没有问题,正常打印。
<2>.打印所有环境变量
import encodingsimport os# 遍历所有环境变量并打印for key, value in os.environ.items(): print(key + ': ' + value)
依然没问题,到此一切都正常,但是报错依然在。就先到这,先挂起吧!
来源地址:https://blog.csdn.net/u010164190/article/details/129957875