tl;dr install this plugin and have PyCharm start blackd
via a shell script on launch. Reassign the "reformat code" keyboard shortcut to call the plugin.
Immediate Performance Issues
Whilst making my yearly pilgrimage from vim to PyCharm (I usually go back to vim after a bit), I found it less than straightforward to apply the black formatter instead of using PyCharm's built-in "reformat code" settings.
For those who don't know what black is, it's a way to never think about formatting in Python ever again.
After following the Black docs about setting black up in PyCharm, I found the file watcher for it to be incredibly slow, interrupting the flow of my work while I waited for it to format a single file.
So I instead turned off the file watcher and remapped the "reformat code" keyboard shortcut to instead call black
on the currently displayed file. This is great, but it also opens a terminal and displays the executed command as well as
Process finished with exit code 0
every single time the code is reformatted. A little research yielded a --quiet
parameter to black
, but for whatever reason this didn't silence the output, at least not when executed via PyCharm.
Plugin?
Jetbrains IDEs have a pretty decent plugin marketplace and community. Maybe not on par with VSCode by now, but then again there's a lot more (nice) batteries included in Jetbrains IDEs than VSCode.
A quick search yielded BlackConnect, which glues PyCharm to a blackd
daemon. It requires installing and running blackd
(pip install black[d]
), but it's nice and fast and doesn't bring attention to itself. I remapped the "reformat code" keyboard shortcut to invoke BlackConnect.
A Little Bit of Configuration
Making it work on startup requires an extra step, though: I made an on_pycharm_startup.sh
file containing a single command blackd
, did chmod +x
on it, then added it to Tools>Startup Tasks. Boom! Black works quickly and silently when manually called via keyboard shortcut.
A note: BlackConnect has a "reformat on save" option, but it's just as slow as and less reliable than running black
from a file watcher. Maybe this will change at some point.
Top comments (11)
Thanks for sharing your journey back to pycharm with us. I started using pycharm when I first learned python , but it took too many resources to do that and pro e for my main job. I ended up running sublime text for quite awhile, a short period in vscode, before landing full time in neovim.
Any reason you're going to pycharm? I could see the appeal of it just mostly works out of the box..
I've recently started really leaning in on the vim features that most vim emulators are missing, jumplist, taglist, quickfix.
Yeah, itās the included batteries, in particular the refactoring and go-to-definition ones. Itās āhomeā and then vim is my minimal cabin in the woods; Iāve just been in the cabin for a longer stretch than usual. š
Do you prefer to keep your vim config minimal or would you be willing to bring refactoring and go-to-definition to vim?
totally open. HOWEVER, let me up the ante and say that I also want the one-keystroke (two?)
pip install
and import functionality. šCan you give me an example? Does pycharm do this. I'm thinking of several things you could be referring to.
Yeah, if I import from a package that isnāt installed in a projectās virtualenv, then press alt-space over that import and itās squiggly underline, I get a context menu whose first option is to install the package.
Similarly, if I invoke a function (or anything) that doesnāt exist in the current moduleās namespace, alt-space over that thing brings up a context menu the first option of which (i think) is to import it.
I've had auto import working with the native lsp, but it's spotty and I don't understand enough to debug it yet. Does pycharm handle installing packages with a diferent package name than you import? I think I could easily make a keybinding that does a pip install of the word under the cursor, but I don't have a clue how you would figure out that bs4 is beautifulsoup4 or pil is pillow. Are these features that you use ofen? I leverage cookiecutter heavily so I end up with everything in requirements.txt.
Anyways, I was just curious. I can't choose what editor makes you most comfortable or productive, nor would I want anyone using an editor that was not by choice. I really like nvim and the benefits it gives me, thats why I use it.
No I think differently named packages probably canāt be auto installed. what I like about pycharm is that it tells me right away about things Iām calling which havenāt been imported, and then not having to navigate anywhere to put the import statement in. So in that context I use it very frequently, and I also very frequently waste time when developing in vim because of forgotten imports.
I always end up fiddling quite a bit with whichever editor Iām using, so I donāt think either editor wins on that, but PyCharm is less flexible, which limits my fiddling a little bit.
What version of vim are you on? Some options I've had success with to get the red underline is ALE or the native lsp in nvim. I am really digging the native lsp in nvim 0.5.0, but as its still only in nightly it there is not quite as much content around it, so setting it up has taken more time than I'd like to admit that I spent on it. ALE is something that I installed long ago and didn't realize all the great things it does until shortly before uninstalling it. ALE feels quite battle tested and seemed like it did a lot out of the box. There was a setting or two that felt annoying that I changed, but it was pretty minimal compared to the current state of native lsp or coc.vim.
Here is what some errors look like on my setup.
I'm trying to swich from pyright to pyls as it will allow me to use mypy for type checking. Currently pyright complains about a bunch of pandas stuff that mypy does not. pyright seems to work with the least config though if you are already on nvim 0.5.0
Mmm, depends on whether I'm doing work or personal stuff, but I think I switched back to Vim 8-point-something from nvim 0.4.something a couple months ago. I used Ale for quite a while for linting + fixing, it worked fine. Also, the vim emulation in Jetbrains lacks some pretty important things, like it won't autocomplete a word if it happens to exist as a variable but you're trying to complete it in a string. There are a few other things like that.
While nvim-lsp is really useful, it is as limited as the LSP it is using. I found the proprietary PyCharm LSP incredibly useful when working on large-scale django applications. The autocompletion is incredible there!
I still use nvim (nightly) for the frontend part of my fullstack role, but for now I cannot live without e.g.
Car.objects.filter(driver__address__city="Detroit").values("some__other__attribute")
where both driver address and some_other_attribute will be offered as auto completion. I didn't get this "deep insight" to work with nvim nor with VS Code.