3

I've tried to setup a python virtual environment following the top answer here: https://emacs.stackexchange.com/questions/24267/using-org-babel-to-enable-virtualenv/38047 I tried activating the virtual environment using two methods: source $PYENV/bin/activate and pyvenv-workon $PYENV. I expect to see the output of which python and which pip refer back to this virtual environment, but it uses the computer's primary python installation. The path to $VIRTUAL_ENV is correct, however. Similarly, the final code block does not use the virtual environment as expected either. Any ideas what I'm missing?

    #+BEGIN_SRC bash :dir ~/ :results drawer   
pwd   
python3 -m venv py3_venv 
    #+END_SRC

    #+RESULTS: 
:RESULTS: 
/home/jj 
:END:

    #+BEGIN_SRC bash :dir ~/ :results raw drawer 
source py3_venv/bin/activate
    #+END_SRC

    #+RESULTS: 
:RESULTS: 
:END:

    https://emacs.stackexchange.com/questions/24267/using-org-babel-to-enable-virtualenv/38047
    #+BEGIN_SRC elisp :session venv-py3 :results raw drawer 
(pyvenv-workon "py3_venv")
    #+END_SRC

    #+RESULTS: 
:RESULTS: nil 
:END:

    #+BEGIN_SRC sh :session venv-py3 
echo $VIRTUAL_ENV 
echo $PYTHONPATH 
which python 
which pip
    #+END_SRC

    #+RESULTS: 
| /home/jj/.virtualenvs/py3_venv 
| 
|                                
| 
| /home/jj/anaconda3/bin/python  
| 
| /home/jj/anaconda3/bin/pip     |

    #+BEGIN_SRC python :results output :session venv-py3 
import sys \
print('\n'.join(sys.path))
    #+END_SRC

        #+RESULTS: 
: /home/jj/anaconda3/lib/python37.zip 
: /home/jj/anaconda3/lib/python3.7 
: /home/jj/anaconda3/lib/python3.7/lib-dynload 
: /home/jj/anaconda3/lib/python3.7/site-packages
debby
  • 71
  • 1
  • 8
  • Have you tried [setting org-babel-python-command](https://emacs.stackexchange.com/a/28186/8811) to the desired python executable in your `.emacs` file? – unutbu Jan 01 '19 at 04:08
  • 1
    [`venv` is a builtin implementation of `virtualenv`](https://stackoverflow.com/a/29950604/190597) and the [user guide for `virtualenv` states](https://virtualenv.pypa.io/en/stable/userguide/), "If you directly run a script or the python interpreter from the virtualenv’s bin/ directory (e.g. path/to/ENV/bin/pip or /path/to/ENV/bin/python-script.py) then **sys.path will automatically be set to use the Python libraries associated with the virtualenv**" (my emphasis). So merely setting `org-babel-python-command` is enough to essentially activate that venv. (See the caveat in the above link.) – unutbu Jan 01 '19 at 04:27
  • This did the trick. Thank you! – debby Jan 14 '19 at 06:04

0 Answers0