.dir-locals.el 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. ;;; .dir-locals.el
  2. ;;
  3. ;; If you get ``*** EPC Error ***`` (even after a jedi:install-server) in your
  4. ;; emacs session, mostly you have jedi-mode enabled but the python enviroment is
  5. ;; missed. The python environment has to be next to the
  6. ;; ``<repo>/.dir-locals.el`` in::
  7. ;;
  8. ;; ./local/py3
  9. ;;
  10. ;; In Emacs, some buffer locals are referencing the project environment:
  11. ;;
  12. ;; - prj-root --> <repo>/
  13. ;; - python-environment-directory --> <repo>/local
  14. ;; - python-environment-default-root-name --> py3
  15. ;; - python-shell-virtualenv-root --> <repo>/local/py3
  16. ;; When this variable is set with the path of the virtualenv to use,
  17. ;; `process-environment' and `exec-path' get proper values in order to run
  18. ;; shells inside the specified virtualenv, example::
  19. ;; (setq python-shell-virtualenv-root "/path/to/env/")
  20. ;;
  21. ;; To setup such an environment build target 'pyenv' or 'pyenvinstall'::
  22. ;;
  23. ;; $ make pyenvinstall
  24. ;;
  25. ;; Alternatively create the virtualenv, source it and install jedi + epc
  26. ;; (required by `emacs-jedi <https://tkf.github.io/emacs-jedi>`_)::
  27. ;;
  28. ;; $ python -m venv ./local/py3
  29. ;; ...
  30. ;; $ source ./local/py3/bin/activate
  31. ;; (py3)$ # now install into the activated 'py3' environment ..
  32. ;; (py3)$ pip install jedi epc
  33. ;; ...
  34. ;;
  35. ;; Here is what also I found useful to add to my .emacs::
  36. ;;
  37. ;; (global-set-key [f6] 'flycheck-mode)
  38. ;; (add-hook 'python-mode-hook 'my:python-mode-hook)
  39. ;;
  40. ;; (defun my:python-mode-hook ()
  41. ;; (add-to-list 'company-backends 'company-jedi)
  42. ;; (require 'jedi-core)
  43. ;; (jedi:setup)
  44. ;; (define-key python-mode-map (kbd "C-c C-d") 'jedi:show-doc)
  45. ;; (define-key python-mode-map (kbd "M-.") 'jedi:goto-definition)
  46. ;; (define-key python-mode-map (kbd "M-,") 'jedi:goto-definition-pop-marker)
  47. ;; )
  48. ;;
  49. ((nil
  50. . ((fill-column . 80)
  51. (indent-tabs-mode . nil)
  52. ))
  53. (makefile-gmake-mode
  54. . ((indent-tabs-mode . t)
  55. ))
  56. (python-mode
  57. . ((indent-tabs-mode . nil)
  58. ;; project root folder is where the `.dir-locals.el' is located
  59. (eval . (setq-local
  60. prj-root (locate-dominating-file default-directory ".dir-locals.el")))
  61. (eval . (setq-local
  62. python-environment-directory (expand-file-name "./local" prj-root)))
  63. ;; use 'py3' enviroment as default
  64. (eval . (setq-local
  65. python-environment-default-root-name "py3"))
  66. (eval . (setq-local
  67. python-shell-virtualenv-root
  68. (concat python-environment-directory
  69. "/"
  70. python-environment-default-root-name)))
  71. ;; python-shell-virtualenv-path is obsolete, use python-shell-virtualenv-root!
  72. ;; (eval . (setq-local
  73. ;; python-shell-virtualenv-path python-shell-virtualenv-root))
  74. (eval . (setq-local
  75. python-shell-interpreter
  76. (expand-file-name "bin/python" python-shell-virtualenv-root)))
  77. (eval . (setq-local
  78. python-environment-virtualenv
  79. (list (expand-file-name "bin/virtualenv" python-shell-virtualenv-root)
  80. ;;"--system-site-packages"
  81. "--quiet")))
  82. (eval . (setq-local
  83. pylint-command
  84. (expand-file-name "bin/pylint" python-shell-virtualenv-root)))
  85. ;; pylint will find the '.pylintrc' file next to the CWD
  86. ;; https://pylint.readthedocs.io/en/latest/user_guide/run.html#command-line-options
  87. (eval . (setq-local
  88. flycheck-pylintrc ".pylintrc"))
  89. ;; flycheck & other python stuff should use the local py3 environment
  90. (eval . (setq-local
  91. flycheck-python-pylint-executable python-shell-interpreter))
  92. ;; use 'M-x jedi:show-setup-info' and 'M-x epc:controller' to inspect jedi server
  93. ;; https://tkf.github.io/emacs-jedi/latest/#jedi:environment-root -- You
  94. ;; can specify a full path instead of a name (relative path). In that case,
  95. ;; python-environment-directory is ignored and Python virtual environment
  96. ;; is created at the specified path.
  97. (eval . (setq-local jedi:environment-root python-shell-virtualenv-root))
  98. ;; https://tkf.github.io/emacs-jedi/latest/#jedi:server-command
  99. (eval .(setq-local
  100. jedi:server-command
  101. (list python-shell-interpreter
  102. jedi:server-script)
  103. ))
  104. ;; jedi:environment-virtualenv --> see above 'python-environment-virtualenv'
  105. ;; is set buffer local! No need to setup jedi:environment-virtualenv:
  106. ;;
  107. ;; Virtualenv command to use. A list of string. If it is nil,
  108. ;; python-environment-virtualenv is used instead. You must set non-nil
  109. ;; value to jedi:environment-root in order to make this setting work.
  110. ;;
  111. ;; https://tkf.github.io/emacs-jedi/latest/#jedi:environment-virtualenv
  112. ;;
  113. ;; (eval . (setq-local
  114. ;; jedi:environment-virtualenv
  115. ;; (list (expand-file-name "bin/virtualenv" python-shell-virtualenv-root)
  116. ;; ;;"--python"
  117. ;; ;;"/usr/bin/python3.4"
  118. ;; )))
  119. ;; jedi:server-args
  120. )))