I am trying to use black to refactor my code, but when I run C-c C-r r (elpy-refactor), the code is not refactored using black but using another formatter.
pyvenv-workon.elpy-refactor.The code is refactored but not with black. black is available within the virtual environment as I can run:
...from the command line and it refactor the file properly.
This might be due to a bad configuration with elpy-rpc-python-command. I had to set it to python3.6 (see below), otherwise black is not found by elpy-config.
Debian.
(elpy-config)Elpy Configuration
Emacs.............: 26.3
Elpy..............: 1.32.0
Virtualenv........: devops (/home/mikael.capelle/envs/devops)
Interactive Python: ipython 7.10.1 (/home/mikael.capelle/envs/devops/bin/ipython)
RPC virtualenv....: rpc-venv (/home/mikael.capelle/.emacs.d/elpy/rpc-venv)
Python...........: python3.6 3.6.9 (/home/mikael.capelle/.emacs.d/elpy/rpc-venv/bin/python3.6)
Jedi.............: 0.15.1
Rope.............: 0.14.0
Autopep8.........: 1.4.4
Yapf.............: 0.29.0
Black............: 19.10b0
Syntax checker....: flake8 (/home/mikael.capelle/envs/devops/bin/flake8)
;; python
(use-package elpy
:ensure t
:config
(when (load "flycheck" t t)
(require 'flycheck-mypy)
(flycheck-add-next-checker 'python-flake8 'python-mypy)
(setq elpy-modules (delq 'elpy-module-flymake elpy-modules))
(add-hook 'elpy-mode-hook 'flycheck-mode))
(setq python-shell-interpreter "ipython"
python-shell-interpreter-args "-i --simple-prompt")
(add-to-list 'python-shell-completion-native-disabled-interpreters
"jupyter")
:init
(setq elpy-rpc-backend "jedi")
(setq elpy-rpc-python-command "python3.6")
(elpy-enable))
(add-hook 'python-mode-hook 'hs-minor-mode)
You can use elpy-black-fix-code to reformat your code using black.
Add this to your init to be able to use it with C-c C-r f:
(define-key elpy-refactor-map (kbd "f")
(cons (format "%sormat code"
(propertize "f" 'face 'bold))
'elpy-black-fix-code))
@galaunay Thanks, I could use this temporarily but this will only work for the whole file, right?
Isn't elpy-refactor suppose to use black if available? I thought it did on my previous installation, but maybe I never faced a case where the default refactoring was different from black's one.
So I tried on my previous installation and indeed elpy-refactor uses black. Below are the two elpy-config outputs and a small snippet that is refactored differently:
Python snippet:
import typing
# black version:
def ldap_update_password(
username: str, old_password: str, new_password: str
) -> typing.Tuple[bool, str]:
pass
# other version:
def ldap_update_password(username: str, old_password: str,
new_password: str) -> typing.Tuple[bool, str]:
pass
elpy-config that works properly:
Elpy Configuration
Emacs.............: 26.1
Elpy..............: 1.32.0
Virtualenv........: myenv (/opt/envs/myenv)
Interactive Python: ipython 7.9.0 (/opt/envs/myenv/bin/ipython)
RPC virtualenv....: rpc-venv (/home/mikael.capelle/.emacs.d/elpy/rpc-venv)
Python...........: python3 3.6.8 (/home/mikael.capelle/.emacs.d/elpy/rpc-venv/bin/python3)
Jedi.............: 0.15.1
Rope.............: 0.14.0
Autopep8.........: 1.4.4
Yapf.............: 0.28.0 (0.29.0 available)
Black............: 19.10b0
Syntax checker....: flake8 (/opt/envs/myenv/bin/flake8)
Warnings
The directory ~/.local/bin/ is not in your PATH. As there is no active virtualenv, installing
Python packages locally will place executables in that directory, so Emacs won't find them. If
you are missing some commands, do add this directory to your PATH -- and then do
`elpy-rpc-restart'.
There is a newer version of the yapf package available.
elpy-config that does not work properly:
Elpy Configuration
Emacs.............: 26.3
Elpy..............: 1.32.0
Virtualenv........: flask (/home/mikael.capelle/envs/flask)
Interactive Python: ipython (not found)
RPC virtualenv....: rpc-venv (/home/mikael.capelle/.emacs.d/elpy/rpc-venv)
Python...........: python3 3.6.5 (/home/mikael.capelle/.emacs.d/elpy/rpc-venv/bin/python3)
Jedi.............: 0.15.1
Rope.............: Not found (0.14.0 available)
Autopep8.........: 1.4.4
Yapf.............: 0.29.0
Black............: 19.10b0
Syntax checker....: flake8 (/home/mikael.capelle/envs/flask/bin/flake8)
Warnings
The directory ~/.local/bin/ is not in your PATH. As there is no active virtualenv, installing
Python packages locally will place executables in that directory, so Emacs won't find them. If
you are missing some commands, do add this directory to your PATH -- and then do
`elpy-rpc-restart'.
One issue might be that if there are 2 or more formatters found, it will use whichever it finds first (I believe this was the case for another issue) If you're not using the other formatter (Looks like yapf is being used in this case) then you can try removing. Otherwise manually calling it will be your best bet
@gopar Thanks, this seems to fix the problem.
I think the issue was that I said "yes" when elpy asked me if I wanted the relevant package to be automatically installed in the rpc-env, so I had to remove yapf and autopep8 from the environment.
You are not the first to put forward the need to be able to control which formater is used by default.
I think it may be a good idea to add a new option, or be clearer about how to use a specific formater.
I will put that on the to-do list.
Most helpful comment
You are not the first to put forward the need to be able to control which formater is used by default.
I think it may be a good idea to add a new option, or be clearer about how to use a specific formater.
I will put that on the to-do list.