.dir-locals.el 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. ;; project root folder is where the `.dir-locals.el' is located
  53. (eval . (setq-local
  54. prj-root (locate-dominating-file default-directory ".dir-locals.el")))
  55. (eval . (setq-local
  56. python-environment-directory (expand-file-name "./local" prj-root)))
  57. ;; use 'py3' enviroment as default
  58. (eval . (setq-local
  59. python-environment-default-root-name "py3"))
  60. (eval . (setq-local
  61. python-shell-virtualenv-root
  62. (expand-file-name python-environment-default-root-name python-environment-directory)
  63. ))
  64. (eval . (setq-local
  65. python-shell-interpreter
  66. (expand-file-name "bin/python" python-shell-virtualenv-root)))
  67. ))
  68. (makefile-gmake-mode
  69. . ((indent-tabs-mode . t)
  70. ))
  71. (yaml-mode
  72. . (
  73. ;; flycheck should use the local py3 environment
  74. (eval . (setq-local
  75. flycheck-yaml-yamllint-executable
  76. (expand-file-name "bin/yamllint" python-shell-virtualenv-root)))
  77. (eval . (setq-local
  78. flycheck-yamllintrc
  79. (expand-file-name ".yamllint.yml" prj-root)))
  80. (flycheck-checker . yaml-yamllint)
  81. ))
  82. (python-mode
  83. . ((indent-tabs-mode . nil)
  84. (eval . (setq-local
  85. python-environment-virtualenv
  86. (list (expand-file-name "bin/virtualenv" python-shell-virtualenv-root)
  87. ;;"--system-site-packages"
  88. "--quiet")))
  89. (eval . (setq-local
  90. pylint-command
  91. (expand-file-name "bin/pylint" python-shell-virtualenv-root)))
  92. ;; pylint will find the '.pylintrc' file next to the CWD
  93. ;; https://pylint.readthedocs.io/en/latest/user_guide/run.html#command-line-options
  94. (eval . (setq-local
  95. flycheck-pylintrc ".pylintrc"))
  96. ;; flycheck & other python stuff should use the local py3 environment
  97. (eval . (setq-local
  98. flycheck-python-pylint-executable python-shell-interpreter))
  99. ;; use 'M-x jedi:show-setup-info' and 'M-x epc:controller' to inspect jedi server
  100. ;; https://tkf.github.io/emacs-jedi/latest/#jedi:environment-root -- You
  101. ;; can specify a full path instead of a name (relative path). In that case,
  102. ;; python-environment-directory is ignored and Python virtual environment
  103. ;; is created at the specified path.
  104. (eval . (setq-local jedi:environment-root python-shell-virtualenv-root))
  105. ;; https://tkf.github.io/emacs-jedi/latest/#jedi:server-command
  106. (eval .(setq-local
  107. jedi:server-command
  108. (list python-shell-interpreter
  109. jedi:server-script)
  110. ))
  111. ;; jedi:environment-virtualenv --> see above 'python-environment-virtualenv'
  112. ;; is set buffer local! No need to setup jedi:environment-virtualenv:
  113. ;;
  114. ;; Virtualenv command to use. A list of string. If it is nil,
  115. ;; python-environment-virtualenv is used instead. You must set non-nil
  116. ;; value to jedi:environment-root in order to make this setting work.
  117. ;;
  118. ;; https://tkf.github.io/emacs-jedi/latest/#jedi:environment-virtualenv
  119. ;;
  120. ;; (eval . (setq-local
  121. ;; jedi:environment-virtualenv
  122. ;; (list (expand-file-name "bin/virtualenv" python-shell-virtualenv-root)
  123. ;; ;;"--python"
  124. ;; ;;"/usr/bin/python3.4"
  125. ;; )))
  126. ;; jedi:server-args
  127. )))