首页 > 解决方案 > 我的 django,uwsgi 项目由于虚拟环境而无法运行

问题描述

我将在 Ubuntu Server 上使用 uwsgi 为我的 django 项目提供服务,但它没有运行。

我正在使用 python 3.6,但 uwsgi 显示它是 2.7

我将默认 python 更改为 python3.6 但 uwsgi 仍然不起作用。

这是我的命令:

uwsgi --http :8001 --home /home/ubuntu/repository/env --chdir 
/home/ubuntu/repository/project -w project.wsgi

这是错误消息:

*** Starting uWSGI 2.0.18 (64bit) on [Tue Jun  4 21:03:58 2019] ***
compiled with version: 5.4.0 20160609 on 04 June 2019 11:39:14
os: Linux-4.4.0-1079-aws #89-Ubuntu SMP Tue Mar 26 15:25:52 UTC 2019
nodename: ip-172-31-18-239
machine: x86_64
clock source: unix
detected number of CPU cores: 2
current working directory: /home/ubuntu/repository/charteredbus
*** running under screen session 1636.sbus ***
detected binary path: /usr/local/bin/uwsgi
!!! no internal routing support, rebuild with pcre support !!!
chdir() to /home/ubuntu/repository/charteredbus
*** WARNING: you are running uWSGI without its master process manager ***
your processes number limit is 15738
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 :8001 fd 4
spawned uWSGI http 1 (pid: 8402)
uwsgi socket 0 bound to TCP address 127.0.0.1:39614 (port auto-assigned) fd 3
Python version: 2.7.12 (default, Nov 12 2018, 14:36:49)  [GCC 5.4.0 20160609]
Set PythonHome to /home/ubuntu/repository/env
ImportError: No module named site

标签: pythondjangoubuntuuwsgi

解决方案


不幸的是,uWSGI 必须使用与您的 virtualenv 匹配的 python 版本进行编译。这意味着:如果 uWSGI 是用 python 2.7 编译的,你不能在你的 virtualenv(和你的 Django 应用程序)中使用 python 3.6。

幸运的是,有一些方法可以解决这个问题:

  • 在您的 virtualenv 中安装 uWSGI 并使用该 uWSGI 二进制文件运行 Django。
  • 使用 Python 作为 uWSGI 的插件。

第一个非常简单。您需要做的就是在启动脚本中更改 uWSGI 二进制文件的路径,以指向安装在 virtualenv 中的 uWSGI。(如果你使用 systemd 启动 uWSGI,我推荐 systemd 用户单元。只是不要忘记运行loginctl enable-linger

第二个没那么复杂。首先你必须安装没有 python 插件的 uWSGI,然后为你需要的所有 python 版本安装单独的插件。您可以在此处找到更多信息。如果您使用 uWSGI,您的系统包存储库中可能有现成的插件。


推荐阅读