Solving Python uwsgi Error – init_fs_encoding: failed to get the Python codec of the filesystem encoding

Python uwsgi Error – init_fs_encoding: failed to get the Python codec of the filesystem encoding is an error which occurs in Python when we override the virtual environment variables or have a faulty configuration.

In this article I am going to show you why this error is happening and how you can solve this error. I am also going to share with you other possible solutions which have worked for many developers who suffered from this error.

Explaining the Error : Python uwsgi Error – init_fs_encoding: failed to get the Python codec of the filesystem encoding

First of all we need to understand why the error happens at all, the error happens when we override the virtual environment variables or have a faulty configuration.

The error you are having should be very similar to this one, double check in order to avoid confusion.

                                                                       #
Set PythonHome to /home/env3/educ
Python path configuration:
  program name = '/home/env3/educ/bin/python'
.....
  sys.base_prefix = '/home/env3/educ'
  sys.prefix = '/home/env3/educ'
.....
Fatal Python error: init_fs_encoding: failed to get the Python codec of the filesystem encoding
ModuleNotFoundError: No module named 'encodings'
<no Python frame>
                                                                       #

Bellow I present multiple solutions that can help you solve the error.

Solution 1 : unset PYTHONPATH.

The first solution is to unset the PYTHONPATH or the PYTHONHOME. Since both override the virtual enviroment variables on your machine.

For PYTHONPATH you can use the command bellow.

                                                                       #
unset PYTHONPATH
                                                                       #

For PYTHONHOME you can use the command bellow.

                                                                       #
unset PYTHONHOME
                                                                       #

If none of the commands solved your problem you can try the solution bellow.

Solution 2 : fix the permissions for uWSGI.

If the solutions above did not work, you should try fixing the permissions for uWSGI in order to access the virtual environment.

The uwsgi.ini config should look like this

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

The uwsgi.ini config should look like this if the venv was created in the directory of the project.

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

The solutions above should be enough to solve the problem, if you like our effort make sure to consider donating to our Kofi account, there is a red button that you can use if you are feeling generous.

Summing-up

Finally, we are at the end of this article, I hope this article has been helpful, I hope you solved your problem, coding can be hard when you have a lot of confusing errors here and there.

Thank you for reading, keep learning and keep coding, cheers. If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/