ImportError:没有名为'django'uWSGI的模块Django Nginx Ubuntu 16.04 Python3.6

时间:2019-08-05 12:52:32

标签: python django ubuntu-16.04 uwsgi

我花了一天的时间来解决这个问题。

我在Digital Ocean上与DjangoPython3.6NginxUbuntu 16.04uWSGI一起运行。

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

创建的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时,我会得到相同的结果。

更多错误日志是

Aug 05 12:18:20 myproject uwsgi[16998]: --- no python application found, check your system...

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有关。

从我的python manage.py runserver级别运行manage.py很好。

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

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

我的File: /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才能运行,而我没有解决方案,有人可以提供任何建议吗?

1 个答案:

答案 0 :(得分:1)

如果您在系统上使用两种不同版本的python(您的系统范围内的python似乎是3.5.2),则应pip install uwsgi 内部包含以下内容的virtualenv您将要使用的python版本。

然后,您应该直接使用指向特定路径的 uwsgi启动 / home / user / Env / myproject / bin / uwsgi :< / p>

> /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