gpt4 book ai didi

python - 如何让 emacs-jedi 使用项目特定的 virtualenvs

转载 作者:太空狗 更新时间:2023-10-29 17:46:43 25 4
gpt4 key购买 nike

我希望 emacs-jedi 能够检测到我在不同项目中编辑文件的时间,并在可用时使用相应的 virtualenv。按照惯例,我的 virtualenvs 与我的项目同名。它们位于 $HOME/.virtualenvs/

我找到了 kenobi.el但它假定 virtualenvs 位于项目根目录的 bin 目录中。它还有一些我根本不需要的其他功能。

在 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 的解决方案用于激活 virtualenvs 的 ELPA 包,允许 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 documentation用于自动激活项目虚拟环境的替代方法。简而言之,您可以在项目的根目录中创建一个 .dir-locals.el 文件,内容如下:

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

"myproject-env" 更改为您的虚拟环境的名称,并使用 python-mode Hook 激活虚拟环境:

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

关于python - 如何让 emacs-jedi 使用项目特定的 virtualenvs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21246218/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com