Python uwsgi w/ virtualenv --no-site-packages — import site = AttributeError {'userbase'}
-
18-03-2021 - |
Question
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
Solution
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.