3

I tried to start uwsgi for myproject(mywords_dict):

uwsgi --http :8000  -H /root/mywords_dict/env --chdir mywords_dict -w mywords_dict.wsgi

But received the following error:

Fatal Python error: initfsencoding: Unable to get the locale encoding
ModuleNotFoundError: No module named 'encodings'

I am so confused that uwsgi can work well if I run it after entering virtual env .Python version of 'mywords_dict' is 3.7.1. I have checked that the there is encoding package in mywords_dic/env. And I can run python successfully from there. Does anyone can help?

*** Starting uWSGI 2.0.18 (64bit) on [Fri Aug 30 00:15:16 2019] ***
compiled with version: 4.8.5 20150623 (Red Hat 4.8.5-36) on 29 August 
2019 13:49:55
os: Linux-3.10.0-862.14.4.el7.x86_64 #1 SMP Wed Sep 26 15:12:11 UTC 2018
nodename: VM_0_17_centos
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 1
current working directory: /root
detected binary path: /root/.pyenv/versions/3.7.1/bin/uwsgi
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** 
chdir() to mywords_dict
*** WARNING: you are running uWSGI without its master process manager ***
your processes number limit is 3882
your memory page size is 4096 bytes
detected max file descriptor number: 100001
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uWSGI http bound on :8000 fd 4
spawned uWSGI http 1 (pid: 22645)
uwsgi socket 0 bound to TCP address 127.0.0.1:42301 (port auto-assigned) fd 3
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** 
Python version: 3.7.1 (default, Nov 11 2018, 01:35:46)  [GCC 4.8.5 20150623 (Red Hat 4.8.5-28)]
Set PythonHome to /root/mywords_dict/env
**Fatal Python error: initfsencoding: Unable to get the locale encoding
ModuleNotFoundError: No module named 'encodings'**
Johnson
  • 39
  • 1
  • 1
  • 4
  • `Set PythonHome to /root/mywords_dict/env` — do you actually have a working installation of Python 3 in that directory? Can you run `python` from there and `import encodings`? – 9000 Aug 29 '19 at 16:55
  • 1
    You might need to reinstall python or reset virtual env [This might be a duplicate](https://stackoverflow.com/questions/38132755/importerror-no-module-named-encodings) – ryan yang Aug 29 '19 at 16:55
  • @9000 yes, I can run python from there and import encoding successfully . That is why I am so confused. – Johnson Aug 29 '19 at 17:03
  • 1
    @ryanyang , thank u for sharing. But I can run python and import encodings successfully in virtual env without using uwsgi. It's so wired. – Johnson Aug 29 '19 at 17:08
  • I occasionally experience this. To fix it I have to do `unset PYTHONHOME` before I call python. In fact, I added it to my `.bashrc` file and the problem went away. – SyntaxVoid Aug 29 '19 at 17:39
  • 1
    @SyntaxVoid thanks, but it didn't work for me. – Johnson Aug 30 '19 at 14:51
  • 2
    Hi guys, I finally solved this problem by 'rm -rf env' and rebuild it . And I still don't know why it happend. – Johnson Aug 31 '19 at 08:35

2 Answers2

2

This is due to wrong python virtual environment path. It is defined in you .ini file under home variable.

What you need to do is assign to this variable path pointing to the root of your venv. So if your venv path is /home/user/project/venv, just assign this path to the home variable. Be sure not to include the trailing / (slash) in the venv path (since it is later concatenated to /bin/python and you'll end up with double slash.

Then save your .ini file and try starting uWSGI again.

JovanToroman
  • 605
  • 6
  • 16
  • 1
    I made this exact mistake: I changed the env path in the conf from `/home/vinzee/.pyenv/versions/envname/bin/` to `/home/vinzee/.pyenv/versions/envname` and it worked. – vvvvv Oct 07 '21 at 17:22
-4

I got the same error. Fixed after trying these steps :

  1. Uninstall python(38) as administrator. Checking all the boxes during custom installation, including install for all user and precompiling standard library.
  2. Downgrading python38 to 37

I still dont know what & where the error came from though haha.

Dharman
  • 30,962
  • 25
  • 85
  • 135