python - uWSGI找不到所有Python安装中都存在的模块

标签 python django uwsgi python-3.6

我试图在运行 Raspbian GNU/Linux 9(stretch)的 Raspberry Pi 上通过 uWSGI 运行 Django 应用程序,它附带发行版中的 Python 3.5,我编译并安装了 Python 3.6 作为替代方案。这些是我当前的 Python 安装:

  • Python 2:/usr/bin/python
  • Python 3.5:/usr/bin/python3.5
  • Python 3.6:/usr/local/bin/python3.6

我还为我的应用程序创建了一个基于 python3.6 的 virtualenv,位于 ~/.virtualenvs。问题是该服务提示无法获取区域设置编码:

Fatal Python error: Py_Initialize: Unable to get the locale encoding

由于缺少模块:

ImportError: No module named 'encodings'

但是,如果我启动 virtualenv 并发出:

import encodings

它工作得很好:

>>> import encodings
>>> encodings
<module 'encodings' from '/home/ariel/.virtualenvs/you2ogg/lib/python3.6/encodings/__init__.py'>

这就是我的 service.ini 文件的样子:

[uwsgi]

plugins = python3

chdir = /srv/user/app/
module = app.wsgi
home = /home/user/.virtualenvs/app/

master = true

processes = 10

socket = /srv/user/app/app.sock
chmod-socket = 666
vacuum = true

uid = 1000
gid = 1000

这就是日志中关于我的服务的内容:

Wed Dec 20 11:29:51 2017 - *** Starting uWSGI 2.0.14-debian (32bit) on [Wed Dec 20 11:29:49 2017] ***
Wed Dec 20 11:29:51 2017 - compiled with version: 6.2.1 20161124 on 18 December 2016 15:05:38
Wed Dec 20 11:29:51 2017 - os: Linux-4.9.59-v7+ #1047 SMP Sun Oct 29 12:19:23 GMT 2017
Wed Dec 20 11:29:51 2017 - nodename: rpi3
Wed Dec 20 11:29:51 2017 - machine: armv7l
Wed Dec 20 11:29:51 2017 - clock source: unix
Wed Dec 20 11:29:51 2017 - pcre jit disabled
Wed Dec 20 11:29:51 2017 - detected number of CPU cores: 4
Wed Dec 20 11:29:51 2017 - current working directory: /
Wed Dec 20 11:29:51 2017 - writing pidfile to /run/uwsgi/app/you2ogg/pid
Wed Dec 20 11:29:51 2017 - detected binary path: /usr/bin/uwsgi-core
Wed Dec 20 11:29:51 2017 - setgid() to 1000
Wed Dec 20 11:29:51 2017 - setuid() to 1000
Wed Dec 20 11:29:51 2017 - chdir() to /srv/ariel/you2ogg/
Wed Dec 20 11:29:51 2017 - your processes number limit is 7346
Wed Dec 20 11:29:51 2017 - your memory page size is 4096 bytes
Wed Dec 20 11:29:51 2017 - detected max file descriptor number: 1024
Wed Dec 20 11:29:51 2017 - lock engine: pthread robust mutexes
Wed Dec 20 11:29:51 2017 - thunder lock: disabled (you can enable it with --thunder-lock)
Wed Dec 20 11:29:51 2017 - uwsgi socket 0 bound to UNIX address /run/uwsgi/app/you2ogg/socket fd 3
Wed Dec 20 11:29:51 2017 - uwsgi socket 1 bound to UNIX address /srv/ariel/you2ogg/you2ogg.sock fd 5
Wed Dec 20 11:29:51 2017 - Python version: 3.5.3 (default, Jan 19 2017, 14:11:04)  [GCC 6.3.0 20170124]
Wed Dec 20 11:29:51 2017 - Set PythonHome to /home/ariel/.virtualenvs/you2ogg/
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ImportError: No module named 'encodings'

Current thread 0x76f35000 (most recent call first):

但请注意,显然 uWSGI 坚持使用 Python 3.5 来实现谁知道什么,并且根据其他一些问题,设置 PYTHONPATH 解决了这个问题,但是我必须指出,这个应用程序在 Fedora 机器上运行完全正常,其中该 var 也未设置。

这里有什么问题吗?

最佳答案

终于找到问题所在了。事实证明,uWSGI 的 python 插件以某种方式绑定(bind)到了 python 版本,例如:

  • python → python2.7
  • python3 → python3.5

所以我必须按照this guide编译并手动安装我的Python 3.6插件。 :

PYTHON=python3.6 uwsgi --build-plugin "/usr/src/uwsgi/plugins/python python36"
mv python36_plugin.so /usr/lib/uwsgi/plugins/python36_plugin.so
chmod 644 /usr/lib/uwsgi/plugins/python36_plugin.so

现在,uWSGI 服务初始化日志中的 Python 版本显示为 Python 3.6.X,并且应用程序可以正常工作。它在 Fedora 中工作的原因可能是因为从 Fedora 26 开始,它就将 Python 3.6 作为默认的 python3 版本,所以也许该插件是开箱即用的兼容。

关于python - uWSGI找不到所有Python安装中都存在的模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47917291/

相关文章:

python - 如何抑制 Python AttributeError?

python numpy/scipy 查找多维数组中相对变量的计数或频率

python - 使用 django-social-auth 出现导入错误

python - 我应该在哪里对对象和字段进行 Django 验证?

javascript - 如何将 javascript 函数的值传递给 Django View ?

nginx - 如何配置 nginx 和 uwsgi 将调用从路径重定向到子域?

python - NaN 设置为 -1 时缩放功能的影响

python - Django 中的查询集联合

python - 如何让 Uwsgi 与 wagtail (django) 一起工作

python-3.x - 运行 UWSGI 时在 SO 中找不到符号 _PyByteArray_Type