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
➜  build git:(master) ✗ echo $PYTHONPATH
➜  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 = [
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
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

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


I’m actually not using the default python from the repository, but built and installed a newer python manually. Which causes my python is not in the default directory but under $HOME/.local . In such, I need to set up my own PYTHONPATH .

After I unset PYTHONHOME, I still met some further errors which seem relate to Python:

Traceback (most recent call last):                
  File "/usr/lib/cnf-update-db", line 8, in <module>
    from CommandNotFound.db.creator import DbCreator
ModuleNotFoundError: No module named 'CommandNotFound'
Reading package lists... Done
E: Problem executing scripts APT::Update::Post-Invoke-Success 'if /usr/bin/test -w /var/lib/command-not-found/ -a -e /usr/lib/cnf-update-db; then /usr/lib/cnf-update-db > /dev/null; fi'
E: Sub-process returned an error code

Any further suggestions?