为什么我的虚拟环境没有被 uwsgi 激活?

为什么我的虚拟环境没有被 uwsgi 激活?

在全新安装的 Debian Bullseye 上,我无法使用该uwsgi命令。它无法加载请求的虚拟环境。

这是失败的命令:

uwsgi --plugins http,python3 --wsgi-file wsgi.py --http :9090 --virtualenv /opt/example/virtualenv

的内容wsgi.py只是Django默认生成的文件,并添加了一条print()语句:

import os, sys
print(sys.path)
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'example.settings')
application = get_wsgi_application()

这是该命令的输出uwsgi

$ uwsgi --plugins http,python3 --wsgi-file wsgi.py --http :9090 --virtualenv /opt/example/virtualenv
*** Starting uWSGI 2.0.19.1-debian (64bit) on [Tue Nov 24 16:33:40 2020] ***
compiled with version: 10.2.0 on 23 November 2020 03:27:28
os: Linux-5.9.0-2-cloud-amd64 #1 SMP Debian 5.9.6-1 (2020-11-08)
nodename: exodus
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 1
current working directory: /opt/example
detected binary path: /usr/bin/uwsgi-core
*** WARNING: you are running uWSGI without its master process manager ***
your processes number limit is 3868
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uWSGI http bound on :9090 fd 4
spawned uWSGI http 1 (pid: 33793)
uwsgi socket 0 bound to TCP address 127.0.0.1:41467 (port auto-assigned) fd 3
Python version: 3.9.0+ (default, Oct 19 2020, 09:51:18)  [GCC 10.2.0]
PEP 405 virtualenv detected: /opt/example/virtualenv
Set PythonHome to /opt/example/virtualenv
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x562115d61f30
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 72920 bytes (71 KB) for 1 cores
*** Operational MODE: single process ***
['.', '', '/usr/lib/python39.zip', '/usr/lib/python3.9', '/usr/lib/python3.9/lib-dynload']
Traceback (most recent call last):
  File "wsgi.py", line 12, in <module>
    from django.core.wsgi import get_wsgi_application
ModuleNotFoundError: No module named 'django'
unable to load app 0 (mountpoint='') (callable not found or import error)
*** no app loaded. going in full dynamic mode ***
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI worker 1 (and the only) (pid: 33792, cores: 1)

如您所见,uwsgi通知我已检测到虚拟环境并具有“ Set PythonHome to /opt/example/virtualenv”,但语句的输出print()显示事实并非如此。

如果我手动激活虚拟环境,我可以确认 Django 已正确安装在虚拟环境中:

$ . /opt/example/virtualenv/bin/activate
$ python
Python 3.8.6 (default, Sep 25 2020, 09:36:53) 
[GCC 10.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> django.__file__
'/opt/example/virtualenv/lib/python3.8/site-packages/django/__init__.py'
>>>

出了什么问题?我该如何修复它?

答案1

我以前见过类似的东西,我所做的是uwsgi通过安装pip,所以当你运行它时,uwsgi已经“在”虚拟环境因此被激活。

相关内容