Skip to content

Instantly share code, notes, and snippets.

@ieure
Created May 21, 2012 20:18
Show Gist options
  • Save ieure/2764399 to your computer and use it in GitHub Desktop.
Save ieure/2764399 to your computer and use it in GitHub Desktop.
(defun python-find-python-command (&optional root)
"Return the python interpreter to use for ROOT."
(let ((env-root (local-path
(locate-dominating-file
(or root default-directory) "bin/python"))))
(or (when env-root (concat env-root "bin/python"))
python-python-command)))
(defun python-find-project-root (&optional start)
"Return the root of this Python project."
(let ((start (or start default-directory)))
(or (locate-dominating-file start "setup.py")
(locate-dominating-file start ".git"))))
(defmacro with-virtualenv-python (&rest body)
"Locate a local Python interpreter and make it available in the body."
`(let* ((local-python-command (python-find-python-command))
(python-command local-python-command)
(python-python-command local-python-command)
(test-case-python-executable local-python-command))
,@body))
(defun run-virtualenv-python (new)
(interactive "P")
(with-virtualenv-python
(let ((default-directory (python-find-project-root)))
(run-python python-python-command nil new))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment