pyvenv
pyvenv copied to clipboard
setting `python-shell-interpreter-args' based on virtualenv
I have python-shell-interpreter
set to jupyter
, and python-shell-interpreter-args
to:
console --kernel python3 --simple-prompt
I have jupyter and a python3 kernel installed in several virtualenvs. Unfortunately, activating the virtualenv with pyvenv-workon myvenv
runs the python3
kernel from the main environment (not myvenv
).
This is what elpy-config
shows with myvenv
activated:
Emacs.............: 26.1
Elpy..............: 1.32.0
Virtualenv........: (/home/joe/.virtualenvs/myvenv/)
Interactive Python: jupyter 4.6.1 (/home/joe/.virtualenvs/myvenv/bin/jupyter)
RPC virtualenv....: elpy-rpc-venv (/home/joe/.virtualenvs/elpy-rpc-venv)
Python...........: python3 3.7.4 (/home/joe/.virtualenvs/elpy-rpc-venv/bin/python3)
Jedi.............: 0.16.0
Rope.............: 0.16.0
Autopep8.........: 1.5
Yapf.............: 0.29.0
Black............: 19.10b0
Syntax checker....: flake8 (/usr/bin/flake8)
so it runs the right jupyter
from myvenv
, but presumably using the python3
kernel from the main environment as the sys.path
shown is not from myvenv
. Same thing occurs in a terminal outside Emacs.
I thought I could get around the problem by creating a kernel with the same as the virtualenv; i.e. create a kernel named myvenv
in the example above. Then, use pyvenv-post-activate-hooks
and pyvenv-post-deactivate-hooks
to set python-shell-interpreter-args
to call the appropriate kernel:
(defun sl/pyvenv-toggle-python-shell-interpreter-args ()
"toggle `python-shell-interpreter-args'"
(setq python-shell-interpreter-args
(format "console --kernel %s --simple-prompt"
(or pyvenv-virtual-env-name "python3"))))
(add-hook 'pyvenv-post-activate-hooks
'sl/pyvenv-toggle-python-shell-interpreter-args)
(add-hook 'pyvenv-post-deactivate-hooks
'sl/pyvenv-toggle-python-shell-interpreter-args)
The post-activate hook works as expected, but not the post-deactivate hook, perhaps as per its docstring note: "‘`pyvenv-virtual-env’ is still set."
Any tips or alternative solutions?