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
.emacsfile?venvis a builtin implementation ofvirtualenvand the user guide forvirtualenvstates, "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 settingorg-babel-python-commandis enough to essentially activate that venv. (See the caveat in the above link.)org-babel-python-commandfor a single org-babel block using some keyword or something?:python /path/to/python/in/your/venv/python.3.13.