George Silvis
George Silvis

Reputation: 11

How does a django app start it's virtualenv?

I'm trying to understand how virtual environment gets invoked. The website I have been tasked to manage has a .venv directory. When I ssh into the site to work on it I understand I need to invoke it with source .venv/bin/activate. My question is: how does the web application invoke the virtual environment? How do I know it is using the .venv, not the global python?

More detail: It's a Drupal website with Django kind of glommed onto it. Apache it the main server. I believe the Django is served by gunicorn. The designer left town _

Upvotes: 0

Views: 268

Answers (3)

George Silvis
George Silvis

Reputation: 11

Okay, I've found how, in my case, the virtualenv was being invoked for the django.

BASE_DIR/run/gunicorn script has:

#GUNICORN='/usr/bin/gunicorn'
GUNICORN=".venv/bin/gunicorn"
GUNICORN_CONF="$BASE_DIR/run/gconf.py"
.....
$GUNICORN --config $GUNICORN_CONF --daemon --pid $PIDFILE $MODULE

So this takes us into the .venv where the gunicorn script starts with:

#!/media/disk/www/aavso_apps/.venv/bin/python

Voila

Upvotes: 1

AndrewSmiley
AndrewSmiley

Reputation: 1963

If you use just Django behind a reverse proxy, Django will use whatever is the python environment for the user that started the server was determined in which python command. If you're using a management tool like Gunicorn, you can specify which environment to use in those configs. Although Gunicorn itself requires us to activate the virtual environment before invoking Gunicorn EDIT: Since you're using Gunicorn, take a look at this, https://www.digitalocean.com/community/tutorials/how-to-deploy-python-wsgi-apps-using-gunicorn-http-server-behind-nginx

Upvotes: 0

Artem Bernatskyi
Artem Bernatskyi

Reputation: 4677

Just use absolute path when calling python in virtualenv.
For example your virtualenv is located in /var/webapps/yoursite/env
So you must call it /var/webapps/yoursite/env/bin/python

Upvotes: 0

Related Questions