[PYTHON] Introduce elpy to emacs

Table of Contents

  1. [Introduction](# orgc6070a6)
  2. [I hate emacs](# org85eb6ff)
  3. [What is elpy](# org95f68a5)
  4. [Introduction of elpy](# org4db3c00)
  5. [Add melpa repository](# orgd8a887d)
  6. Load Package (# org4d233fb)
  7. [Activate Package](# orga71c8b8)
  8. [Bring a package to install with pip](# orgfddbff4)
  9. [Confirm](# org68d0e96)
  10. [At the end](# org1905831)

Introduction

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/.

What I hate about emacs

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.

What is elpy

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.

Introduction of elpy

Add melpa repository

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/")

Load the package

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.

Activate the package

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"

Bring a package to install with pip

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.

Confirm

Open the python .py file, type M-x elpy-config and press return. elpy-config.png

This time it worked. The packages I brought were all recognized.

At the end

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

Introduce elpy to emacs
Introduce serverspec to Linux
I tried to introduce Pylint
Try to introduce the theme to Pelican
Forcibly introduce real-time translation to Zoom
emacs + elpy + pyenv-virtualenv uses system python
Emacs perspective.el workspace is easier to use
[Rails] How to introduce Google Analytics [Easy]
Introduce Python library TRML2PDF to MacOSX (10.11.xx)
[Python] Introduce UIKit3 to your Django project