【问题标题】:'import site' failed while trying to run UWSGI + virtualenv尝试运行 UWSGI + virtualenv 时“导入站点”失败
【发布时间】:2023-03-26 21:52:01
【问题描述】:

我在 CentOS 上安装了 Python 2.7 并使用以下方法为我的项目创建了一个 virtualenv:

$ virtualenv -p /usr/local/bin/python2.7 venv

我已经安装了 uwsgi,但 virtualenv 已停用。

我还安装了 uwsgi-plugin-python,因为我遇到了“请求的修饰符不可用:”问题。

nginx 配置:

upstream django {
    server unix:///tmp/mysite.sock; # for a file socket
}

server {
    listen      80;
    server_name mysite;
    charset     utf-8;

    client_max_body_size 75M;   # adjust to taste

    location /media  {
        alias /projects/mysite/media;
    }

    location /static {
        alias /projects/rebus/rebus/static;
    }

    location / {
        uwsgi_pass  django;
        include     /etc/nginx/uwsgi_params;
    }
}

uwsgi ini 文件:

[uwsgi]

chdir  = /projects/mysite
module = mysite.wsgi
virtualenv  = /projects/mysite/venv
plugin = python
master = true
processes = 2
socket = /tmp/mysite.sock
chmod-socket = 664
vacuum = true

现在当我启动它并尝试访问该网站时:

uwsgi --ini mysite_uwsgi.ini

我得到以下日志:

*** Starting uWSGI 2.0.11.1 (64bit) on [Wed Aug 19 12:01:22 2015] ***
compiled with version: 4.4.7 20120313 (Red Hat 4.4.7-11) on 21 July 2015 15:58:54
os: Linux-2.6.32-042stab084.12 #1 SMP Tue Nov 26 20:18:08 MSK 2013
nodename: vs23.wovz.net
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 8
current working directory: /var/run
detected binary path: /usr/sbin/uwsgi
chdir() to /projects/mysite
your processes number limit is 1024
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 socket 0 bound to UNIX address /tmp/mysite.sock fd 3
Python version: 2.6.6 (r266:84292, Jul 23 2015, 15:22:56)  [GCC 4.4.7 20120313 (Red Hat 4.4.7-11)]
Set PythonHome to /projects/mysite/venv
'import site' failed; use -v for traceback
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x12e7160
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 218304 bytes (213 KB) for 2 cores
*** Operational MODE: preforking ***
Traceback (most recent call last):
  File "./mysite/wsgi.py", line 10, in <module>
    import os
ImportError: No module named os
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 master process (pid: 1106)
spawned uWSGI worker 1 (pid: 1107, cores: 1)
spawned uWSGI worker 2 (pid: 1108, cores: 1)
--- no python application found, check your startup logs for errors ---

看起来我的带有 Python 2.7 的 virtualenv 无法识别,并且 UWSGI 无法加载我的 Django 应用程序。我该如何解决?

【问题讨论】:

  • uwsgi.ini 中,您有virtualenv = ... 指向您的venv。我的(工作)配置文件由home=... 指定
  • @henrikstroem 你可以在这里使用。
  • 这是因为 uwsgi 是为 Python 2.6 编译的。你不能强制它使用 Python 2.7 的虚拟环境,它不会工作。

标签: python django virtualenv uwsgi


【解决方案1】:

这也可能与编译的 Python uwsgi 版本有关。如果您使用--python=python3 创建虚拟环境但使用通过pip install uwsgi 而不是pip3 install uwsgi 获得的uwsgi,那么您将在默认Python 为2.7 的系统上看到相同的症状('import site' failed)。

【讨论】:

    【解决方案2】:

    也许你没有在你的 virtualenv 中安装 uwsgi 模块

    解决方案:

    source /projects/mysite/venv/bin/activate 
    pip install uwsgi 
    

    然后重新运行你的 uwsgi 服务器,它可能会工作

    【讨论】:

      猜你喜欢
      • 2021-10-17
      • 1970-01-01
      • 1970-01-01
      • 2017-05-29
      • 2020-11-19
      • 2013-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多