humitos / emacs-configuration

All my Emacs' configuration
25 stars 4 forks source link

Activate virtualenv only if needed #10

Open humitos opened 7 years ago

humitos commented 7 years ago

I been using this chunk of code in my .dir-locals.el to activate an specific virtualenv once one of the file of this project is visited.

((python-mode
  . (
     ;; use our own virtualenv for this project
     (pyvenv-workon . "mozio-ondemand")
    )
  ))

The problem with this is that activating a venv takes 1 or 2 seconds, and sometimes I'm opening a file that is inside the same project, so there is no need to re-activate it.

On the other hand, another problem is at emacs startup when a bunch of files are opened and then it tries to activate a lot of venv at the same moment.

To avoid this, I'm creating an alias for the pyvenv-workon to check the emacs start time and also if the venv is already activated.

;; stolen from here
;; http://gnuvola.org/software/personal-elisp/dist/lisp/diversions/emacs-uptime.el
(defun pyvenv-workon-alias ()
  "Activates the virtualenv only if we started Emacs more than 30 seconds ago"
  (if (> (- (car (cdr (current-time))) (car (cdr emacs-start-time))) 30)
      (message "Activate virtualenv")
    (message "Do not activate virtualenv")))
humitos commented 7 years ago

I found this plugin that maybe does the magic for me: https://github.com/robert-zaremba/auto-virtualenvwrapper.el