0

i'm getting a ModuleNotFoundError: No module named 'encodings' error from uwsgi when the virtualenv path is different from the project home.

Environment:

OS: debian bullseye
uwsgi version: 2.0.19.1-debian
python version: 3.9

Error scenario:

virtualenv: /home/venvs/py39 project home: /opt/local/apps/myproject

However, the error does not appear when the project home is in the virtual env ie. virtualenv: /home/venvs/py39 project home: /home/venvs/py39/apps/myproject

The failing configuration:

[uwsgi]
project-home = /opt/local/apps/MyProject
plugins-dir = /usr/lib/uwsgi/plugins
plugin = python39
pythonpath = %(project-home)
virtualenv = /home/venvs/py39 
master = 1 
chdir = %(project-home)
socket = /var/run/uwsgi/%n.sock
chmod-socket = 666
manage-script-name = True
python-path = %(project-home) 
module = wsgi
callable = app
uid = www-data
gid = www-data
processes = 8
log-date = true

The error message:

Python version: 3.9.2 (default, Feb 28 2021, 17:03:44)  [GCC 10.2.1 20210110]
!!! Python Home is not a directory: /home/venvs/py39 !!!
Set PythonHome to /home/venvs/py39
Python path configuration:
  PYTHONHOME = '/home/venvs/py39'
  PYTHONPATH = (not set)
  program name = '/home/venvs/py39/bin/python'
  isolated = 0
  environment = 1
  user site = 1
  import site = 1
  sys._base_executable = '/home/venvs/py39/bin/python'
  sys.base_prefix = '/home/venvs/py39'
  sys.base_exec_prefix = '/home/venvs/py39'
  sys.platlibdir = 'lib'
  sys.executable = '/home/venvs/py39/bin/python'
  sys.prefix = '/home/venvs/py39'
  sys.exec_prefix = '/home/venvs/py39'
  sys.path = [
    '/home/venvs/py39/lib/python39.zip',
    '/home/venvs/py39/lib/python3.9',
    '/home/venvs/py39/lib/python3.9/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'

Note that PYTHONHOME (and PYTHONPATH) are set by uswgi using info from the conf

Working configuration ie. when project home is a directory inside the venv

[uwsgi]
project-home = /home/venvs/py39/apps/MyProject
plugins-dir = /usr/lib/uwsgi/plugins
plugin = python39
pythonpath = %(project-home)
virtualenv = /home/venvs/py39 
master = 1 
chdir = %(project-home)
socket = /var/run/uwsgi/%n.sock
chmod-socket = 666
manage-script-name = True
python-path = %(project-home) 
module = wsgi
callable = app
uid = www-data
gid = www-data
processes = 8
log-date = true

The above conf is successful and this message is in the logs:

** Operational MODE: preforking ***
added /home/venvs/py39/apps/MyProject to pythonpath.
*** uWSGI is running in multiple interpreter mode ***

It's as if the project path is not added to the python path (done by uwsgi) in the 1st scenario but works in the 2nd one.

Has anyone else come across this ?

wax
  • 23
  • 4

1 Answers1

0

I did an upgrade from Debian 10 to 11, the virtualenv was still python 3.7, so I just deleted the virtualenv and recreated it with python 3.9 and it was working again.

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • Hi, sorry for time taken to get back. The virtualenv is not the issue as it's newly created (the py39 path is a hint) so missing packages are reinstalled. The problem occurs when the "project-home" is *not* a subdirectory within the *virtualenv directory* (haven't tested vice versa) so eg. not working -> *project-home* is "/a/b/ph" and *venv diretcory* is "/c/d/vd". Working, in my case, is -> *project-home* is "/c/d/vd/my-projects/ph" ie. a subdirectory within the *virtualenv* directory. – wax Dec 29 '21 at 18:42