Auto-activate local pyenv version

This commit is contained in:
Fredrik Bergroth 2015-12-07 13:58:36 +01:00 committed by syl20bnr
parent c9c3d685ea
commit 577e7b2d86
4 changed files with 40 additions and 4 deletions

View File

@ -11,6 +11,7 @@
- [[Test runner][Test runner]]
- [[Anaconda dependencies][Anaconda dependencies]]
- [[Automatic buffer formatting on save][Automatic buffer formatting on save]]
- [[Automatic activation of local pyenv version][Automatic activation of local pyenv version]]
- [[autoflake][autoflake]]
- [[pylookup][pylookup]]
- [[Key Bindings][Key Bindings]]
@ -86,6 +87,17 @@ To enable automatic buffer formatting on save with [[https://github.com/google/
(python :variables python-enable-yapf-format-on-save t)))
#+END_SRC
** Automatic activation of local pyenv version
A project-specific pyenv version may be written to a file called
=.python-version= using the [[https://github.com/yyuu/pyenv/blob/master/COMMANDS.md#pyenv-local][pyenv local]] command.
Spacemacs can search in parent directories for this file, and automatically set
the pyenv version. The behavior can be set with the variable
=python-auto-set-local-pyenv-version= to:
- =on-visit= (default) set the version when you visit a python buffer,
- =on-project-switch= set the version when you switch projects,
- =nil= to disable.
** autoflake
To be able to suppress unused imports easily, install [[https://github.com/myint/autoflake][autoflake]]:

View File

@ -24,3 +24,8 @@
(defvar python-fill-column 79
"Fill column value for python buffers")
(defvar python-auto-set-local-pyenv-version 'on-visit
"Automatically set pyenv version from \".python-version\".
Possible values are `on-visit', `on-project-switch' or `nil'.")

View File

@ -43,3 +43,15 @@
(shell-quote-argument (buffer-file-name))))
(revert-buffer t t t))
(message "Error: Cannot find autoflake executable.")))
(defun pyenv-mode-set-local-version ()
"Set pyenv version from \".python-version\" by looking in parent directories."
(interactive)
(-when-let (root-path (locate-dominating-file default-directory ".python-version"))
(let* ((file-path (expand-file-name ".python-version" root-path))
(version (with-temp-buffer
(insert-file-contents-literally file-path)
(current-word))))
(if (member version (pyenv-mode-versions))
(pyenv-mode-set version)
(message "pyenv: version `%s' is not installed (set by %s)" version file-path)))))

View File

@ -82,10 +82,17 @@
(defun python/init-pyenv-mode ()
(use-package pyenv-mode
:defer t
:init (progn
(spacemacs/set-leader-keys-for-major-mode 'python-mode
"vs" 'pyenv-mode-set
"vu" 'pyenv-mode-unset))))
:commands (pyenv-mode-versions)
:init
(progn
(pcase python-auto-set-local-pyenv-version
(`on-visit
(add-hook 'python-mode-hook 'pyenv-mode-set-local-version))
(`on-project-switch
(add-hook 'projectile-after-switch-project-hook 'pyenv-mode-set-local-version)))
(spacemacs/set-leader-keys-for-major-mode 'python-mode
"vs" 'pyenv-mode-set
"vu" 'pyenv-mode-unset))))
(defun python/init-pyvenv ()
(use-package pyvenv