1

I'm struggling to get line length correctly setup under Elpy.

I found How to customize the line character length in elpy? and have in the past found flycheck cannot find module for pylint.

My configuration looks like (I've tried both autopep8 and yapf)...

(add-hook 'python-mode-hook
      (lambda ()
        (setq flycheck-python-pylint-executable "~/.virtualenvs/default/bin/pylint")
        (setq flycheck-pylintrc "~/.emacs.d/settings/.pylintrc")))
;; enable autopep8 formatting on save
(require 'py-autopep8)
(add-hook 'elpy-mode-hook 'py-autopep8-enable-on-save)
;; (require 'py-yapf)
;; (add-hook 'python-mode-hook 'py-yapf-enable-on-save)

I have as per the suggestions in autopep8 · PyPI created ~/.config/pycodestyle which contains...

[pycodestyle]
max_line_length = 120
ignore = E501

...and yet on saving a file it doesn't apply the 120 character rule in so much as lines that are > 120 characters are not auto-formatted and still reports in *Messages*...

line too long (124 > 79 characters) [E501] [2 times]

I've also set the following configuration option

(setq-default fill-column 120)

...which I'm unsure whether it will be interfering with things.

Any ideas or suggestions on how to configure autopep8 to automatically split lines at 120 characters would be very gratefully received as I feel like I've been going round in circles, despite seemingly trying the suggested solutions (I admit I may have misunderstood how to do this though or just cocked things up!).

EDIT 2020-12-29

In light of answer from Chin-Ben I checked elpy-config I get...

Elpy Configuration

Emacs.............: 27.1
Elpy..............: 1.35.0
Virtualenv........: python3_9 (/home/neil/.virtualenvs/python3_9/)
Interactive Python: ipython 7.16.1 (/home/neil/.virtualenvs/python3_9/bin/ipython)
RPC virtualenv....: rpc-venv (/home/neil/.config/emacs/elpy/rpc-venv)
 Python...........: python3 3.6.12 (/home/neil/.config/emacs/elpy/rpc-venv/bin/python3)
 Jedi.............: 0.16.0 (0.18.0 available)
 Rope.............: 0.16.0
 Autopep8.........: 1.5 (1.5.4 available)
 Yapf.............: 0.29.0 (0.30.0 available)
 Black............: 19.10b0 (20.8b1 available)
Syntax checker....: flake8 (/home/neil/.virtualenvs/python3_9/bin/flake8)

Warnings

There is a newer version of Jedi available.

[Update jedi]

There is a newer version of the autopep8 package available.

[Update autopep8]

There is a newer version of the yapf package available.

[Update yapf]

There is a newer version of the black package available.

[Update black]

...and have duly updated jedi/autopep8/yapf/black all of which are from the RPC env, but in doing so I noticed that pip was outdated as it reported...

You are using pip version 18.1, however version 20.3.3 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

There are many versions of pip installed on my system in various places, one in ~/.local/bin/pip along with many other commands (all dated from 2018, possibly from where I did a pip install --user before I understood virtual environments correctly).

Removing ~/.local/bin/pip didn't resolve this so its something else out of the many being picked up out of...

❱ locate pip | grep bin | grep 'pip$'
/home/user/.config/emacs/elpy/rpc-venv/bin/pip
/home/user/.local/bin/pip
/home/user/.virtualenvs/elpy-rpc-venv/bin/pip
/home/user/.virtualenvs/floow_jobs/bin/pip
/home/user/.virtualenvs/python3_9/bin/pip
/home/user/.virtualenvs/python3_9/default/bin/pip
/home/user/.virtualenvs/tcx2gpx-test/bin/pip
/home/user/.virtualenvs/test/bin/pip
/usr/bin/pip

Its not the system that is being picked up as that is up-to-date, the elpy-rpc-venvone is up-to-date too, but the ~/.config/emacs/elpy/rpc-venv/bin/pip reports an error which is informative...

❱ /usr/bin/pip --version
pip 20.3.3 from /usr/lib/python3.8/site-packages/pip (python 3.8)
❱ /home/user/.virtualenvs/elpy-rpc-venv/bin/pip --version
pip 20.3.3 from /home/user/.virtualenvs/elpy-rpc-venv/lib/python3.6/site-packages/pip (python 3.6)
❱ /home/user/.config/emacs/elpy/rpc-venv/bin/pip --version
zsh: /home/user/.config/emacs/elpy/rpc-venv/bin/pip: bad interpreter: /home/neil/.emacs.d/elpy/rpc-venv/bin/python3: no such file or directory

...and I've been having trouble with Jedi complaining about the EPC Server configuration...

Error (jedi): 
================================
Failed to start Jedi EPC server.
================================

*** EPC Error ***
make client process failed


*** EPC Server Config ***
Server arguments: ("/home/user/.virtualenvs/python3_9/default/bin/jediepcserver")
Actual command: /home/user/.virtualenvs/python3_9/default/bin/jediepcserver
VIRTUAL_ENV envvar: "/home/user/.virtualenvs/python3_9/"

*** jedi-mode is disabled in #<buffer test.py> ***
Fix the problem and re-enable it.

*** You may need to run "M-x jedi:install-server". ***
This could solve the problem especially if you haven't run the command yet
since Jedi.el installation or update and if the server complains about
Python module imports.

...which the suggested course of action (M-x jedi:install-server) did not resolve. What stands out though is that...

❱ /home/user/.config/emacs/elpy/rpc-venv/bin/pip --version
zsh: /home/user/.config/emacs/elpy/rpc-venv/bin/pip: bad interpreter: /home/user/.emacs.d/elpy/rpc-venv/bin/python3: no such file or directory

...indicates that something has gone awry when I moved from keeping my config under ~/.emacs.d to ~/.config/emacs/ a while back.

Suffice to say it looks like my virtual environments/EPC server are not working as intended, but this has been really useful in helping to identify this as I was baffled as to why I couldn't fix things.

1
  • An alternative solution is to use black, which will handle all formatting for you and integrates with elpy.
    – al0
    May 1, 2020 at 14:01

2 Answers 2

1

According to Elpy README, flymake-mode is used for syntax check. Not flycheck.

See https://github.com/jorgenschaefer/elpy/blob/4032c7251eb2d74ec8a301a3988b62b7a0f00932/elpy.el#L3703 the line containing (flymake-mode 1).

If you prefer flycheck, please turn off flymake-mode and make sure the virtual environment created by elpy is activated.

Steps to set up Elpy,

  • Please read its README at least once

  • Install latest Elpy from https://melpa.org

  • Set up Python environment and copy sample setup code into ~/.emacs

  • Start Emacs, Elpy will automatically install all required python packages into a new virtual environment

  • Run M-x elpy-config, you got,

elpy without virtual environment activated

  • Add (with-eval-after-load 'elpy (pyvenv-activate "~/.emacs.d/elpy/rpc-venv")) into ~/.emacs and restart Emacs. Or activate virtual environment manually by running command source ~/.emacs.d/elpy/rpc-venv/bin/activate in bash shell

  • Run M-x elpy-config again, you got, enter image description here

  • Continue edit python code as usual.

If you want to set up flake8, see https://flake8.pycqa.org/en/latest/user/configuration.html ,

Flake8 supports storing its configuration in the following places:

Your top-level user directory
In your project in one of setup.cfg, tox.ini, or .flake8.

You can run source ~/.emacs.d/elpy/rpc-venv/bin/activate && flake8 --help && deactivate to list all flake8 options.

Extra tip, It's wise to backup all the required python packages in virtual environment,

source ~/.emacs.d/elpy/rpc-venv/bin/activate && pip freeze > elpy-requirements.txt && deactivate

You can restore the packages,

source ~/.emacs.d/elpy/rpc-venv/bin/activate && pip install -r elpy-requirements.txt && deactivate
1
  • Thanks, this is really, really useful as its helped me identify a "few" problems with my virtual and RPC/EPC environments that I need to resolve and have been causing me headaches. Hopefully once I work through those I can sort everything out.
    – slackline
    Dec 29, 2020 at 20:38
0

I am using configuration per project. The configuration filesetup.cfg is needed at root folder of your project.

Below is my setting in setup.cfg.

[flake8]
max-line-length = 120
[pycodestyle]
max_line_length = 120
[yapf]
column_limit = 120
1
  • 1
    Thanks, I'll give a go at putting in the options to my global configuration (would rather avoid having things set on a per project basis).
    – slackline
    Dec 3, 2019 at 10:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.