I understand how to create a virtualenv, activate that virtualenv, and then install django while the virtualenv is activated. Ex:
virtualenv ~/narnia
source ~/narnia/bin/activate
pip install django
From what I understand, when you "activate" a virtual environment it just changes some path variables while you're in a shell, nothing more. So when django is installed with pip while narnia is activated, it simply installs things into locations relative to ~/narnia instead of relative to /usr (ex. ~/narnia/lib/python2.6/site-packages instead of /usr/lib/python2.6/site-packages).
And now let's say I want to create a django project with that virtualenv. I don't even have to be activated, correct? I could just do:
deactivate
cd ~
narnia/bin/django-admin.py startproject myproject
So, if I have a web server set up with "myproject"...how is that django project tied to the narnia virtualenv at this point? Won't the system path variables simply be the normal systemwide python paths? How does "myproject" know to use the narnia environment?
I am a little confused on how that works. Any help is appreciated.
Thanks!
Web servers typically have a way to configure their Python use. You can set environment variables, paths, and so on. Use these tools to point to the virtualenv.
For example, in my Apache httpd.conf:
WSGIDaemonProcess myapp processes=2 threads=12 python-path=/home/nedbat/webapps/myapp/server:/home/nedbat/webapps/myapp/lib/python2.7
WSGIProcessGroup myapp
WSGIRestrictEmbedded On
WSGILazyInitialization On
WSGIScriptAlias / /home/nedbat/webapps/myapp/myapp.wsgi
and I have an myapp.wsgi file:
import os
import site
import sys
VE = '/home/nedbat/webapps/myapp/ve'
site.addsitedir(VE + '/lib/python2.7/site-packages')
os.environ['DJANGO_SETTINGS_MODULE'] = 'prod_settings'
os.environ['USE_PYPY'] = 'y'
os.environ['TMPDIR'] = '/home/nedbat/webapps/myapp/tmp'
from django.core.handlers.wsgi import WSGIHandler
application = WSGIHandler()
If you wish to use a virtualenv with Django in a production or even staging environment, I'd expect you to activate the environment and, depending on whether you do it manually or use e.g. virtualenvwrapper, set the appropriate environment variables. I'm not sure what purpose it would have to install Django in a virtual environment and then not use it.
By the way, just as I look at this I see the related question Django and VirtualEnv Development/Deployment Best Practices show up in the sidebar. This seems to be rather relevant for your set-up.