2017-06-08 11:47:56 +02:00
|
|
|
;;; lang/python/config.el -*- lexical-binding: t; -*-
|
2015-06-15 09:06:10 +02:00
|
|
|
|
2019-02-28 04:10:35 -05:00
|
|
|
(defvar +python-ipython-repl-args '("-i" "--simple-prompt" "--no-color-info")
|
2018-12-05 14:31:02 -05:00
|
|
|
"CLI arguments to initialize ipython with when `+python/open-ipython-repl' is
|
|
|
|
called.")
|
|
|
|
|
2019-02-28 04:10:35 -05:00
|
|
|
(defvar +python-jupyter-repl-args '("--simple-prompt")
|
2018-12-05 14:31:02 -05:00
|
|
|
"CLI arguments to initialize 'jupiter console %s' with when
|
|
|
|
`+python/open-ipython-repl' is called.")
|
|
|
|
|
2017-09-26 01:02:42 +02:00
|
|
|
|
|
|
|
;;
|
2018-09-07 19:36:16 -04:00
|
|
|
;; Packages
|
2017-09-26 01:02:42 +02:00
|
|
|
|
2019-07-23 12:44:03 +02:00
|
|
|
(use-package! python
|
2018-05-25 00:46:11 +02:00
|
|
|
:defer t
|
2015-06-15 09:06:10 +02:00
|
|
|
:init
|
2017-02-19 18:57:16 -05:00
|
|
|
(setq python-environment-directory doom-cache-dir
|
2018-10-06 00:25:50 -04:00
|
|
|
python-indent-guess-indent-offset-verbose nil)
|
2016-05-01 01:10:30 -04:00
|
|
|
:config
|
2019-02-18 01:56:38 -05:00
|
|
|
(set-repl-handler! 'python-mode #'+python/open-repl)
|
2019-05-17 21:01:39 -04:00
|
|
|
(set-docsets! 'python-mode "Python 3" "NumPy" "SciPy")
|
2017-03-04 20:51:35 -05:00
|
|
|
|
2018-06-16 19:32:25 +02:00
|
|
|
(set-pretty-symbols! 'python-mode
|
|
|
|
;; Functional
|
|
|
|
:def "def"
|
|
|
|
:lambda "lambda"
|
|
|
|
;; Types
|
|
|
|
:null "None"
|
|
|
|
:true "True" :false "False"
|
|
|
|
:int "int" :str "str"
|
|
|
|
:float "float"
|
|
|
|
:bool "bool"
|
|
|
|
:tuple "tuple"
|
|
|
|
;; Flow
|
|
|
|
:not "not"
|
|
|
|
:in "in" :not-in "not in"
|
|
|
|
:and "and" :or "or"
|
|
|
|
:for "for"
|
|
|
|
:return "return" :yield "yield")
|
|
|
|
|
2019-07-22 00:38:23 +02:00
|
|
|
;; Stop the spam!
|
|
|
|
(setq python-indent-guess-indent-offset-verbose nil)
|
|
|
|
|
2019-02-21 16:08:27 -05:00
|
|
|
(when (featurep! +lsp)
|
2019-03-13 19:22:58 -04:00
|
|
|
(add-hook 'python-mode-local-vars-hook #'lsp!))
|
2019-02-21 16:08:27 -05:00
|
|
|
|
2019-07-22 00:38:33 +02:00
|
|
|
;; Default to Python 3. Prefer the versioned Python binaries since some
|
|
|
|
;; systems stupidly make the unversioned one point at Python 2.
|
|
|
|
(when (and (executable-find "python3")
|
|
|
|
(string= python-shell-interpreter "python"))
|
|
|
|
(setq python-shell-interpreter "python3"))
|
|
|
|
|
2019-07-28 02:16:58 +02:00
|
|
|
(add-hook! 'python-mode-hook
|
2019-07-22 00:37:34 +02:00
|
|
|
(defun +python-use-correct-flycheck-executables-h ()
|
|
|
|
"Use the correct Python executables for Flycheck."
|
|
|
|
(let ((executable python-shell-interpreter))
|
|
|
|
(save-excursion
|
2019-08-06 14:50:42 -04:00
|
|
|
(goto-char (point-min))
|
2019-07-22 00:37:34 +02:00
|
|
|
(save-match-data
|
|
|
|
(when (or (looking-at "#!/usr/bin/env \\(python[^ \n]+\\)")
|
|
|
|
(looking-at "#!\\([^ \n]+/python[^ \n]+\\)"))
|
|
|
|
(setq executable (substring-no-properties (match-string 1))))))
|
|
|
|
;; Try to compile using the appropriate version of Python for
|
|
|
|
;; the file.
|
|
|
|
(setq-local flycheck-python-pycompile-executable executable)
|
|
|
|
;; We might be running inside a virtualenv, in which case the
|
|
|
|
;; modules won't be available. But calling the executables
|
|
|
|
;; directly will work.
|
|
|
|
(setq-local flycheck-python-pylint-executable "pylint")
|
|
|
|
(setq-local flycheck-python-flake8-executable "flake8"))))
|
|
|
|
|
2018-07-31 14:02:34 +02:00
|
|
|
(define-key python-mode-map (kbd "DEL") nil) ; interferes with smartparens
|
2018-10-06 00:27:52 -04:00
|
|
|
(sp-local-pair 'python-mode "'" nil
|
|
|
|
:unless '(sp-point-before-word-p
|
|
|
|
sp-point-after-word-p
|
|
|
|
sp-point-before-same-p))
|
2018-07-31 14:02:34 +02:00
|
|
|
|
2019-03-07 00:11:45 -05:00
|
|
|
;; Affects pyenv and conda
|
2019-07-18 15:27:20 +02:00
|
|
|
(advice-add #'pythonic-activate :after-while #'+modeline-update-env-in-all-windows-h)
|
|
|
|
(advice-add #'pythonic-deactivate :after #'+modeline-clear-env-in-all-windows-h)
|
2019-03-04 20:48:52 -05:00
|
|
|
|
2019-03-01 15:12:27 -05:00
|
|
|
(setq-hook! 'python-mode-hook tab-width python-indent-offset))
|
2015-06-15 09:06:10 +02:00
|
|
|
|
2017-02-19 18:57:16 -05:00
|
|
|
|
2019-07-23 12:44:03 +02:00
|
|
|
(use-package! anaconda-mode
|
2019-07-28 02:38:27 +02:00
|
|
|
:after python
|
2016-04-23 22:08:46 -04:00
|
|
|
:init
|
2017-03-16 23:38:22 -04:00
|
|
|
(setq anaconda-mode-installation-directory (concat doom-etc-dir "anaconda/")
|
2016-04-23 22:08:46 -04:00
|
|
|
anaconda-mode-eldoc-as-single-line t)
|
|
|
|
:config
|
2017-12-08 22:33:12 -05:00
|
|
|
(add-hook 'anaconda-mode-hook #'anaconda-eldoc-mode)
|
2018-07-31 19:20:58 +02:00
|
|
|
(set-company-backend! 'anaconda-mode '(company-anaconda))
|
|
|
|
(set-lookup-handlers! 'anaconda-mode
|
2018-01-24 03:16:36 -05:00
|
|
|
:definition #'anaconda-mode-find-definitions
|
|
|
|
:references #'anaconda-mode-find-references
|
|
|
|
:documentation #'anaconda-mode-show-doc)
|
2018-07-31 19:20:58 +02:00
|
|
|
(set-popup-rule! "^\\*anaconda-mode" :select nil)
|
2018-01-31 05:32:05 -05:00
|
|
|
|
2019-07-28 02:38:27 +02:00
|
|
|
(add-hook! 'python-mode-local-vars-hook
|
|
|
|
(defun +python-init-anaconda-mode-maybe-h ()
|
|
|
|
"Enable `anaconda-mode' if `lsp-mode' isn't."
|
|
|
|
(unless (bound-and-true-p lsp-mode)
|
|
|
|
(anaconda-mode +1))))
|
|
|
|
|
2019-07-26 19:57:13 +02:00
|
|
|
(defun +python-auto-kill-anaconda-processes-h ()
|
2018-01-31 05:32:05 -05:00
|
|
|
"Kill anaconda processes if this buffer is the last python buffer."
|
|
|
|
(when (and (eq major-mode 'python-mode)
|
|
|
|
(not (delq (current-buffer)
|
|
|
|
(doom-buffers-in-mode 'python-mode (buffer-list)))))
|
|
|
|
(anaconda-mode-stop)))
|
|
|
|
(add-hook! 'python-mode-hook
|
2019-07-26 19:57:13 +02:00
|
|
|
(add-hook 'kill-buffer-hook #'+python-auto-kill-anaconda-processes-h nil t))
|
2015-06-15 09:06:10 +02:00
|
|
|
|
2018-07-31 14:02:34 +02:00
|
|
|
(when (featurep 'evil)
|
|
|
|
(add-hook 'anaconda-mode-hook #'evil-normalize-keymaps))
|
2018-12-23 23:54:27 -05:00
|
|
|
(map! :localleader
|
|
|
|
:map anaconda-mode-map
|
2017-02-28 12:11:18 -05:00
|
|
|
:prefix "f"
|
2018-12-23 23:54:27 -05:00
|
|
|
"d" #'anaconda-mode-find-definitions
|
|
|
|
"h" #'anaconda-mode-show-doc
|
|
|
|
"a" #'anaconda-mode-find-assignments
|
|
|
|
"f" #'anaconda-mode-find-file
|
|
|
|
"u" #'anaconda-mode-find-references))
|
2017-02-19 18:57:16 -05:00
|
|
|
|
2015-06-15 09:06:10 +02:00
|
|
|
|
2019-07-23 12:44:03 +02:00
|
|
|
(use-package! pyimport
|
2019-05-28 17:02:10 -04:00
|
|
|
:after python
|
|
|
|
:config
|
|
|
|
(map! :map python-mode-map
|
|
|
|
:localleader
|
2019-08-09 20:09:18 -03:00
|
|
|
(:prefix ("i" . "imports")
|
|
|
|
:desc "Insert missing imports" "i" #'pyimport-insert-missing
|
|
|
|
:desc "Remove unused imports" "r" #'pyimport-remove-unused
|
|
|
|
:desc "Sort imports" "s" #'pyimpsort-buffer
|
|
|
|
:desc "Optimize imports" "o" #'+python/optimize-imports
|
|
|
|
)))
|
2019-05-28 17:02:10 -04:00
|
|
|
|
|
|
|
|
2019-07-23 12:44:03 +02:00
|
|
|
(use-package! nose
|
2016-04-04 12:06:47 -04:00
|
|
|
:commands nose-mode
|
2018-07-31 14:02:34 +02:00
|
|
|
:preface (defvar nose-mode-map (make-sparse-keymap))
|
2019-07-22 04:30:04 +02:00
|
|
|
:minor ("/test_.+\\.py$" . nose-mode)
|
2016-04-04 12:06:47 -04:00
|
|
|
:config
|
2018-06-18 02:26:05 +02:00
|
|
|
(set-popup-rule! "^\\*nosetests" :size 0.4 :select nil)
|
2018-06-15 18:03:11 +02:00
|
|
|
(set-yas-minor-mode! 'nose-mode)
|
2018-07-31 14:02:34 +02:00
|
|
|
(when (featurep 'evil)
|
|
|
|
(add-hook 'nose-mode-hook #'evil-normalize-keymaps))
|
|
|
|
|
2018-12-23 23:54:27 -05:00
|
|
|
(map! :localleader
|
|
|
|
:map nose-mode-map
|
2017-02-28 12:11:18 -05:00
|
|
|
:prefix "t"
|
2018-12-23 23:54:27 -05:00
|
|
|
"r" #'nosetests-again
|
|
|
|
"a" #'nosetests-all
|
|
|
|
"s" #'nosetests-one
|
|
|
|
"v" #'nosetests-module
|
|
|
|
"A" #'nosetests-pdb-all
|
|
|
|
"O" #'nosetests-pdb-one
|
|
|
|
"V" #'nosetests-pdb-module))
|
2016-04-04 12:06:47 -04:00
|
|
|
|
2018-12-05 21:45:40 -05:00
|
|
|
|
2019-07-23 12:44:03 +02:00
|
|
|
(use-package! python-pytest
|
2018-12-05 21:45:40 -05:00
|
|
|
:defer t
|
2018-12-03 23:15:40 -05:00
|
|
|
:init
|
|
|
|
(map! :after python
|
2018-11-22 12:53:08 -06:00
|
|
|
:localleader
|
2018-12-23 23:54:27 -05:00
|
|
|
:map python-mode-map
|
2018-11-22 12:53:08 -06:00
|
|
|
:prefix "t"
|
2018-12-23 23:54:27 -05:00
|
|
|
"f" #'python-pytest-file
|
|
|
|
"k" #'python-pytest-file-dwim
|
2019-05-25 21:01:31 -03:00
|
|
|
"t" #'python-pytest-function
|
|
|
|
"m" #'python-pytest-function-dwim
|
|
|
|
"r" #'python-pytest-repeat
|
2018-12-23 23:54:27 -05:00
|
|
|
"p" #'python-pytest-popup))
|
2018-05-31 11:32:26 +02:00
|
|
|
|
2018-12-05 21:45:40 -05:00
|
|
|
|
2018-05-31 11:32:26 +02:00
|
|
|
;;
|
2018-07-31 14:02:34 +02:00
|
|
|
;; Environment management
|
2018-05-31 11:32:26 +02:00
|
|
|
|
2019-07-23 12:44:03 +02:00
|
|
|
(use-package! pipenv
|
2018-07-31 14:02:34 +02:00
|
|
|
:commands pipenv-project-p
|
2018-08-11 21:18:21 +02:00
|
|
|
:hook (python-mode . pipenv-mode)
|
|
|
|
:init (setq pipenv-with-projectile nil)
|
|
|
|
:config
|
2018-09-26 12:28:32 -04:00
|
|
|
(set-eval-handler! 'python-mode
|
2018-10-06 00:25:50 -04:00
|
|
|
'((:command . (lambda () python-shell-interpreter))
|
2018-09-26 12:28:32 -04:00
|
|
|
(:exec (lambda ()
|
|
|
|
(if-let* ((bin (executable-find "pipenv"))
|
2018-09-26 20:38:02 -04:00
|
|
|
(_ (pipenv-project-p)))
|
|
|
|
(format "PIPENV_MAX_DEPTH=9999 %s run %%c %%o %%s %%a" bin)
|
2018-09-26 12:28:32 -04:00
|
|
|
"%c %o %s %a")))
|
2019-03-07 00:11:45 -05:00
|
|
|
(:description . "Run Python script"))))
|
2018-07-31 14:02:34 +02:00
|
|
|
|
|
|
|
|
2019-07-23 12:44:03 +02:00
|
|
|
(use-package! pyvenv
|
2019-03-04 20:46:20 -05:00
|
|
|
:after python
|
2019-03-07 00:11:45 -05:00
|
|
|
:init
|
2019-03-04 20:46:20 -05:00
|
|
|
(when (featurep! :ui modeline)
|
2019-07-18 15:27:20 +02:00
|
|
|
(add-hook 'pyvenv-post-activate-hooks #'+modeline-update-env-in-all-windows-h)
|
|
|
|
(add-hook 'pyvenv-pre-deactivate-hooks #'+modeline-clear-env-in-all-windows-h))
|
2019-03-07 00:11:45 -05:00
|
|
|
:config
|
|
|
|
(add-hook 'hack-local-variables-hook #'pyvenv-track-virtualenv)
|
2019-03-04 20:46:20 -05:00
|
|
|
(add-to-list 'global-mode-string
|
2019-03-07 00:11:45 -05:00
|
|
|
'(pyvenv-virtual-env-name (" venv:" pyvenv-virtual-env-name " "))
|
2019-07-25 10:18:03 +05:30
|
|
|
'append)
|
|
|
|
(map! :map python-mode-map
|
|
|
|
:localleader
|
|
|
|
:prefix "e"
|
|
|
|
:desc "activate" "a" #'pipenv-activate
|
|
|
|
:desc "deactivate" "d" #'pipenv-deactivate
|
|
|
|
:desc "install" "i" #'pipenv-install
|
|
|
|
:desc "lock" "l" #'pipenv-lock
|
|
|
|
:desc "open module" "o" #'pipenv-open
|
|
|
|
:desc "run" "r" #'pipenv-run
|
|
|
|
:desc "shell" "s" #'pipenv-shell
|
|
|
|
:desc "uninstall" "u" #'pipenv-uninstall))
|
|
|
|
|
2019-03-04 20:46:20 -05:00
|
|
|
|
|
|
|
|
2019-07-23 12:44:03 +02:00
|
|
|
(use-package! pyenv-mode
|
2018-07-31 14:02:34 +02:00
|
|
|
:when (featurep! +pyenv)
|
|
|
|
:after python
|
|
|
|
:config
|
|
|
|
(pyenv-mode +1)
|
2018-09-25 22:17:41 -04:00
|
|
|
(when (executable-find "pyenv")
|
2019-09-23 10:14:19 +02:00
|
|
|
(add-to-list 'exec-path (expand-file-name "shims" (or (getenv "PYENV_ROOT") "~/.pyenv"))))
|
|
|
|
(add-hook 'python-mode-hook #'+python-pyenv-mode-set-auto-h)
|
|
|
|
(add-hook 'doom-switch-buffer-hook #'+python-pyenv-mode-set-auto-h))
|
2018-07-31 14:02:34 +02:00
|
|
|
|
|
|
|
|
2019-07-23 12:44:03 +02:00
|
|
|
(use-package! conda
|
2018-07-31 14:02:34 +02:00
|
|
|
:when (featurep! +conda)
|
|
|
|
:after python
|
|
|
|
:config
|
2019-09-22 14:05:03 -04:00
|
|
|
;; The location of your anaconda home will be guessed from a list of common
|
|
|
|
;; possibilities, starting with `conda-anaconda-home''s default value (which
|
|
|
|
;; will consult a ANACONDA_HOME envvar, if it exists).
|
2018-07-31 14:02:34 +02:00
|
|
|
;;
|
2019-09-22 14:05:03 -04:00
|
|
|
;; If none of these work for you, `conda-anaconda-home' must be set
|
|
|
|
;; explicitly. Afterwards, run M-x `conda-env-activate' to switch between
|
2018-07-31 23:07:22 +02:00
|
|
|
;; environments
|
2018-07-31 14:02:34 +02:00
|
|
|
(unless (cl-loop for dir in (list conda-anaconda-home
|
|
|
|
"~/.anaconda"
|
|
|
|
"~/.miniconda"
|
2018-11-14 22:33:18 -08:00
|
|
|
"~/.miniconda3"
|
2019-07-26 22:29:33 -07:00
|
|
|
"~/miniconda3"
|
2018-09-09 17:18:39 +08:00
|
|
|
"/usr/bin/anaconda3"
|
2018-11-15 00:54:22 -05:00
|
|
|
"/usr/local/anaconda3"
|
2019-07-27 11:11:47 -07:00
|
|
|
"/usr/local/miniconda3"
|
|
|
|
"/usr/local/Caskroom/miniconda/base")
|
2018-07-31 14:02:34 +02:00
|
|
|
if (file-directory-p dir)
|
|
|
|
return (setq conda-anaconda-home dir
|
|
|
|
conda-env-home-directory dir))
|
|
|
|
(message "Cannot find Anaconda installation"))
|
|
|
|
|
|
|
|
;; integration with term/eshell
|
|
|
|
(conda-env-initialize-interactive-shells)
|
|
|
|
(after! eshell (conda-env-initialize-eshell))
|
2019-07-27 13:53:38 +02:00
|
|
|
|
2019-03-01 15:12:27 -05:00
|
|
|
(add-to-list 'global-mode-string
|
2019-03-07 00:11:45 -05:00
|
|
|
'(conda-env-current-name (" conda:" conda-env-current-name " "))
|
2018-08-01 22:29:51 +02:00
|
|
|
'append))
|
2019-07-26 22:32:17 -04:00
|
|
|
|
2019-07-27 13:53:38 +02:00
|
|
|
|
2019-07-26 22:32:17 -04:00
|
|
|
(use-package! lsp-python-ms
|
|
|
|
:when (featurep! +lsp)
|
2019-07-27 13:53:38 +02:00
|
|
|
:after lsp-clients
|
2019-07-26 22:32:17 -04:00
|
|
|
:init
|
2019-09-11 19:55:25 -04:00
|
|
|
(setq lsp-python-ms-dir (concat doom-etc-dir "mspyls/"))
|
|
|
|
|
2019-07-27 13:53:38 +02:00
|
|
|
;; HACK lsp-python-ms shouldn't install itself if it isn't present. This
|
|
|
|
;; circumvents LSP falling back to pyls when lsp-python-ms is absent.
|
|
|
|
;; Installing the server should be a deliberate act; either 'M-x
|
|
|
|
;; lsp-python-ms-setup' or setting `lsp-python-ms-executable' to an existing
|
|
|
|
;; install will do.
|
2019-07-28 02:27:56 +02:00
|
|
|
(defadvice! +python--dont-auto-install-server-a ()
|
2019-07-27 13:53:38 +02:00
|
|
|
:override #'lsp-python-ms--command-string
|
|
|
|
lsp-python-ms-executable))
|