Table of Contents
I have previously failed to deploy elpy. elpy is a package that makes emacs an integrated environment (IDE) for Python.
I don't know the cause, but it didn't work no matter how hard I tried. I gave up after trial and error for about 3 days. There is a story that it is bad to use emacs even though I have no ability, but I can not move to another environment because my fingers are accustomed to it.
Since I recently re-introduced Python, I've been writing Python code using emacs as a regular editor. When I looked sideways at a colleague who used the IDE's auto-completion feature, I was always envious. So, I took the initiative and tried to introduce elpy again.
This article is a full copy </ del> reference to Real Python Introduction Article.
This article was also posted on my blog https://achiwa912.github.io/.
Suddenly, I don't like emacs. There are many advanced users of emacs in many ways. There aren't many introductory articles for beginners, or even if they are, they are often too advanced to understand.
I'm always nervous when rewriting the .emacs.d / init.el configuration file. I should have written it according to the advice in the introductory article, but I get a lot of incomprehensible errors and emacs becomes useless.
I'm sure emacs users like masochism </ del> spicy things.
That's why I hate the fact that emacs doesn't allow for various adventures. I feel the pressure to maintain the status quo. Speaking of adventures in the last 10 years, I started using org-mode and introduced elpy this time.
Let's get back to elpy. elpy stands for Emacs Python Development Environment, which provides the following Python IDE features.
--Automatic indentation (also in standard python-mode) --Syntax highlighting --Auto-completion --Syntax check --Python REPL integration --Virtual environment support
In addition, if you include a package that works with elpy, your emacs will transform into a great IDE that is as good as that one.
Immediately, edit the example init.el. Since elpy comes from an emacs package repository called melpa, you have to make emacs go to see melpa. My settings for this part are as follows.
(require 'package)
(setq package-archives
'(("gnu" . "http://elpa.gnu.org/packages/")
("melpa" . "http://melpa.org/packages/")
("org" . "http://orgmode.org/elpa/")))
(package-initialize)
(when (not package-archive-contents)
(package-refresh-contents))
This is the part that is important.
("melpa" . "http://melpa.org/packages/")
Next, bring the required packages from melpa and others. I got this method from Real Python, and it's pretty good. First, the necessary packages are defined as myPackages, and if it is not installed after that, it will be installed automatically.
(defvar myPackages
'(better-defaults
elpy
flycheck ;; On the fly syntax checking
py-autopep8 ;; Run autopep8 on save
blacken ;; Black formatting on save
material-theme
)
)
(mapc #'(lambda (package)
(unless (package-installed-p package)
(package-install package)))
myPackages)
I've read various packages besides elpy, but I'll explain them briefly.
--better-defaults --It seems that the default settings of emacs improve the poor points. For now, I don't know what has changed, but I'll put it in somehow. --flycheck --The grammar check is done on the fly. --py-autopep8 --When saving, it checks pep8 compliance and corrects it without permission. --blacken --When saving, it checks the format of Black and corrects it without permission. ――material-theme ――This theme was pretty good, so I switched from the one I was using so far.
Then activate the package. This is also just a sutra copy from Real Python like a Nembutsu, but it seems to have worked well. In addition, I put everything in at once here, but if an error occurs, I will not know what is wrong, so I think it is better to add while moving little by little.
(load-theme 'material t)
;; elpy
(elpy-enable)
(setq elpy-rpc-virtualenv-path 'current)
;; Flycheck
(when (require 'flycheck nil t)
(setq elpy-modules (delq 'elpy-module-flymake elpy-modules))
(add-hook 'elpy-mode-hook 'flycheck-mode))
;; autopep8
(require 'py-autopep8)
(add-hook 'elpy-mode-hook 'py-autopep8-enable-on-save)
There is one caveat. I'm using venv instead of the popular virtualenv, so I followed the initial error message and did the following: I'm sorry if there are any side effects.
(setq elpy-rpc-virtualenv-path 'current)
And by doing this, I was told to put ~ / .local / bin in the environment variable PATH, so I modified ~ / .zshrc (on macOS).
export PATH="$HOME/.local/bin:$PYENV_ROOT/bin:$PATH"
Use pip to bring packages that cannot be brought from melpa.
pip3 install jedi flake8 autopep8 yapf black
I think it was like this. Actually, I installed them one by one, so I wrote by looking at the history.
Open the python .py file, type M-x elpy-config and press return.
This time it worked. The packages I brought were all recognized.
The Real Python introduction article mentioned above starts with the explanation of emacs, so I accidentally overlooked it, but in a lot of searches It was the most useful article. I am grateful that there are few emacs articles for beginners. Thank you, Real Python.
Recommended Posts