[Django]-Fatal Python error: init_fs_encoding: failed to get the Python codec of the filesystem encoding, when trying to start uwsgi

36๐Ÿ‘

โœ…

I see your PYTHONHOME is set to PYTHONHOME = '/home/env3/educ'. Try to check if it is really there.

The solution for me was to remove the PYTHONHOME environment variable.
For you, it can be just that, or setting that variable to another value.
This worked on Windows, and would work on Linux for sure. If someone tries this on Linux, please post a comment here !

A CPython developer confirmed the solution here. :

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.

๐Ÿ‘คChristopher J.

13๐Ÿ‘

I solve this problem by unsetting PYTHONHOME or PYTHONPATH, because they override the virtual environment variables.

In my case this is caused by my adminโ€™s base environment. I would try removing PYTHONHOME and PYTHONPATH variables by doing

unset PYTHONPATH
unset PYTHONHOME
๐Ÿ‘คatakann

7๐Ÿ‘

This message is for those with the same problem who will find this page later.
I couldnโ€™t start uWSGI with my config, and I had the same message in logging files.

init_fs_encoding: failed to get the Python codec of the filesystem
encoding Python runtime state: core initialized ModuleNotFoundError:
No module named โ€˜encodingsโ€™

The problem was uWSGI didnโ€™t have enough permissions to access the virtual environment. When I fixed the permissions, uWSGI started my config.

Denys posted the uwsgi.ini config in his message. I guess the path to the virtual environment is wrong.

base = /home/env3/educ
projectname = educ
virtualenv = /home/env3/%(projectname)

If the virtual environment was created in the project directory, then the path should look like this:

virtualenv = /home/env3/%(projectname)/env

1๐Ÿ‘

The cause of the problem in my case was in the setting of virtualenv. Under linux system, the path to virtualenv starts very likely as /home/(your login username)/env3/, not /home/env3/. So before you try other solutions, you had better make sure the path to your virtualenv is correct.

๐Ÿ‘คxhe

0๐Ÿ‘

In my case, I had to remove home= variable and it fixed the issue.

๐Ÿ‘คrfedorov

0๐Ÿ‘

try using http=127.0.0.1:8000 in uwsgi.ini file.
that solved my problem.

0๐Ÿ‘

in my case I did not unset the pythonhome, pythonpath
Just simply type this in cygwin terminal:

python.exe setup.py install (take note of the ".exe")

๐Ÿ‘คMel Tamps

0๐Ÿ‘

You need to clear the environment variable PYTHONHOME

Solution

Just before the line where this error occurs, add the following

import os
if 'PYTHONHOME' in os.environ:
   del os.environ['PYTHONHOME']

Explanation

I encountered this issue when attempting to execute cinnamon-screensaver-command --lock using the subprocess module inside a python app running as an AppImage (which extracts and executes itself in a temporary squashfs filesystem).

Python path configuration:
  PYTHONHOME = '/tmp/.mount_buskilParXTg/opt/python3.7'
  PYTHONPATH = (not set)
  program name = '/usr/bin/python3'
  isolated = 0
  environment = 1
  user site = 1
  import site = 1
  sys._base_executable = '/usr/bin/python3'
  sys.base_prefix = '/tmp/.mount_buskilParXTg/opt/python3.7'
  sys.base_exec_prefix = '/tmp/.mount_buskilParXTg/opt/python3.7'
  sys.executable = '/usr/bin/python3'
  sys.prefix = '/tmp/.mount_buskilParXTg/opt/python3.7'
  sys.exec_prefix = '/tmp/.mount_buskilParXTg/opt/python3.7'
  sys.path = [
    '/tmp/.mount_buskilParXTg/opt/python3.7/lib/python38.zip',
    '/tmp/.mount_buskilParXTg/opt/python3.7/lib/python3.8',
    '/tmp/.mount_buskilParXTg/opt/python3.7/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'

If you unset the environment variable (with del os.environ['PYTHONHOME']), then the issue goes away.

Leave a comment