Python uwsgi w/ virtualenv --no-site-packages -- import site = AttributeError {'userbase'}
长话短说,我有一个 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 和 .egg 中的路径链接。 .pth 文件
Long story short I have a nginx/uwsgi deployment where I cannot use
import site
within the deploy script without raising error
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'}
If I use sys.path.append for everything then it works though it doesn't follow any of the egg path links
references:
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()
Any ideas? Ways to work around??
Goal is to be able to set the python path using the virtualenv with it following path links within .egg & .pth files
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我对uwsgi和nginx配置不是很熟悉。但我知道您需要使用 virtualenv 提供的 python 解释器来运行 WSGI 应用程序运行器
deploy.py
。在您的情况下/home/project/virtualenv/bin/python
。您可能拥有 uWSGI 0.9.7 或更高版本。在这种情况下,这里是针对您的问题的解决方法。此邮件列表中有信息表明此问题已在
tip
中修复。这可能意味着它已经在当前稳定的 0.9.9 版本中得到修复。I'm not very familiar with uwsgi and nginx configuration. But I know that you need to run your WSGI application runner
deploy.py
with the python interpreter provided by your virtualenv./home/project/virtualenv/bin/python
in your case.It is possible that you have uWSGI 0.9.7 or later. In this case here is the workaround for your problem. There is information in this mailing list that this issue were fixed in
tip
. This might mean that it is already fixed in the current stable 0.9.9 version.