Fatal Python error: init_fs_encoding: failed to get the Python codec of the filesystem encoding
发布时间:2024-02-12
摘要: 问题 按python3 回车 无法使用 root@autodl-container-c6d211bb3c-2ce423ea:/usr/local/python-3.8.1/bin# python3 Python path configuration: PYTH...

问题

按python3 回车
无法使用

root@autodl-container-c6d211bb3c-2ce423ea:/usr/local/python-3.8.1/bin# python3
Python path configuration:
  PYTHONHOME = '/etc'
  PYTHONPATH = ':/usr/local/python-2.7.9/lib/python2.7:/usr/local/python-3.8.1/lib/python3.8'
  program name = 'python3'
  isolated = 0
  environment = 1
  user site = 1
  import site = 1
  sys._base_executable = '/usr/local/python-3.8.1/bin/python3'
  sys.base_prefix = '/etc'
  sys.base_exec_prefix = '/etc'
  sys.executable = '/usr/local/python-3.8.1/bin/python3'
  sys.prefix = '/etc'
  sys.exec_prefix = '/etc'
  sys.path = [
    '',
    '/usr/local/python-2.7.9/lib/python2.7',
    '/usr/local/python-3.8.1/lib/python3.8',
    '/etc/lib/python38.zip',
    '/etc/lib/python3.8',
    '/etc/lib/python3.8/lib-dynload',
  ]
Fatal Python error: init_fs_encoding: failed to get the Python codec of the filesystem encoding
Python runtime state: core initialized
  File "/usr/local/python-2.7.9/lib/python2.7/encodings/__init__.py", line 123
    raise CodecRegistryError,\
                            ^
SyntaxError: invalid syntax

Current thread 0x00007f970333e280 (most recent call first):

原因

python3的PYTHONPATH不对
我的PYTHONPATH有两个,
前面一个是python2.7.9的,
它先调用了python2.7.9的,
没调用3.8.1的,
所以报错

解决

清掉原来的PYTHONPATH
设置新的,
赋值为3.8.1的路径

root@autodl-container-c6d211bb3c-2ce423ea:/usr/local/python-3.8.1/bin# unset PYTHONPATH
root@autodl-container-c6d211bb3c-2ce423ea:/usr/local/python-3.8.1/bin# export PYTHONPATH=$PYTHONPATH:/usr/local/python-3.8.1/lib/python3.8
root@autodl-container-c6d211bb3c-2ce423ea:/usr/local/python-3.8.1/bin# python3
Python 3.8.1 (default, Feb  8 2023, 15:33:21) 
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()

缺点是python2.7.9不能用了,
具体表现是输入python回车会报错,
如果要用的话,
要把PYTHONPATH改回2.7.9的