3

I'm having trouble getting Apache/WSGI to use my VirtualEnv. I have added the following two lines (path on server is pointing to the actual location of site-packages in the target virtualenv) to my WSGI file:

import site
site.addsitedir('/sites/mysite/virtpy/lib/python2.6/site-packages')

(from http://www.foxhop.net/django-virtualenv-apache-mod_wsgi). However, when I try to load the url in the browser I get a 500. Checking the apache logs:

 [Sun Jul 17 11:07:11 2011] [error] [client 94.170.105.142]     app =   import_module(appname)
[Sun Jul 17 11:07:11 2011] [error] [client 94.170.105.142]   File "/usr/local/lib/python2.6/dist-packages/django/utils/importlib.py", line 35, in import_module
[Sun Jul 17 11:07:11 2011] [error] [client 94.170.105.142]     __import__(name)
[Sun Jul 17 11:07:11 2011] [error] [client 94.170.105.142] TemplateSyntaxError: Caught ImportError while rendering: No module named tagging
[Sun Jul 17 11:07:11 2011] [debug] mod_deflate.c(615): [client 94.170.105.142] Zlib: Compressed 629 to 387 : URL /

So I guess that the VirtualEnv isn't being loaded. Anyone know how to tell Apache / WSGI the correct virtualenv to use?

UPDATE

I have updated django.wsgi following Ken's advice, but now I am getting the following error in the apache log

[Sun Jul 17 16:46:36 2011] [info] [client 94.170.105.142] mod_wsgi (pid=11260, process='', application='igniteflow-django.com:8090|'): Loading WSGI script '/sites/igniteflow/apache/django.wsgi'.
[Sun Jul 17 16:46:36 2011] [error] [client 94.170.105.142] mod_wsgi (pid=11260): Target WSGI script '/sites/igniteflow/apache/django.wsgi' cannot be loaded as Python module.
[Sun Jul 17 16:46:36 2011] [error] [client 94.170.105.142] mod_wsgi (pid=11260): Exception occurred processing WSGI script '/sites/igniteflow/apache/django.wsgi'.
[Sun Jul 17 16:46:36 2011] [error] [client 94.170.106.142] Traceback (most recent call last):
[Sun Jul 17 16:46:36 2011] [error] [client 94.170.105.142]   File "/sites/igniteflow/apache/django.wsgi", line 5, in <module>
[Sun Jul 17 16:46:36 2011] [error] [client 94.170.105.142]     execfile(activate_this, dict(__file__=activate_this))
[Sun Jul 17 16:46:36 2011] [error] [client 94.170.105.142] IOError: [Errno 13] Permission denied: '/root/.virtualenvs/igniteflow/bin/activate_this.py'

I assume this is because the virtualenv is in root and apache doesn't have permissions? I chowned the folder to root:www-data but it hasn't fixed the problem. Any suggestions?

igniteflow
  • 8,404
  • 10
  • 38
  • 46
  • Are you sure the tagging library is in the `site-packages` dir of your virtual env? Depending on how you set your env up, some libraries get installed from src, not in the site-packages folder – Craig Blaszczyk Jul 17 '11 at 15:10

1 Answers1

9

In my app.wsgi file I have something like this. You will need to change it to put to where your virtual env is located, mine is under /opt/ve/ve_name/ in this example.

import os
# activate virtualenv
activate_this = os.path.expanduser("/opt/ve/ve_name/bin/activate_this.py")
execfile(activate_this, dict(__file__=activate_this))
Ken Cochrane
  • 75,357
  • 9
  • 52
  • 60
  • IOError: [Errno 13] Permission denied: '/root/.virtualenvs/site1/bin/activate_this.py' Do you know how I can install the virtualenv somewhere where apache will have access? I tried making the folder root:www-data but no change – igniteflow Jul 17 '11 at 16:41
  • 1
    It looks like you are using virtualenvwrapper. To change where your ve's are installed with virtualenvwrapper you will need to change the value of WORKON_HOME; export WORKON_HOME=/opt/Envs ; make sure the directory exists, and you will need to move your current envs there, unless you want to create from scratch. In production I don't bother using virtualenvwrapper I just use strait up virtualenv, it allows me to put my ve's where ever I want, but requires more manual work. – Ken Cochrane Jul 17 '11 at 22:20
  • thanks! that got it working. I set WORKON_HOME in bashrc to be /sites/.virtualenv, recreated the virtualenv with mkvirtualenv --no-site-packages and it's now all up and running – igniteflow Jul 17 '11 at 23:44