Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

setting `python-shell-interpreter-args' based on virtualenv #96

Open
spluque opened this issue Feb 6, 2020 · 0 comments
Open

setting `python-shell-interpreter-args' based on virtualenv #96

spluque opened this issue Feb 6, 2020 · 0 comments

Comments

@spluque
Copy link

spluque commented Feb 6, 2020

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant