using python virtual env in R

python, r, virtualenv

Solution

Use the "activate" bash script before running R, so that the R process inherits the changed environment variables.

$ source myvirtualenv/bin/activate

$ R

Now rPython should be able to use the packages in your virtualenv.

Works for me. May behave strangely if the Python version you made the virtualenv with is different to the one rPython links into the R process.

Problem

I am using 'rPython' package for calling python within R but I am unable to make R refer to my python's virtual environment. In R, I have tried using ``` system('. /home/username/Documents/myenv/env/bin/activate') ``` but after running the above my python library path does not change (which I check via `python.exec(print sys.path)`). When I run ``` python.exec('import nltk') ``` I am thrown the error: Error in python.exec("import nltk") : No module named nltk although it is there in my virtual env. I am using R 3.0.2, Python 2.7.4 on Ubuntu 13.04. Also, I know I can change the python library path from within R by using ``` python.exec("sys.path='\your\path'") ``` but I don't want this to be entered manually over and over again whenever a new python package is installed. Thanks in advance!

Original source