How can I configure Elpy to use dev dependencies like Black, Jedi, and flake8 that are installed in a Poetry project virtual environment rather than those that are installed systemwide?
2 Answers
Use the poetry.el
Emacs package with poetry-tracking-mode
enabled by add-hook
ing it to elpy-mode-hook
. Elpy will automatically detect the Poetry virtual environment and check for those dev tools in that environment.
Jan 2021 update: I've since stopped using Elpy and switched to lsp-mode
and lsp-python-ms
- it's a far better development experience. The only caveat is that it can only discover virtualenvs local to the project, so Poetry has to be configured to create venvs locally (poetry config virtualenvs.in-project true
).

- 3,454
- 1
- 17
- 38
-
Can you please add how poetry should be configured? I'm also using this package but unable to get the lsp to work with poetry properly – scientific_explorer Oct 28 '21 at 19:53
-
1My init.el is on GitHub [here](https://github.com/jidicula/dotfiles/blob/main/init.el), I've added a hook to activate `poetry-tracking-mode` to `python-mode-hook`, which itself is bound to lsp-mode and `lsp-pyright`. I had it working with `mspyls` previously, too. – jidicula Oct 29 '21 at 16:16
-
1For some reason I had to remove all workspaces from `lsp-mode` and re-add to get this working. Used [this jidicula `init.el` as reference for `lsp-mode`](https://github.com/jidicula/dotfiles/blob/f6832115ef6406d0826f14cb21027f9f71638ca6/init.el). jidicula has since moved to eglot - unclear if the `poetry-tracking-mode` still works with it, but suspect so. – holocronweaver Oct 02 '22 at 20:53
-
I haven't worked with Python in a long time so I'm not sure how well Eglot plays with Poetry. – jidicula Oct 05 '22 at 12:57
If you use emacs -nw
, then another option is
- Install virtualenvwrapper in poetry project. That is run
poetry add -D virtualenvwrapper
. - Install elpy dependencies you need.
M-x elpy-config' to find dependencies. So
poetry add -D jedi rope autopep8 yapf black flake8`. This can also be done when elpy prompts you, but then it will probably install as a normal dependency and not just dev dependency. - Add
(setq elpy-rpc-virtualenv-path 'current)
so that elpy uses the poetry virtual env for Virtualenv, Interactive Python, RPC virtual env. - Launch emacs through poetry
poetry run emacs -nw
You need to do this for each poetry project. There should be way to install some of these globally in pyenv and have poetry and the project virtual env pick it up. However, doing it for each project keeps dependencies isolated, even for dev stuff.