spacemacs icon indicating copy to clipboard operation
spacemacs copied to clipboard

spacemacs/python-execute-file still uses default interpreter after switching python environments

Open alcazoid opened this issue 8 years ago • 19 comments

Description :octocat:

spacemacs/python-execute-file still uses default interpreter after changing env with pyenv.

Reproduction guide :beetle:

  • Start Emacs
  • Create a python file with the following code, printing path to interpreter:
import sys
print sys.executable
  • Switch python environment with SPC-m-V-a
  • Run the script with SPC-m-c-c

Observed behaviour: :eyes: :broken_heart: The script prints the path to the default python interpreter(first found in PATH). BTW, SPC-e-v shows that python-pycompile checker actually has correct path to the interpreter, it's just spacemacs/python-execute-file that doesn't know anything about it. Also, SPC-m-' starts REPL in correct env.

Expected behaviour: :heart: :smile: It is expected that the script will be executed with the interpreter from the env chosen, not the first interpreter found in PATH.

System Info :computer:

  • OS: darwin
  • Emacs: 25.1.1
  • Spacemacs: 0.200.7
  • Spacemacs branch: master (rev. c2774bc)
  • Graphic display: t
  • Distribution: spacemacs
  • Editing style: vim
  • Completion: ivy
  • Layers:
(javascript ivy emacs-lisp dash git markdown osx python themes-megapack ess syntax-checking)
  • System configuration features: NOTIFY ACL LIBXML2 ZLIB TOOLKIT_SCROLL_BARS NS MODULES

alcazoid avatar Jan 24 '17 21:01 alcazoid

Same issue with conda.el package for emacs, instead of pyenv.

alcazoid avatar Jan 24 '17 21:01 alcazoid

Having this same issue of not changing python interpreter.

chrislatorres avatar Jul 24 '17 02:07 chrislatorres

Are you sure that you are using pyenv and not pyvenv. The command SPC m V a is for pyvenv. Because it works with pyenv for me.

cpaulik avatar Aug 03 '17 11:08 cpaulik

Could it be related to the system itself? I get the same result on fedora. And I am indeed using pyenv.

In the terminal I get access to Python 3, which is what I want to use, but in emacs it defaults to version 2.7, which is not even the default on my system.

I have a hunch that a bunch of other stuff is also broken :(

Edit: I also get a different set of packages available. I can check this with the command import site; print(site.getsitepackages())

mikavilpas avatar May 12 '18 15:05 mikavilpas

@cpaulik you are correct, it's pyvenv

@sp3ctum not sure, I'm on macOS

alcazoid avatar May 14 '18 12:05 alcazoid

Same problem as the OP here (OS X, Spacemacs [email protected]). Spacemacs ignores the Python version set via pyvenv (SPC-m-V-w, pyvenv-workon) and just uses system's default.

In my case my venv is a Conda environment:

> /Users/frodera/miniconda3/envs/python37/bin/python --version                                                                                                         
Python 3.7.0

After setting the env via SPC-m-V-w, emacs reports that the path of that env is the first one listed in Spacemacs' PATH and it even lists it in the status bar. However, when running a python script via SPC-m-c-c it reverts to system's default (2.7). See attached image.

getenv "PATH" -> /Users/frodera/miniconda3/envs/python37/bin:/usr/local/opt/coreutils/...

screen shot 2018-08-05 at 11 05 18

frodera avatar Aug 05 '18 01:08 frodera

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] avatar Feb 29 '20 03:02 github-actions[bot]

same issue here with conda layer, please not close it.

tshu-w avatar May 23 '20 03:05 tshu-w

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] avatar Oct 17 '21 11:10 github-actions[bot]

active

tshu-w avatar Oct 17 '21 11:10 tshu-w

Is there any variable I can set to hardcode the python version it runs as a workaround?

dgealow avatar Nov 11 '21 00:11 dgealow

@dgealow maybe python-shell-interpreter

tshu-w avatar Nov 11 '21 02:11 tshu-w

Actually, I found the problem. In layers/+lang/python/packages.el, the function spacemacs/python-execute-file contains the line (compile-command (format "python %s" (file-name-nondirectory , which essentially means that it will always run whatever the default version of python is regardless of any spacemacs variables or loaded environment. It looks like there's a more robust fix in the works in the latest development branch (haven't tested it yet) but as a workaround I just found the file in my ~/.emacs.d/ directory. I changed that line to ...format "python3 %s"... since my default Python 3 installation is what I want 99% of the time. You might be able to precision-edit the line to read the python-shell-interpreter variable but I didn't feel like trying anything fancy. I'd recommend trying the dev version to anyone else looking for a solution.

dgealow avatar Nov 11 '21 02:11 dgealow

Closing now. Please always use develop branch.

lebensterben avatar Nov 12 '21 10:11 lebensterben

This issue also happen in develop, If I remember correctly

tshu-w avatar Nov 12 '21 12:11 tshu-w

Issue appears to probably be solved, at least for the use case of "I just want python 3". If no virtual environment is active, then it will use python-shell-interpreter to run the script--default ipython, overrideable with (setq-default python-shell-interpreter "python2") (or whatever python version you want) in dotspacemacs/user-config.

However, I can't verify that it works properly with virtual environments, because of a bug introduced into ipython just 3 days ago. Will report back as soon as that gets fixed!

dgealow avatar Feb 28 '22 02:02 dgealow

Alright, downgraded my ipython to test this out. It seems that if you have ipython installed in your virtual environment, it will use that version of ipython. However, if you don't, then it will use the system default ipython rather than the virual environment's python.

dgealow avatar Feb 28 '22 02:02 dgealow

We shouldn't support python 2.

So the issue should be considered fixed if it works as expected under python 3, with and without virtual environment.

lebensterben avatar Feb 28 '22 03:02 lebensterben

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] avatar May 01 '24 16:05 github-actions[bot]