syl20bnr / spacemacs

A community-driven Emacs distribution - The best editor is neither Emacs nor Vim, it's Emacs *and* Vim!
http://spacemacs.org
GNU General Public License v3.0
23.64k stars 4.89k forks source link

Python mode hook for pyenv-mode can overwrite Python interpreter set by virtualenv #13747

Closed swinkels closed 3 months ago

swinkels commented 4 years ago

Description :octocat:

Suppose you have a Python project that 1) has a .python-version file (which specifies which Python interpreter should be used) and 2) has a virtualenv. If you open a Python file after you have activated the virtualenv, and then start a Python REPL, the Python REPL will use the Python interpreted specified by file .python-version and not the one specified by virtualenv.

This is caused by the fact that the Python mode hook pyenv-mode-set-local-version will overwrite a Python interpreter that is set by pyvenv-activate (and which is stored in variable python-shell-virtualenv-root).

Reproduction guide :beetle:

Observed behaviour: :eyes: :broken_heart: The REPL uses the Python version specified in the .python-version file, not the one specified by the virtualenv.

Expected behaviour: :heart: :smile: The REPL uses the Python interpreter specified by the active virtualenv.

System Info :computer:

Backtrace :paw_prints:

The issue does not result in a backtrace.

swinkels commented 4 years ago

As mentioned, the hook pyenv-mode-set-local-version overwrites python-shell-virtualenv-root, which is set by pyvenv-activate. IMHO (but I can be wrong) the interpreter of the active virtualenv should have precedence over the one that is specified in a .python-version file. If that is indeed the case, pyenv-mode-set-local-version should do nothing if pyvenv-activate is not nil. If you agree, I can make a pull request for this.

swinkels commented 4 years ago

There is a workaround: just (re)activate the virtualenv in the file that you just opened. However, fixing this would be better :)

EDIT: in hindsight this is a bad workaround as python-shell-virtualenv-root is reset every time you open a new file from the same project.

alexey0308 commented 4 years ago

Can it be solved with the pyenv plugin https://github.com/pyenv/pyenv-virtualenv ? https://github.com/pythonic-emacs/pyenv-mode/issues/10 As i understand, it addresses the issue of management of both, pyenv and virtualenv.

swinkels commented 4 years ago

It might be solved by using pyenv-virtualenv en pyenv-mode, especially if you use pyenv-virtuelenv to manage your virtualenvs :). However, I use tox to manage my virtualenvs, which is also a valid workflow.

IMO the underlying question is whether pyenv-mode should override the virtualenv settings by pyvenv, that is, the value of variable python-shell-virtualenv-root.

github-actions[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Please let us know if this issue is still valid!

github-actions[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Please let us know if this issue is still valid!

github-actions[bot] commented 6 months ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Please let us know if this issue is still valid!