reddit / emacs
Programming in Python
arivar · 2022-07-25 00:10
Hi everyone,
I've recently moved from Cinnamon to i3wm and I am quite happy with this, except for Pycharm, that is quite terrible on the WM, so I am giving a try to emacs and I started with Doom. However, there is very little documentation on how to setup it in a way that it is comparable with Pycharm in usability, so I am wondering if you could point me in a direction on how to do that. Besides, there are some other problems that I am facing:
- How do you setup and use python environments on emacs? I tried poetry, but it doesn't have anyway to inherit global site packages. There are many packages that I installed globally as I will always use them, so I don't want to keep reinstalling them in every environment.
- I see nowhere a place to run and debug my python scripts and see the results. Also, after everything is setup will it be able to show graphs and images?
- Treemacs always open in the .doom.d directory how can I make it open in my home directory?
Thanks in advance.
KaranasToll · 2022-07-25 01:15
You will need a python language server. This can be used with lsp mode or eglot. M-x pdb for debugging. M-x compile for running. Emacs can view image files just fine.
your_sweetpea · 2022-07-25 03:05
/u/KaranasToll seems to be talking about vanilla emacs whereas you're using Doom, so I recommend taking a look at the Doom documentation for the [`:lang python`](https://docs.doomemacs.org/latest/modules/lang/python/) module.
I'm not sure if you've read through the Doom [getting started guide](https://github.com/doomemacs/doomemacs/blob/master/docs/getting_started.org) but the instructions for using modules are contained in that.
It looks like the Doom `:lang python` module supports [conda, poetry, and pyenv](https://docs.doomemacs.org/latest/modules/lang/python/#/description/module-flags) for environment management, and it seems like setup is pretty automatic once you enable the right flags.
IncrocioVitali · 2022-07-25 06:52
The purpose of virtual environments is isolation, it's a feature to not use globally installed packages.
That said, you can use the same environment for many projects if you want. Then it becomes "globalish". Maybe that's easier with conda rather than poetry in doom, you can try both.
There's a command to open a REPL, bound to SPC o r by default. Do it from a python file and it opens a python REPL and so on.
perkinslr · 2022-07-25 07:07
Personally, I use elpy, which has support for virtual envs, uses jedi for completion, and has REPL / testing integration that works. I, also, used pycharm previously, and find this adequate. I have not replaced the right-click->refactor abilities from pycharm (and once in a while do miss them), but otherwise I miss nothing from pycharm.
shaqfooVA · 2022-07-25 13:30
I've tried many different ways to use virtual environments in python projects, my favorite one is using direnv mode for emacs, and then having a project-specific .envrc file (in the root of the project) where I activate the environment. This passes the necessary environment variables to all the buffers where they need to be seen for flycheck/black/etc.
I used to have a system for using virtualenvwrapper.el and activating in dir-locals, but it was not quite as good.
arivar · 2022-07-25 13:46
Thanks, do you think it would be better is I use spacemacs instead of doom?
Btw, any idea how to fix my problem with treemacs?
arivar · 2022-07-25 15:00
Thanks, I’ll give eglot a try. Do you have any idea how to fix my problem with treemacs?
your_sweetpea · 2022-07-25 16:19
I personally have a preference for Doom, although I just use vanilla emacs, but I'd recommend you go ahead and try both and see what you think. Doom has a bit of a different philosophy to spacemacs for certain.
I haven't used treemacs personally, but I imagine it just uses your `default-directory` at the time of launching. `default-directory` is buffer-local and reflects the directory that your current buffer is in, although it can get a bit confusing with buffers that don't actually represent a file or directory on disk. Non-file buffers created just after launching are typically going to end up using the directory that emacs is launched from (aka the PWD at the time) for their `default-directory`.
I suspect you're opening treemacs right after launching, so it should follow that and open to the directory you're actually launching emacs from -- are you launching emacs from within .doom.d?
Can you try `C-h v default-directory <RET>` before opening treemacs? What does it say the value of `default-directory` is?
KaranasToll · 2022-07-25 17:43
for treemacs, you can press h and l to change directory, OR the intended way is to add projects to treemacs. It will remember to the directories of the projects.
arivar · 2022-07-25 17:44
Sorry, but how do I add?
Edit: never mind, I’ve just found it. Thanks!
CJ6_ · 2022-07-25 19:37
Seconding the use of direnv! It’s worked really well for me
Y_Pon · 2022-07-26 09:12
I have stuff to say about this.
First of all, if you are Python Dev you need a full and nice experience that can be comparable with modern IDE like IDEA e.t.c.
And it's possible to achieve this goal with Emacs + some packages.
I have vanilla Emacs with my personal config, so I don't know how it works for Doom or Spacemacs, maybe it's already configured by default in a good way.
So, what do you need:
\- Language server for Python (lsp and lsp-ui)
Use lsp-mode it's more reach with features at the moment
[https://emacs-lsp.github.io/lsp-mode/page/installation/](https://emacs-lsp.github.io/lsp-mode/page/installation/)
\- Real-time program debugging (dap-mode)
[https://github.com/emacs-lsp/dap-mode](https://github.com/emacs-lsp/dap-mode)
\- Integration between those packages
https://alpha2phi.medium.com/emacs-lsp-and-dap-7c1786282324
The part of my config for lsp-mode and dap-mode
(use-package lsp-mode
:ensure t
:hook
(python-mode . lsp)
:commands lsp
:config
;; performance improvements
(setq lsp-log-io nil) ; if set to true can cause a performance hit
(setq lsp-enable-symbol-highlighting nil)
)
(use-package lsp-ui
:ensure t
:config
(setq lsp-ui-sideline-show-hover t)
(setq lsp-ui-sideline-show-code-actions t)
(setq lsp-ui-sideline-delay 0)
(setq lsp-ui-sideline-update-mode t)
)
Dap-mode configuration is quite complex, so I will not post it here. Check manuals, it should help :-)
LongjumpingAd7260 · 2022-07-28 13:35
I did a similar move 2 years ago. It becomes really good when you start coding some elisp. Currently, I am using Doom (emacs keys), i3wm (in KDE), lsp and pyright. When I can, I choose poetry. There is a emacs poetry package as well.
For a better experience with i3wm, I use almost always emacs "frames" instead of emacs "windows". So, I can control my "frames" using i3wm. The following code will provide frames instead of windows in most cases:
`(customize-set-variable`
`'display-buffer-base-action`
`'(display-buffer-reuse-window (reusable-frames . 0)))`