簡體   English   中英

如何讓emacs-jedi使用項目特定的virtualenvs

[英]How can I make emacs-jedi use project-specific virtualenvs

我想emacs-jedi檢測我在不同項目中編輯文件的時間,並使用相應的virtualenv(如果可用)。 按照慣例,我的virtualenvs與我的項目同名。 它們位於$HOME/.virtualenvs/

我找到了kenobi.el,但它假設在項目根目錄的bin目錄中找到了virtualenvs。 它還有一些我根本不需要的其他功能。

在kenobi.el的啟發下,我為jedi編寫了以下初始化。 它工作得很好,但並不完美。

如果我從項目中導入庫A ,則A導入B 我能夠跳到A定義的定義,但是一旦出現,我就無法繼續跳到B定義。

我的初始化:

(defun project-directory (buffer-name)
  (let ((git-dir (file-name-directory buffer-name)))
    (while (and (not (file-exists-p (concat git-dir ".git")))
                git-dir)
      (setq git-dir
            (if (equal git-dir "/")
                nil
              (file-name-directory (directory-file-name git-dir)))))
    git-dir))

(defun project-name (buffer-name)
  (let ((git-dir (project-directory buffer-name)))
    (if git-dir
        (file-name-nondirectory
         (directory-file-name git-dir))
      nil)))

(defun virtualenv-directory (buffer-name)
  (let ((venv-dir (expand-file-name
                   (concat "~/.virtualenvs/" (project-name buffer-name)))))
    (if (and venv-dir (file-exists-p venv-dir))
        venv-dir
      nil)))    

(defun jedi-setup-args ()
  (let ((venv-dir (virtualenv-directory buffer-file-name)))
    (when venv-dir
      (set (make-local-variable 'jedi:server-args) (list "--virtual-env" venv-dir)))))

(setq jedi:setup-keys t)
(setq jedi:complete-on-dot t)
(add-hook 'python-mode-hook 'jedi-setup-args)
(add-hook 'python-mode-hook 'jedi:setup)

我如何初始化jedi有什么問題?

我現在已經確定了一個使用virtualenvwrapper ELPA包來激活virtualenvs的解決方案,允許emacs-jedi從VIRTUAL_ENV環境變量中獲取virtualenv路徑。

這是一個完整的,有效的emacs-jedi初始化:

(defun project-directory (buffer-name)
  "Return the root directory of the project that contain the
given BUFFER-NAME. Any directory with a .git or .jedi file/directory
is considered to be a project root."
  (interactive)
  (let ((root-dir (file-name-directory buffer-name)))
    (while (and root-dir
                (not (file-exists-p (concat root-dir ".git")))
                (not (file-exists-p (concat root-dir ".jedi"))))
      (setq root-dir
            (if (equal root-dir "/")
                nil
              (file-name-directory (directory-file-name root-dir)))))
    root-dir))

(defun project-name (buffer-name)
  "Return the name of the project that contain the given BUFFER-NAME."
  (let ((root-dir (project-directory buffer-name)))
    (if root-dir
        (file-name-nondirectory
         (directory-file-name root-dir))
      nil)))

(defun jedi-setup-venv ()
  "Activates the virtualenv of the current buffer."
  (let ((project-name (project-name buffer-file-name)))
    (when project-name (venv-workon project-name))))

(setq jedi:setup-keys t)
(setq jedi:complete-on-dot t)
(add-hook 'python-mode-hook 'jedi-setup-venv)
(add-hook 'python-mode-hook 'jedi:setup)

請記住,您必須先安裝virtualenvwrapper。

閱讀virtualenvwrapper文檔 ,了解自動激活項目虛擬環境的另一種方法。 簡而言之,您可以在項目的根目錄中創建.dir-locals.el文件,其中包含以下內容:

((python-mode . ((project-venv-name . "myproject-env"))))

"myproject-env"更改為virtualenv的名稱,並使用python-mode鈎子激活virtualenvironment:

(add-hook 'python-mode-hook (lambda ()
                              (hack-local-variables)
                              (venv-workon project-venv-name)))
(add-hook 'python-mode-hook 'jedi:setup)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM