Python uwsgi w / virtualenv --no-site-packages - import site = AttributeError {'userbase'}

时间:2011-12-16 01:22:44

标签: python nginx virtualenv uwsgi

长话短说我有一个nginx / uwsgi部署,我无法使用

import site
在部署脚本中

而不会引发错误

Traceback (most recent call last):
  File "/home/PROJECT/virtualenv/PROJECT/deploy/deploy.py", line 1, in <module>
    import site
  File "/usr/lib/python2.7/site.py", line 562, in <module>
    main()
  File "/usr/lib/python2.7/site.py", line 544, in main
    known_paths = addusersitepackages(known_paths)
  File "/usr/lib/python2.7/site.py", line 271, in addusersitepackages
    user_site = getusersitepackages()
  File "/usr/lib/python2.7/site.py", line 260, in getusersitepackages
    USER_SITE = get_path('purelib', '%s_user' % os.name)
  File "/usr/lib/python2.7/sysconfig.py", line 426, in get_path
    return get_paths(scheme, vars, expand)[name]
  File "/usr/lib/python2.7/sysconfig.py", line 417, in get_paths
    return _expand_vars(scheme, vars)
  File "/usr/lib/python2.7/sysconfig.py", line 177, in _expand_vars
    res[key] = os.path.normpath(_subst_vars(value, vars))
  File "/usr/lib/python2.7/sysconfig.py", line 159, in _subst_vars
    raise AttributeError('{%s}' % var)
AttributeError: {'userbase'}

如果我将sys.path.append用于所有内容,那么虽然它不遵循任何egg路径链接,但它仍可正常工作

的引用:

nginx.conf

server {
    listen 80;
    server_name example.com;

    access_log /home/PROJECT/logs/nginx/access.log;
    error_log /home/PROJECT/logs/nginx/error.log;
    client_max_body_size 10m;
    keepalive_timeout 120;

    location /static/ {
#        root /home/PROJECT/virtualenv/PROJECT/;
        alias /home/PROJECT/virtualenv/PROJECT/static/;
        autoindex on;
#        error_page 404 = "404";
    }
    location /media/ {
#        root /home/PROJECT/virtualenv/PROJECT/;
        alias /home/PROJECT/virtualenv/PROJECT/media/;
        autoindex on;
        error_page 404 = "404";
    }
    location / {
        uwsgi_pass uwsgi_main;
        include uwsgi_params;
        uwsgi_param UWSGI_PYHOME /home/PROJECT/virtualenv;
        uwsgi_param UWSGI_SCRIPT deploy.deploy;
        uwsgi_param UWSGI_CHDIR /home/PROJECT/virtualenv/PROJECT;
        root /home/PROJECT/virtualenv;
    }
}

uwsgi upstart script

description "uWSGI starter"
start on (local-filesystems
and runlevel [2345])
stop on runlevel [016]
respawn
exec /usr/local/sbin/uwsgi \
--uid www-data \
--socket 127.0.0.1:5050 \
--master \
--logto /var/log/uwsgi_main.log \
--logdate \
--optimize 2 \
--processes 2 \
--harakiri 120 \
--vhost \
--no-site

deploy.py

import sys 
import site 
import os 

envroot = '/home/project/virtualenv' 
#envroot = os.path.join(os.path.abspath(__file__), '../..') 

prev_sys_path = list(sys.path) 
site.addsitedir(os.path.join(envroot, 'lib/python2.7/site-packages')) 
sys.path.append(os.path.join(envroot, 'project')) 

new_sys_path = [p for p in sys.path if p not in prev_sys_path] 
for item in new_sys_path: 
    sys.path.remove(item) 
sys.path[:0] = new_sys_path 

os.environ['DJANGO_SETTINGS_MODULE'] = 'settings' 
from django.core.handlers.wsgi import WSGIHandler 
application = WSGIHandler()

有什么想法吗?解决方法?

目标是能够使用virtualenv设置python路径,并跟随.egg&amp;中的路径链接。 .pth文件

1 个答案:

答案 0 :(得分:2)

我对uwsgi和nginx配置不太熟悉。但我知道你需要使用virtualenv提供的python解释器来运行你的WSGI应用程序运行器deploy.py。在你的情况下/home/project/virtualenv/bin/python

您可能拥有uWSGI 0.9.7或更高版本。在这种情况下,这是您的问题的workaround。此邮件列表中的信息已在tip中修复此问题。这可能意味着它已经固定在当前稳定的0.9.9版本中。