auto-virtualenv
auto-virtualenv copied to clipboard
Automatically activate python virtualenv on Emacs
auto-virtualenv
Automatically activate python virtualenvs on emacs based on project name or .python-version file.
Installation
MELPA
auto-virtualenv is available on MELPA.
You can install auto-virtualenv with the following command.
M-x package-install [RET] auto-virtualenv [RET]
Manual
Clone this repository somewhere and add this directory to you
load-path.
Configuration
(require 'auto-virtualenv)
(add-hook 'python-mode-hook 'auto-virtualenv-set-virtualenv)
Optionally:
;; Activate on changing buffers
(add-hook 'window-configuration-change-hook 'auto-virtualenv-set-virtualenv)
;; Activate on focus in
(add-hook 'focus-in-hook 'auto-virtualenv-set-virtualenv)
With use-package
(use-package auto-virtualenv
:ensure t
:init
(use-package pyvenv
:ensure t)
:config
(add-hook 'python-mode-hook 'auto-virtualenv-set-virtualenv)
(add-hook 'projectile-after-switch-project-hook 'auto-virtualenv-set-virtualenv) ;; If using projectile
)
How it works
On auto-virtualenv-set-virtualenv, the hierarchy is scanned for a
special file, which is part of the list
auto-virtualenv-project-root-files. The list contains files which
indicate a project root, like a .git directory or a .dir-locals.el
file.
In order, files and paths to check for virtualenv to activate:
-
- Try path set from
auto-virtualenv-custom-virtualenv-pathvariable
- Try path set from
-
- Try path from
.auto-virtualenv-versionfile if it exists or
- Try path from
-
- Try name from
.python-versionfile if it exists or
- Try name from
-
- Try
.venvor.virtualenvorvenvdir in the root of project
- Try
-
- Try finding a virtualenv with the same name as Project in virtualenv dirs set in
auto-virtualenv-dir.
- Try finding a virtualenv with the same name as Project in virtualenv dirs set in
-
- Try common virtualenv paths in home dir:
~/.virtulenvsand~/.pyenv/versions
- Try common virtualenv paths in home dir:
The found environment is finally activated using pyvenv-activate.
License
View official GNU site http://www.gnu.org/licenses/gpl.html.
