Django system-wide install to virtualenv

淺唱寂寞╮ 提交于 2019-12-12 10:24:10

问题


I've recently deployed an application online using DigitalOcean single-click droplet setup which setup Django on Ubuntu with nginx and gunicorn. It came with a default django project which I've managed to change to my own. However, the default project doesn't use a virtualenv, it uses a system-wide install. So, the app only works if all the dependencies are installed on the system. I know this because if I uninstall django, it gives me an internal server error.

I would like to use the python in my virtualenv as the interpreter. And refer to the site-packages in that environment. I've tried fiddling with the PYTHONPATH and adding sys.path.append('/home/env/projectname') to the wsgi file but this doesn't work.

How can I achieve this?

/etc/init/gunicorn.conf:

setuid django
setgid django
chdir /home/env/projectname

exec gunicorn \
    --name=prj \
    --pythonpath=prj \
    --bind=127.0.0.1:9000 \
    --config /etc/gunicorn.d/gunicorn.py \
    prj.wsgi:application

回答1:


Try installing the gunicorn into virtualenv and run it from there setting up the working directory and passing the wsgi:application. This tutorial may help you. This tutorial may also be useful.




回答2:


  1. Don't use the name "venv" for virtualenvs, because venv is a similar but different thing. "venv" and "virtualenv" are different ways to create isolated environments; venv is the new way, but it is only available for Python 3.3 and later, and so virtualenv is still more widely used.
  2. To avoid confusion, remember that virtualenv (and venv, for that matter) is badly named. There is nothing virtual about it. It should better be called an isolated environment.

Now, gunicorn is a Python program. It doesn't run your Django project; it imports your Django project. So you can't possibly run gunicorn and your Django project in different environments, because they are actually one program. The way to run your Django project in a specific isolated environment is to start gunicorn in that isolated environment:

source your_virtualenv_dir/bin/activate    
exec gunicorn ...

For this to work, you must either have installed gunicorn in the virtualenv, or you must have installed gunicorn system-wide and created the virtualenv with --system-site-packages.

See also this article of mine.



来源:https://stackoverflow.com/questions/40821716/django-system-wide-install-to-virtualenv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!