0

当 virtualenv 路径与项目主目录不同时,我从 uwsgi 收到 ModuleNotFoundError: No module named 'encodings' 错误。

环境:

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

错误场景:

virtualenv:/home/venvs/py39 项目主页:/opt/local/apps/myproject

但是,当项目主页在虚拟环境中时,不会出现错误。virtualenv:/home/venvs/py39 项目主页:/home/venvs/py39/apps/myproject

失败的配置:

[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

错误信息:

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'

请注意,PYTHONHOME(和 PYTHONPATH)是由 uswgi 使用来自 conf 的信息设置的

工作配置即。当项目主目录是 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

上面的 conf 是成功的,这条消息在日志中:

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

就好像项目路径在第一种情况下没有添加到 python 路径(由 uwsgi 完成),但在第二种情况下有效。

有没有其他人遇到过这个?

4

1 回答 1

0

我从 Debian 10 升级到 11,virtualenv 仍然是 python 3.7,所以我只是删除了 virtualenv 并用 python 3.9 重新创建它,它又可以工作了。

于 2021-11-05T20:41:12.583 回答