gpt4 book ai didi

python - 导入错误: No module named 'django' uWSGI Django Nginx Ubuntu 16. 04 Python3.6

转载 作者:行者123 更新时间:2023-12-01 07:27:56 27 4
gpt4 key购买 nike

我花了一天时间试图解决这个问题。

我正在使用 DjangoPython3.6NginxDigital Ocean 上运行Ubuntu 16.04uWSGI

必须通过 deadsnakesppa 安装 Python3.6 并拥有我用

创建的 virtualenv
mkvirtualenv --python=python3.6 myproject

我正在关注this tutorial我之前遵循过,但是当我尝试运行以下代码来测试应用程序服务器(不在 virtualenv 中)时

uwsgi --http :8080 --home /home/user/Env/myproject --chdir /home/user/myproject/src/myproject -w myproject.wsgi

我得到了这个回溯

*** Starting uWSGI 2.0.18 (64bit) on [Mon Aug  5 12:23:53 2019] ***
compiled with version: 5.4.0 20160609 on 05 August 2019 11:53:48
os: Linux-4.4.0-157-generic #185-Ubuntu SMP Tue Jul 23 09:17:01 UTC 2019
nodename: myproject
machine: x86_64
clock source: unix
detected number of CPU cores: 1
current working directory: /home/user/myproject/src/myproject
detected binary path: /usr/local/bin/uwsgi
!!! no internal routing support, rebuild with pcre support !!!
*** WARNING: you are running uWSGI without its master process manager ***
your processes number limit is 3898
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 :8080 fd 4
spawned uWSGI http 1 (pid: 17338)
uwsgi socket 0 bound to TCP address 127.0.0.1:34660 (port auto-assigned) fd 3
Python version: 3.5.2 (default, Nov 12 2018, 13:43:14) [GCC 5.4.0 20160609]
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x21aaf90
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 72904 bytes (71 KB) for 1 cores
*** Operational MODE: single process ***
Traceback (most recent call last):
File "./myproject/wsgi.py", line 12, in <module>
from django.core.wsgi import get_wsgi_application
ImportError: 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: 17337, cores: 1)

当我激活 virtualenv 时,我得到了同样的结果。

更多错误日志是

8 月 05 日 12:18:20 myproject uwsgi[16998]: --- 未找到 python 应用程序,请检查您的系统...

Aug 05 11:39:47 myproject systemd[1]: uwsgi.service: Main process exited, code=exited, status=203/EXEC
Aug 05 11:39:47 myproject systemd[1]: Failed to start uWSGI Emperor service.
Aug 05 11:39:47 wmyproject systemd[1]: uwsgi.service: Unit entered failed state.
Aug 05 11:39:47 myproject systemd[1]: uwsgi.service: Failed with result 'exit-code'.
Aug 05 11:39:47 myproject systemd[1]: uwsgi.service: Service hold-off time over, scheduling restart.
Aug 05 11:39:47 myproject systemd[1]: Stopped uWSGI Emperor service.
Aug 05 11:39:47 myproject systemd[1]: Starting uWSGI Emperor service...
Aug 05 11:39:47 myproject systemd[14970]: uwsgi.service: Failed at step EXEC spawning /usr/local/bin/uwsgi: No such file or directory

如果我运行以下命令(不在 virtualenv 内)

python3 -c 'import django.core.wsgi'

我明白

Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: No module named 'django'

但是如果我在 virtualenv 中运行它,我不会收到错误,这意味着该错误与 uWSGI 有关。

从我的 manage.py 级别运行 python manage.py runserver 工作正常。

pip3 -V 给出来自/home/user/.local/lib/python3.6/site-packages/pip (python 3.6) 的 pip 19.2.1

python3.6 -V给出Python 3.6.9

我尝试使用sudo -H pip3 install uwsgi重新安装uWSGI,它似乎总是安装到python 3.5 site-packages

如果我在 virtualenv 中运行 pip3 install uwsgi ,它会安装到

Uninstalling uWSGI-2.0.18:
Would remove:
/home/user/Env/myproject/bin/uwsgi
/home/user/Env/myproject/lib/python3.6/site-packages/uWSGI-2.0.18.dist-info/*
/home/user/Env/myproject/l

ib/python3.6/site-packages/uwsgidecorators.py

我的.ini 文件

[uwsgi]
project = myproject
uid = user
base = /home/%(uid)

chdir = %(base)/%(project)/src/%(project)
home = %(base)/Env/%(project)
module = %(project).wsgi:application

master = true
processes = 5

socket = /run/uwsgi/%(project).sock
chown-socket = %(uid):www-data
chmod-socket = 660
vacuum = true

socket = /run/uwsgi/%(project).sock
chown-socket = %(uid):www-data
chmod-socket = 660
vacuum = true

我的文件:/etc/systemd/system/uwsgi.service

[Unit]
Description=uWSGI Emperor service

[Service]
ExecStartPre=/bin/bash -c 'mkdir -p /run/uwsgi; chown user:www-data /run/uwsgi'
ExecStart=/home/user/Env/myproject/bin/uwsgi --emperor /etc/uwsgi/sites
Restart=always
KillSignal=SIGQUIT
Type=notify
NotifyAccess=all

[Install]
WantedBy=multi-user.target

我的项目显然需要 python3.6 才能运行,但我没有解决方案,有人可以提供任何建议吗?

最佳答案

如果您的系统上使用两个不同版本的 python(您的系统范围的 python 似乎是 3.5.2),那么您应该 pip install uwsgi inside包含您要使用的 python 版本的 virtualenv。

然后您应该直接使用 uwsgi 的路径来启动该特定的 uwsgi,在您的情况下为 /home/user/Env/myproject/bin/uwsgi:

> /home/user/Env/myproject/bin/uwsgi --ini /path/to/file/uwsgi.ini

> /home/user/Env/myproject/bin/uwsgi --http :8080 --home /home/user/Env/myproject --chdir /home/user/myproject/src/myproject -w myproject.wsgi

关于python - 导入错误: No module named 'django' uWSGI Django Nginx Ubuntu 16. 04 Python3.6,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57358996/

27 4 0