org-babel :session
と pyvenv を同時に使用して、複数のvirtualenvで作業しています>。
Note that scratchenv-SNmPgRMB
is the name of one of my personal environments created using pipenv replace it with your virtualenv environment name.
#+BEGIN_SRC elisp :session venv-scratch
(pyvenv-workon "scratchenv-SNmPgRMB")
#+END_SRC
#+RESULTS:
#+BEGIN_SRC sh :session venv-scratch
echo $VIRTUAL_ENV
which python
#+END_SRC
#+RESULTS:
: /home/costrouc/.virtualenvs/scratchenv-SNmPgRMB
: /home/costrouc/.virtualenvs/scratchenv-SNmPgRMB/bin/python
#+BEGIN_SRC python :results output :session venv-scratch
import sys
print('\n'.join(sys.path))
#+END_SRC
#+RESULTS:
:
: /home/costrouc/.virtualenvs/scratchenv-SNmPgRMB/lib/python36.zip
: /home/costrouc/.virtualenvs/scratchenv-SNmPgRMB/lib/python3.6
: /home/costrouc/.virtualenvs/scratchenv-SNmPgRMB/lib/python3.6/lib-dynload
: /usr/lib/python3.6
: /home/costrouc/.virtualenvs/scratchenv-SNmPgRMB/lib/python3.6/site-packages
Read paragraph if you need multiple concurrent virtualenvs. One important thing to note about this approach is that you must run the shell and python interpreter once before adding another session with a different virtual environment. The reasoning for this is that when org-babel starts a session for sh
and python
it launches an interpreter. This means that at a minimum you need to run these three blocks before starting a new virtualenv session.