64

I am trying to start my uwsgi server in my virtual environment, but after I added plugin python3 option I get this error every time:

!!! Python Home is not a directory: /home/env3/educ !!!
Set PythonHome to /home/env3/educ
Python path configuration:
  PYTHONHOME = '/home/env3/educ'
  PYTHONPATH = (not set)
  program name = '/home/env3/educ/bin/python'
  isolated = 0
  environment = 1
  user site = 1
  import site = 1
  sys._base_executable = '/home/env3/educ/bin/python'
  sys.base_prefix = '/home/env3/educ'
  sys.base_exec_prefix = '/home/env3/educ'
  sys.executable = '/home/env3/educ/bin/python'
  sys.prefix = '/home/env3/educ'
  sys.exec_prefix = '/home/env3/educ'
  sys.path = [
    '/home/env3/educ/lib/python38.zip',
    '/home/env3/educ/lib/python3.8',
    '/home/env3/educ/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 0x00007efe89db8780 (most recent call first):
<no Python frame>

Also I tried to create new virtual environment using python3 -m venv env and moved project files to it, but still the same error. Here is my uwsgi.ini file:

[uwsgi]

base = /home/env3/educ
projectname = educ

plugins = python3
master = true
virtualenv = /home/env3/%(projectname)
pythonpath = %(base)
env = DJANGO_SETTINGS_MODULE=%(projectname).settings.pro
module = %(projectname).wsgi:application
socket = /tmp/%(projectname).sock
chmod-socket = 666

I use Python 3.8.5

I am trying to use Django + uWSGI + nginx + Postgresql.

3
  • 1
    I'm also having that random error while starting up python. No idea why this occurs Commented Jan 15, 2021 at 17:22
  • 1
    Having this same issue on WSL2, and it's particularly crippling because I can't even use apt without getting this: 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. Incredibly frustrating, and unfortunately unset didn't work in my case. Commented Jul 15, 2021 at 3:24
  • See also github.com/BusKill/buskill-app/issues/… Commented Jun 1, 2023 at 22:56

10 Answers 10

47

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I'm having the same issue, but for me the home and path are already not set. any ideas?
ok so i deleted python from my pc and then reinstalled it and its working again, although I do have to reinstall all my pip libraries now
14

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

5 Comments

Where do you put this? In the terminal or in the file that you are trying to run?
In the terminal, it unsets the PYTHONPATH and PYTHONHOME, "unset" is a terminal command
What commands to use in Windows
for windows you can set PYTHONPATH and PYTHONHOME to empty strings using, "set PYTHONPATH= set PYTHONHOME="
This worked for me so if anyone else comes across this on Linux then try this before looking elsewhere
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 Comment

In my case, I added a /bin at the end of the virtualenv path, and that is what was causing the error.
2

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.

Comments

2

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.

1 Comment

What do you mean by the line where the error occurs? I don't even use the encodings module but this issue is still happening.
0

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

Comments

0

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

Comments

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")

Comments

0

In my case, PYTHONHOME and PYTHONPATH were not set (unset). But still issue kept happening.

My root cause was, I was in a terminal where I had activated a virtual env with a corrupted or mismatching python.

On opening a new terminal and ensuring the virtual env is not activated.,

Fatal Python error: init_fs_encoding: failed to get the Python codec

The above error stopped occurring.

Apart from the uwsgi issue, for those who are facing this issue, in a monorepo with Bazel & Python., venv could be the issue. Try in a new terminal without activating any python.

Comments

-1

In my case, I was running things on an M2 Mini Mac running macOS 15.3.0 and this error happens whenever the Python binary is updated (I use micromamba, i.e. a conda environment).

I need to manually allow incoming connections on local network for some reason. Then the commands no longer fail.

enter image description here

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.