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?

1 Like

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

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?
Cheers
Pei

@zware, I am getting the same error but I don’t have PYTHONHOME or PYTHONPATH set. I am trying to embed python into c++ using pybind11. Here is the simple code:

 #include <pybind11/embed.h>

void main() {
    pybind11::scoped_interpreter guard {};
pybind11::exec("print('hello world')");

}

The file compiles just fine, but when I try to run it in powershell I get the following error message:

PS C:\Users\ben.wolfley\Desktop\Test3\vsstudio\Debug> .\pybind11app.exe
Python path configuration:
PYTHONHOME = (not set)
PYTHONPATH = (not set)
program name = ‘python’
isolated = 0
environment = 1
user site = 1
import site = 1
sys._base_executable = ‘C:\Users\ben.wolfley\Desktop\Test3\vsstudio\Debug\pybind11app.exe’
sys.base_prefix = ‘C:\Users\ben.wolfley\Anaconda3’
sys.base_exec_prefix = ‘C:\Users\ben.wolfley\Anaconda3’
sys.executable = ‘C:\Users\ben.wolfley\Desktop\Test3\vsstudio\Debug\pybind11app.exe’
sys.prefix = ‘C:\Users\ben.wolfley\Anaconda3’
sys.exec_prefix = ‘C:\Users\ben.wolfley\Anaconda3’
sys.path = [
‘C:\Users\ben.wolfley\Anaconda3\python38.zip’,
‘.\DLLs’,
‘.\lib’,
‘C:\Users\ben.wolfley\Desktop\Test3\vsstudio\Debug’,
]
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’

Do you have any idea what is going on?

1 Like

I am also getting same error.

This was the case for me when I set it in my .vimrc file. Thanks for the fix.

Me2, I also got this error. I have not seen any solution for this, are there any?

I am getting same error.Have you solved?

I had the same error and I was able to solve it in the following way: django - init_fs_encoding: failed to get the Python codec of the filesystem encoding - Stack Overflow

I installed swift on windows-10. I got similar error when i was trying to run it on command prompt
([vcvarsall.bat] Environment initialized for: ‘x86’).
I got this error

C:\Windows\System32>swift
Python path configuration:
  PYTHONHOME = 'C:/hostedtoolcache/windows/Python/3.10.2/x64'
  PYTHONPATH = (not set)
  program name = 'python'
  isolated = 0
  environment = 1
  user site = 1
  import site = 1
  sys._base_executable = 'C:\\Library\\Developer\\Toolchains\\unknown-Asserts-development.xctoolchain\\usr\\bin\\lldb.exe'
  sys.base_prefix = 'C:/hostedtoolcache/windows/Python/3.10.2/x64'
  sys.base_exec_prefix = 'C:/hostedtoolcache/windows/Python/3.10.2/x64'
  sys.platlibdir = 'lib'
  sys.executable = 'C:\\Library\\Developer\\Toolchains\\unknown-Asserts-development.xctoolchain\\usr\\bin\\lldb.exe'
  sys.prefix = 'C:/hostedtoolcache/windows/Python/3.10.2/x64'
  sys.exec_prefix = 'C:/hostedtoolcache/windows/Python/3.10.2/x64'
  sys.path = [
    'C:\\Users\\Aarga\\AppData\\Local\\Programs\\Python\\Python310\\python310.zip',
    'C:/hostedtoolcache/windows/Python/3.10.2/x64\\DLLs',
    'C:/hostedtoolcache/windows/Python/3.10.2/x64\\lib',
    'C:\\Library\\Developer\\Toolchains\\unknown-Asserts-development.xctoolchain\\usr\\bin',
  ]
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 0x000040c0 (most recent call first):
  <no Python frame>

Hi,

It is an old topic, but just in case still helps someone that figured out yet…
If your context is the same as @ jiapei100 and mine, which is building your “own” python from cpython source code repository (forked). In my case for my C programs to run, I have setup this configuration:

export PYTHONHOME=/<path-to-your-local-repo>/cpython
export PYTHONPATH=/<path-to-your-local-repo>/cpython/Lib
1 Like

PyScripter and Delphi VCL from embarcadero make this …

python.exe
Could not find platform independent libraries <prefix>
Python path configuration:
  PYTHONHOME = (not set)
  PYTHONPATH = (not set)
  program name = 'python.exe'
  isolated = 0
  environment = 1
  user site = 1
  safe_path = 0
  import site = 1
  is in build tree = 0
  stdlib dir = 'C:\Python311\Lib'
  sys._base_executable = 'C:\\Users\\catafest\\AppData\\Local\\Programs\\Python\\Python311\\python.exe'
  sys.base_prefix = 'C:\\Python311'
  sys.base_exec_prefix = 'C:\\Python311'
  sys.platlibdir = 'DLLs'
  sys.executable = 'C:\\Users\\catafest\\AppData\\Local\\Programs\\Python\\Python311\\python.exe'
  sys.prefix = 'C:\\Python311'
  sys.exec_prefix = 'C:\\Python311'
  sys.path = [
    'C:\\Users\\catafest\\AppData\\Local\\Programs\\Python\\Python311\\python311.zip',
    'C:\\Python311\\DLLs',
    'C:\\Python311\\Lib',
    'C:\\Users\\catafest\\AppData\\Local\\Programs\\Python\\Python311',
  ]
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 0x00000020 (most recent call first):
  <no Python frame>

1 Like