简体   繁体   English

Django,uwsgi,nginx,virtualenv,ImportError:没有名为site的模块

[英]Django, uwsgi, nginx, virtualenv, ImportError: No module named site

I have a Django app and a virtual env and I try to run it under nginx+uwsgi. 我有一个Django应用程序和一个虚拟环境,我尝试在nginx + uwsgi下运行它。 I've configured the whole system as is described here . 我按照这里描述的那样配置了整个系统。 I have the CentOS 7 as well. 我也有CentOS 7。

However, I get the famous "ImportError: No module named site". 但是,我得到了着名的“ImportError:No module named site”。 No other topic here on stack overflow helped me to solve this. 这里没有关于堆栈溢出的其他主题帮我解决了这个问题。

In the log of uwsgi I have these two lines: 在uwsgi的日志中,我有以下两行:

    Set PythonHome to /hedgehog/.virtualenv/hedgehog
    ImportError: No module named site
/etc/uwsgi/sites/hedgehog.ini: 
    [uwsgi]
    project = hedgehog
    username = hedgehog
    base = /var/www/%(username)/code

    chdir = /var/www/hedgehog/code
    home = /%(username)/.virtualenv/%(username)
    module = %(username).wsgi:application

    master = true
    processes = 5

    uid = %(username)
    socket = /run/uwsgi/%(project).sock
    chown-socket = %(username):nginx
    chmod-socket = 660
    vacuum = true
    logto = /var/www/%(username)/log/uwsgi.log

/etc/systemd/system/uwsgi.service: /etc/systemd/system/uwsgi.service:

[Unit]
Description=uWSGI Emperor service

[Service]
ExecStartPre=/usr/bin/bash -c 'mkdir -p /run/uwsgi; chown hedgehog:nginx/run/uwsgi'
ExecStart=/usr/bin/uwsgi --emperor /etc/uwsgi/sites
Restart=always
KillSignal=SIGQUIT
Type=notify
NotifyAccess=all

[Install]
WantedBy=multi-user.target

The app is in /var/www/hedgehog/code. 该应用程序位于/ var / www / hedgehog / code中。

Seems that uwsgi somehow doesn't use the virtualenv. 似乎uwsgi不知何故不使用virtualenv。 I've tried adding this to the ini file: 我已经尝试将其添加到ini文件中:

plugins = python
virtualenv = %(home)

It didn't help. 它没有帮助。

However, if I run "import site" in python interpreter in this virtualenv it gives no error: 但是,如果我在这个virtualenv中运行python解释器中的“import site”,它就不会出错:

[rreimche@rreimche-web sites]$ sudo -u hedgehog -H bash -l
[sudo] password for rreimche: 
[hedgehog@rreimche-web sites]$ python
Python 2.7.5 (default, Jun 24 2015, 00:41:19) 
[GCC 4.8.3 20140911 (Red Hat 4.8.3-9)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import site
>>> exit()

I had the same error and a mistake: I used a Python3 virtualenv by mistake. 我有同样的错误和错误:我错误地使用了Python3 virtualenv。 Once I used a Python2.7 virtualenv it worked. 一旦我使用了Python2.7 virtualenv就可以了。

Cheers 干杯

You don't really need the virtualenv entry in your config since it's the same option as home entry. 您不需要在配置中使用virtualenv条目,因为它与home条目的选项相同。 /%(username)/.virtualenv/%(username) should resolve as /hedgehog/.virtualenv/hedgehog. /%(username)/.virtualenv/%(username)应解析为/hedgehog/.virtualenv/hedgehog。 What does ls -l /hedgehog/.virtualenv/hedgehog say? ls -l /hedgehog/.virtualenv/hedgehog说什么? You may also want to check that you are loading the python plugin for the very same version of python you are using creating your virtualenv. 您可能还想检查您是否正在为正在使用创建virtualenv的python版本加载python插件。 Please paste more log, there may be more hints on what is wrong. 请粘贴更多日志,可能会有更多关于错误的提示。

This also happened to me when I switch my virtualenv to use Python 3, it had been working well under Python 2.7. 当我将virtualenv切换为使用Python 3时,这也发生在我身上,它在Python 2.7下运行良好。 Here's how I solve it: 这是我如何解决它:

replace 更换

plugins = python

with

plugins = python3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM