reddit / emacs
What is your setup for python coding in emacs?
FormerAct · 2022-02-17 14:02
What package/mode do you use and why you did you choose it with respect to other options? I am stick with elpy but it looks unmaintained...
spekvojbek · 2022-02-17 14:24
This is my config for Python. I use LSP with pyright for IDE functionality. Black and ISort for formatting my .py files. I feel like with this I have everything covered. Curious what other people use.
```python
(setenv "PYTHONIOENCODING" "utf8")
(setq python-shell-interpreter-args "-m asyncio")
(use-package python-black
:ensure t
:quelpa
(python-black
:fetcher git
:url "https://github.com/wbolster/emacs-python-black")
:after python)
(use-package py-isort
:ensure t
:quelpa
(py-isort
:fetcher git
:url "https://github.com/paetzke/py-isort.el")
)
(use-package lsp-pyright
:ensure t
:quelpa
(lsp-pyright
:fetcher git
:url "https://github.com/emacs-lsp/lsp-pyright")
:hook (python-mode . (lambda () (require 'lsp-pyright)))
:init (when (executable-find "python3")
(setq lsp-pyright-python-executable-cmd "python3")))
(defun my/python_pay_respects ()
"Make string to fstring"
(interactive)
(when (nth 3 (syntax-ppss))
(let ((p (point)))
(goto-char (nth 8 (syntax-ppss)))
(insert "f")
(goto-char p)
(forward-char))))
(defun my/python_format ()
"Format python buffer with isort and black"
(interactive)
(py-isort-buffer)
(python-black-buffer)
(save-buffer))
(add-hook 'python-mode-hook
(lambda () (local-set-key (kbd "C-c f") 'my/python_pay_respects)))
(add-hook 'python-mode-hook
(lambda () (local-set-key (kbd "C-c r") 'my/python_format)))
```
minikomi · 2022-02-17 15:02
- poetry
- pyenv
- direnv in the same directory as my pyproject.toml
layout pyenv
layout poetry
- pyright with that directory added to workspace
For exploratory coding, I start up a jupyter kernel in that project, on the command line and then use common header argument like
#+PROPERTY: header-args:jupyter-python :session <link to session json>
Extension_Try_6870 · 2022-02-17 17:46
Hi,
Doesn't pyright already support sorting import without the need to use py-isort.el ?
I'm just wondering because I'm using pylsp and I would like to try pyright.
thatsrelativity · 2022-02-18 04:42
Sort of off-topic, but a question about poetry - does it have any facilities like those provided by setup.py develop, like "pip install --editable"?
I couldn't find any last time I tried it, but maybe I was missing some config option. Since I only write tools for myself in python I like to install my scripts and be able to edit them as needed without having to rebuild + reinstall packages
spekvojbek · 2022-02-18 08:29
I should check it out. It's only recently I've starting to use pyright.
There is a \`lsp-organize-imports\` command available, however when executing I get \`LSP :: source.organizeImports action not available\`, so it seems I have some more configuration work
astoff1 · 2022-02-18 12:19
I use and recommend the following:
- The built-in Python major mode
- Eglot and `python-lsp-server` for completion and navigation
- [devdocs.el](https://github.com/astoff/devdocs.el) to read documentation
- [buffer-env](https://github.com/astoff/buffer-env/) or [envrc](https://github.com/purcell/envrc) for virtual environment integration.
(Full disclosure: some of these packages are mine.)
CrazyCrab · 2022-02-18 15:29
Elpy still works for me so I'm using it. It's very good while it works - go to definition works, documentation works, there is even nice interpreted and debugger integration and a hotkey for invoking black. Also, pyvenv.
(use-package elpy
:custom
(elpy-rpc-virtualenv-path 'current)
(elpy-shell-display-buffer-after-send t)
(python-shell-interpreter "ipython3")
(python-shell-interpreter-args "-i --simple-prompt -c \"autoreload 2\"")
(elpy-rpc-python-command "python3")
:init (advice-add 'python-mode :before 'elpy-enable)
:config
(setenv "PYTHONBREAKPOINT" "ipdb.set_trace")
(add-hook 'elpy-mode-hook (lambda () (electric-indent-local-mode -1)))
(add-hook 'elpy-mode-hook (lambda () (elpy-shell-toggle-dedicated-shell 1))))
(use-package pyvenv
:commands (pyvenv-activate)
:init
(setq conda-pytorch-env-dir (expand-file-name "~/soft/conda/envs/py38torch"))
(defun activate-pytorch ()
"Activate conda pytorch environment"
(interactive)
(pyvenv-activate conda-pytorch-env-dir))
:bind (("C-c p" . activate-pytorch)))
FormerAct · 2022-02-18 19:25
Is it still maintained?
CrazyCrab · 2022-02-18 19:48
The last commit in https://github.com/jorgenschaefer/elpy master was 16 days ago, so it seems yes.
dpassen1 · 2022-03-03 15:33
Happen to have an example project with that structure?
torsteinkrause · 2022-07-20 21:08
I have a very simple setup with `lsp-pyright`. You can see my setup in action here: https://youtu.be/gi6_OJLgQEo
Python specific part of my conf: https://gitlab.com/skybert/my-little-friends/-/blob/master/emacs/.emacs#L1472
fast-90 · 2022-07-29 07:27
u/minikomi Just to confirm: using this approach with `eglot` (and `layout pyenv 3.10.5` and `layout poetry` in my `.envrc`) makes Poetry install packages in a subfolder within the project directory for me. Is this expected behaviour?
Also, please can you clarify how you added the directory to the workspace for Pyright?
fast-90 · 2022-07-29 19:53
u/astoff1 Just to confirm - do you still have to create a `pyrightconfig.json` to set `venvPath` and `venv` for pyright/eglot to use the right venv?
astoff1 · 2022-07-29 20:48
I use python-lsp-server and not pyright, but no, there's no extra configuration need.
I install and run the language server in the venv. This is enough information for it to figure out the venv path and what not.
In fact, it helps to *not install* a language server system-wide. Then, if Eglot finds a language server ar all, it's clear that it's the right one, namely, the one from the venv.