![ModuleNotFoundError:没有名为“gunicorn.myapp”的模块](https://linux22.com/image/793948/ModuleNotFoundError%EF%BC%9A%E6%B2%A1%E6%9C%89%E5%90%8D%E4%B8%BA%E2%80%9Cgunicorn.myapp%E2%80%9D%E7%9A%84%E6%A8%A1%E5%9D%97.png)
我正在尝试使两个应用程序可在我的 LAN 服务器(raspberry pi)上访问。
- 实验室应用:这是一个非常简单的应用程序,使用第三方的 flask 制作,使用 sqlite3 数据库,结果是我的应用程序文件夹中的一个文件。它位于路径上
/var/www/lab_app/
,在内部端口 8080 和外部端口 80 上运行。
if __name__ == "__main__":
app.run(host='0.0.0.0', port=8080)
aqi_气象数据:一个更复杂的应用程序,由我使用 django 制作,使用 postgresql 数据库。它位于路径
/var/www/aqi_luftdaten/
并在内部端口 8000 上运行 - 我已通过命令手动启动它:python 管理.py 运行服务器 0.0.0.0:8000
目前,只有以下lab_app
可用且可访问:
它的Web服务器是nginx,其应用程序服务器是uWSGI。
nginx配置如下:
目录中有一个/var/www/lab_app/lab_app_nginx.conf
由符号链接指向的配置文件/etc/nginx/conf.d
ln -s /var/www/lab_app/lab_app_nginx.conf /etc/nginx/conf.d/
其内容是
server {
listen 80;
server_name localhost;
charset utf-8;
client_max_body_size 75M;
location /static {
root /var/www/lab_app/;
}
location / { try_files $uri @labapp; }
location @labapp {
include uwsgi_params;
uwsgi_pass unix:/var/www/lab_app/lab_app_uwsgi.sock;
}
}
通过按照教程,我删除了默认的 nginx sites-enabled 配置文件,因此该目录/etc/nginx/sites-enabled
无效。
目录内/etc/nginx/sites-available/
只有默认文件/etc/nginx/sites-available/default
。
其内容是
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
try_files $uri $uri/ =404;
}
}
uWSGI配置如下:
有一个文件,/var/www/lab_app/lab_app_uwsgi.ini
其内容为:
[uwsgi]
#application's base folder
base = /var/www/lab_app
#python module to import
app = lab_app
module = %(app)
home = %(base)
pythonpath = %(base)
#socket file's location
socket = /var/www/lab_app/%n.sock
#permissions for the socket file
chmod-socket = 666
#the variable that holds a flask application inside the module imported at line #6
callable = app
#location of log files
logto = /var/log/uwsgi/%n.log
我已使用以下命令将其信息加载到 uWSGI
bin/uwsgi --ini /var/www/lab_app/lab_app_uwsgi.ini
然后有一个文件/etc/systemd/system/emperor.uwsgi.service
包含内容
[Unit]
Description=uWSGI Emperor
After=syslog.target
[Service]
ExecStart=/var/www/lab_app/bin/uwsgi --ini /var/www/lab_app/lab_app_uwsgi.ini
# Requires systemd version 211 or newer
RuntimeDirectory=uwsgi
Restart=always
KillSignal=SIGQUIT
Type=notify
StandardError=syslog
NotifyAccess=all
[Install]
WantedBy=multi-user.target
该应用程序在我的 LAN 上运行良好,并且可以通过外部设备浏览器通过我的服务器的 IP 访问。
现在我想aqi_luftdaten
在外部端口 80 上公开应用程序。
所以我要做的第一件事就是为这个应用程序配置 nginx。
因此我创建了一个 nginx 配置文件/var/www/aqi_luftdaten/nginx/aqi_luftdaten_nginx.conf
,其中包含内容
server {
listen 3000;
server_name localhost;
charset utf-8;
client_max_body_size 75M;
location /static/ {
root /var/www/aqi_luftdaten/static/;
}
location / {
proxy_pass http://localhost:8000;
}
}
/etc/nginx/conf.d/
并通过符号将其链接到目录
ln -s /var/www/aqi_luftdaten/nginx/aqi_luftdaten_nginx.conf /etc/nginx/conf.d/
然后重启 nginx
/etc/init.d/nginx restart
我得到了
[ ok ] Restarting nginx (via systemctl): nginx.service.
如果我尝试在其他设备的浏览器中访问 aqi_luftdaten 应用程序,http://<raspberrypi_IP>:3000/
则会
<html><head><title>502 Bad Gateway</title></head>
<body bgcolor="white">
<center><h1>502 Bad Gateway</h1></center>
<hr><center>nginx/1.14.2</center>
</body></html>
我猜这是正确的,因为 uWSGI 尚未配置。
现在我想配置 gunicorn。
有一个文件 /var/www/aqi_luftdaten/gunicorn/aqi_luftdaten.wsgi,其内容如下:
import os
from django.core.wsgi import get_wsgi_application
# environment settings for Django app
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'aqi_luftdaten.settings')
# Initialize app Django
application = get_wsgi_application()
所以我跑
sudo su
cd /var/www/aqi_luftdaten/
source venv/bin/activate
sudo venv/bin/gunicorn gunicorn.aqi_luftdaten.wsgi:application --bind localhost:8000
但我明白
[2024-04-01 14:48:20 +0100] [7595] [INFO] Starting gunicorn 20.0.4
[2024-04-01 14:48:20 +0100] [7595] [INFO] Listening at: http://127.0.0.1:8000 (7595)
[2024-04-01 14:48:20 +0100] [7595] [INFO] Using worker: sync
[2024-04-01 14:48:20 +0100] [7597] [INFO] Booting worker with pid: 7597
[2024-04-01 14:48:20 +0100] [7597] [ERROR] Exception in worker process
Traceback (most recent call last):
File "/var/www/aqi_luftdaten/venv/lib/python3.8/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker
worker.init_process()
File "/var/www/aqi_luftdaten/venv/lib/python3.8/site-packages/gunicorn/workers/base.py", line 119, in init_process
self.load_wsgi()
File "/var/www/aqi_luftdaten/venv/lib/python3.8/site-packages/gunicorn/workers/base.py", line 144, in load_wsgi
self.wsgi = self.app.wsgi()
File "/var/www/aqi_luftdaten/venv/lib/python3.8/site-packages/gunicorn/app/base.py", line 67, in wsgi
self.callable = self.load()
File "/var/www/aqi_luftdaten/venv/lib/python3.8/site-packages/gunicorn/app/wsgiapp.py", line 49, in load
return self.load_wsgiapp()
File "/var/www/aqi_luftdaten/venv/lib/python3.8/site-packages/gunicorn/app/wsgiapp.py", line 39, in load_wsgiapp
return util.import_app(self.app_uri)
File "/var/www/aqi_luftdaten/venv/lib/python3.8/site-packages/gunicorn/util.py", line 358, in import_app
mod = importlib.import_module(module)
File "/usr/local/opt/python-3.8.1/lib/python3.8/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 961, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 973, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'gunicorn.aqi_luftdaten'
[2024-04-01 14:48:20 +0100] [7597] [INFO] Worker exiting (pid: 7597)
[2024-04-01 14:48:20 +0100] [7595] [INFO] Shutting down: Master
[2024-04-01 14:48:20 +0100] [7595] [INFO] Reason: Worker failed to boot.
所以看起来它找不到我的文件/var/www/aqi_luftdaten/gunicorn/aqi_luftdaten.wsgi
,但我不明白为什么。
我也尝试过
venv/bin/gunicorn gunicorn.aqi_luftdaten.wsgi:application --bind localhost:8000
没有sudo
,但错误仍然存在
答案1
依照指示此主题,我已将文件移动/var/www/aqi_luftdaten/gunicorn/aqi_luftdaten.wsgi
到/var/www/aqi_luftdaten/aqi_luftdaten.wsgi
,然后运行
PYTHONPATH=`pwd`/.. venv/bin/gunicorn aqi_luftdaten.wsgi:application --bind localhost:8000
现在应用程序可以正常工作,可以http://<RaspberryIP>:3000/
通过连接到 LAN 的任何外部设备浏览器访问