Fatal Python error: init_fs_encoding: failed to get the Python codec of the filesystem encoding

  • If I export PYTHONHOME=/usr/bin, I got the following ERRORs.
➜  build git:(master) ✗ echo $PYTHONHOME
/usr/bin
➜  build git:(master) ✗ echo $PYTHONPATH
/home/longervision/.local/lib/python3.8/site-packages:/usr/lib/python3.8/site-packages:/usr/local/lib/python3.8/site-packages
➜  build git:(master) ✗ pip show setuptools
Python path configuration:
  PYTHONHOME = '/usr/bin'
  PYTHONPATH = '/home/longervision/.local/lib/python3.8/site-packages:/usr/lib/python3.8/site-packages:/usr/local/lib/python3.8/site-packages'
  program name = '/usr/bin/python'
  isolated = 0
  environment = 1
  user site = 1
  import site = 1
  sys._base_executable = '/usr/bin/python'
  sys.base_prefix = '/usr/bin'
  sys.base_exec_prefix = '/usr/bin'
  sys.executable = '/usr/bin/python'
  sys.prefix = '/usr/bin'
  sys.exec_prefix = '/usr/bin'
  sys.path = [
    '/home/longervision/.local/lib/python3.8/site-packages',
    '/usr/lib/python3.8/site-packages',
    '/usr/local/lib/python3.8/site-packages',
    '/usr/bin/lib/python38.zip',
    '/usr/bin/lib/python3.8',
    '/usr/bin/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
ModuleNotFoundError: No module named 'encodings'

Current thread 0x00007f12b17cbd80 (most recent call first):
<no Python frame>
  • If I export PYTHONHOME=""
➜  build git:(master) ✗ export PYTHONHOME=""
➜  build git:(master) ✗ pip show setuptools
Name: setuptools
Version: 45.2.0
Summary: Easily download, build, install, upgrade, and uninstall Python packages
Home-page: https://github.com/pypa/setuptools
Author: Python Packaging Authority
Author-email: distutils-sig@python.org
License: UNKNOWN
Location: /home/longervision/.local/lib/python3.8/site-packages
Requires: 
Required-by: zope.interface, wadllib, twine, tensorboard, Sphinx, protobuf, Markdown, lazr.restfulclient, launchpadlib, kiwisolver, jsonschema, ipython, google-auth
➜  build git:(master) ✗ 

That seems to be a Python bug…

Hello and welcome :slight_smile:

This is not a Python bug, this is a symptom of setting PYTHONHOME and/or PYTHONPATH when they’re not needed. In nearly all cases you don’t need to set either of them; in the case of PYTHONHOME it’s almost always a mistake to set.

Is there a particular reason you’re trying to set these variables? Are you following a guide that suggests it?

Hi, Zachary:

Good to know this…
I actually tried to build librealsense, but obtained the following:

 CMake Error at wrappers/python/third_party/pybind11/tools/FindPythonLibsNew.cmake:95 (message):
   Python config failure:

   Fatal Python error: Py_Initialize: Unable to get the locale encoding

   ModuleNotFoundError: No module named 'encodings'



   Current thread 0x00007f84ac23c340 (most recent call first):

 Call Stack (most recent call first):
   wrappers/python/third_party/pybind11/tools/pybind11Tools.cmake:16 (find_package)
   wrappers/python/third_party/pybind11/CMakeLists.txt:33 (include)

Please refer to https://github.com/IntelRealSense/librealsense/issues/5765

I then followed https://stackoverflow.com/questions/38132755, and set both variables PHTHONHOME and PYTHONPATH, but obtained the above symptom.

My installed pybind11:

➜  build git:(master) ✗ pip show pybind11
Name: pybind11
Version: 2.4.3
Summary: Seamless operability between C++11 and Python
Home-page: https://github.com/pybind/pybind11
Author: Wenzel Jakob
Author-email: wenzel.jakob@epfl.ch
License: BSD
Location: /home/longervision/.local/lib/python3.8/site-packages
Requires: 
Required-by: 

Does it have something to do with the 3rd-party pybind11?

Cheers
Pei