Ubuntu服务器部署Django+Gunicorn遇ModuleNotFoundError求助
相关文件
gunicorn_myproject.socket
[Unit] Description=myproject socket [Socket] ListenStream=/run/gunicorn_myproject.sock [Install] WantedBy=sockets.target
gunicorn_myproject.service
[Unit] Description=myproject daemon Requires=gunicorn_myproject.socket After=network.target [Service] User=myuser Group=www-data WorkingDirectory=/home/myuser/github/myproject ExecStart=/home/myuser/github/venvs/myproject/bin/gunicorn \ --access-logfile - \ --workers 5 \ --bind unix:/run/gunicorn_myproject.sock \ /home/myuser/github/myproject/MyProject.wsgi:application [Install] WantedBy=multi-user.target
错误日志
执行sudo systemctl start gunicorn.socket后,运行sudo systemctl status gunicorn.socket显示:
● gunicorn_myproject.socket - myproject socket Loaded: loaded (/etc/systemd/system/gunicorn_myproject.socket; enabled; vendor preset: enabled) Active: active (listening) since Tue 2023-01-31 18:43:11 -03; 1s ago Triggers: ● gunicorn_myproject.service Listen: /run/gunicorn_myproject.sock (Stream) CGroup: /system.slice/gunicorn_myproject.socket Jan 31 18:43:11 cpro49739 systemd[1]: Listening on myproject socket.
此时无异常,但执行curl --unix-socket /run/gunicorn_myproject.sock localhost时返回:
curl: (56) Recv failure: Connection reset by peer
随后执行sudo systemctl status gunicorn.service得到错误:
● gunicorn_myproject.service - myproject daemon Loaded: loaded (/etc/systemd/system/gunicorn_myproject.service; enabled; vendor preset: enabled) Active: failed (Result: exit-code) since Tue 2023-01-31 18:47:29 -03; 1min 33s ago TriggeredBy: ● gunicorn_myproject.socket Process: 2539537 ExecStart=/home/myuser/github/venvs/myproject/bin/gunicorn --access-logfile - --workers 5 --bind unix:/run/gunicorn_myproject.sock /home/myuser/github/myproject/MyProject.wsgi:application (code=exited, status=3) Main PID: 2539537 (code=exited, status=3) Jan 31 18:47:29 cpro49739 gunicorn[2539539]: ModuleNotFoundError: No module named '/home/myuser/github/myproject/MyProject' Jan 31 18:47:29 cpro49739 gunicorn[2539539]: [2023-01-31 18:47:29 -0300] [2539539] [INFO] Worker exiting (pid: 2539539) Jan 31 18:47:29 cpro49739 gunicorn[2539537]: [2023-01-31 18:47:29 -0300] [2539537] [WARNING] Worker with pid 2539539 was terminated due to signal 15 Jan 31 18:47:29 cpro49739 gunicorn[2539537]: [2023-01-31 18:47:29 -0300] [2539537] [INFO] Shutting down: Master Jan 31 18:47:29 cpro49739 gunicorn[2539537]: [2023-01-31 18:47:29 -0300] [2539537] [INFO] Reason: Worker failed to boot. Jan 31 18:47:29 cpro49739 systemd[1]: gunicorn_myproject.service: Main process exited, code=exited, status=3/NOTIMPLEMENTED Jan 31 18:47:29 cpro49739 systemd[1]: gunicorn_myproject.service: Failed with result 'exit-code'. Jan 31 18:47:29 cpro49739 systemd[1]: gunicorn_myproject.service: Start request repeated too quickly. Jan 31 18:47:29 cpro49739 systemd[1]: gunicorn_myproject.service: Failed with result 'exit-code'. Jan 31 18:47:29 cpro49739 systemd[1]: Failed to start myproject daemon.
已尝试操作
更换部署目录、使用其他用户、重新创建gunicorn.socket和gunicorn.service文件,均未解决问题。
项目文件夹结构
github └── myproject ├── Apps │ ├── app1 │ │ └── admin.py, apps.py, models.py, tests.py, urls.py, views.py │ ├── app2 │ │ └── admin.py, apps.py, models.py, tests.py, urls.py, views.py │ └── app3 │ └── admin.py, apps.py, models.py, tests.py, urls.py, views.py ├── MyProject │ ├── __init__.py │ ├── asgi.py │ ├── local_settings.py │ ├── settings.py │ ├── urls.py │ └── wsgi.py ├── root | └── (css, js, images, etc.) ├── static | └── (css, js, images, etc.) ├── templates | └── (html files) ├── manage.py ├── README.md └── requirements.txt
问题解决方法
核心问题
错误提示ModuleNotFoundError: No module named '/home/myuser/github/myproject/MyProject'是因为Gunicorn调用WSGI模块时用了错误的路径格式。你写了完整的文件路径,但Gunicorn需要的是基于工作目录的Python包引用格式。
步骤1:修改gunicorn_myproject.service配置
打开/etc/systemd/system/gunicorn_myproject.service,将ExecStart中的WSGI模块路径修改:
原内容:
ExecStart=/home/myuser/github/venvs/myproject/bin/gunicorn \ --access-logfile - \ --workers 5 \ --bind unix:/run/gunicorn_myproject.sock \ /home/myuser/github/myproject/MyProject.wsgi:application
修改后:
ExecStart=/home/myuser/github/venvs/myproject/bin/gunicorn \ --access-logfile - \ --workers 5 \ --bind unix:/run/gunicorn_myproject.sock \ MyProject.wsgi:application
原因:已经设置了WorkingDirectory=/home/myuser/github/myproject,Gunicorn会在此目录下查找Python模块,直接写MyProject.wsgi:application即可(MyProject是包含wsgi.py的合法Python包,因为目录下有__init__.py)。
步骤2:重启服务生效
执行以下命令重新加载systemd配置并重启服务:
sudo systemctl daemon-reload sudo systemctl restart gunicorn_myproject.socket sudo systemctl restart gunicorn_myproject.service
步骤3:验证修复
再次执行curl测试:
curl --unix-socket /run/gunicorn_myproject.sock localhost
如果返回Django项目的响应内容,说明问题解决。
额外排查(若问题仍存在)
- 确认虚拟环境
/home/myuser/github/venvs/myproject已安装所有依赖:进入虚拟环境执行pip list,检查Django、Gunicorn是否存在。 - 检查
MyProject目录下的__init__.py是否存在(即使是空文件也可以,确保Python识别为包)。 - 确保
myuser对/home/myuser/github/myproject目录有读写权限。
内容的提问来源于stack exchange,提问作者Adriano Dias

