Set Python project root in Emacs?

依然范特西╮ 提交于 2019-12-04 13:38:48

OK this should get you started

(defvar my-python-shell-dir-setup-code 
  "import os
home = os.path.expanduser('~')
while os.path.isfile('__init__.py') and (os.getcwd() != home):
    os.chdir('..')
del os")

(defun my-python-shell-dir-setup ()
  (let ((process (get-buffer-process (current-buffer))))
    (python-shell-send-string my-python-shell-dir-setup-code process)
    (message "Setup project path")))

(add-hook 'inferior-python-mode-hook 'my-python-shell-dir-setup)

Here is what we are doing my-python-shell-dir-setup-code is simple python code to find project-dir and set it (it quick and dirty you may want to modify it according to your needs). Then we add a inferior-python-mode-hook (i.e. my-shell-dir-setup) to execute the python code in out inferior shell whenever the shell is created.

I'm not familiar with emacs-for-python, but finding the path to the root of your project is straightforward.

Projectile provides a function projectile-project-root. You could call this in your mod1.py buffer, and it will return "/path/to/myproject". This assumes that projectile can recognise your project root, which will be fine if you've using some sort of VCS.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!