[Answered ]-Django fails to find R when loading RPy2 under virtualenv

2👍

The best way to resolve this issue is to add

os.environ['R_HOME']='/usr/local/Cellar/r/3.0.1/R.framework/Resources'

to the beginning of the wsgi.py file that is being used for mod_wsgi / django. It should be added before activating the virtual environment. Any other environ variables should also be added here. So now the wsgi.py file looks like this:

import os,sys,site
os.environ["R_HOME"]='/usr/local/Cellar/r/3.0.1/R.framework/Resources'
site.addsitedir('/Users/kaiju/.virtualenvs/nauru_dev/lib/python2.7/site-packages')

# Activate the virtual environment
# Taken from http://thecodeship.com/deployment/deploy-django-apache-virtualenv-and-mod_wsgi/
activate_env = os.path.expanduser('/Users/kaiju/.virtualenvs/nauru_dev/bin/activate_this.py')
execfile(activate_env,dict(__file__=activate_env))

sys.path.insert(0,'/usr/local/var/django/code')
sys.path.insert(0,'/usr/local/var/django/code/ri')

# We defer to a DJANGO_SETTINGS_MODULE already in the environment. This breaks
# if running multiple sites in the same mod_wsgi process. To fix this, use
# mod_wsgi daemon mode with each site in its own daemon process, or use
# os.environ["DJANGO_SETTINGS_MODULE"] = "ri.settings"
os.environ.setdefault("DJANGO_SETTINGS_MODULE","ri.settings")

# This application object is used by any WSGI server configured to use this
# file. This includes Django's development server, if the WSGI_APPLICATION
# setting points here.
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

Many thanks to those who contributed to this discussion!

0👍

I believe that sys.path has little to do with the environment variable PATH, which the one that should be set (along with LD_LIBRARY_PATH if needed).

I am guessing that Apache is running as a dedicated user (not your own UID for obvious security reasons), and that user does not have R in its path (other security step – by default Apache should not be able to run any executable on your system).

You’ll have to either allow Apache to run the system’s R, or have an R install only for your server (the later being the better if considering anything beyond a prototype to play with)

Leave a comment