yapf icon indicating copy to clipboard operation
yapf copied to clipboard

How do I install yapf in pycharm

Open liuhui931205 opened this issue 7 years ago • 13 comments

liuhui931205 avatar Oct 12 '18 09:10 liuhui931205

On the main menu, choose File -> Settings for Windows and Linux or press Ctrl+Alt+S Find Project Interpreter under the Project: [Your Project Name]. Click the green plus(+) and search for Yapf and install package

sevvalmehder avatar Oct 24 '18 18:10 sevvalmehder

The best way I've found is to create a File Watcher. That is, go to Preferences -> File Watchers -> + -> . My settings include:

File Type: Python Scope: Open Files Program: yapf Arguments: -i $FilePathRelativeToProjectRoot$

Hope it helps! (Make sure you have installed yapf via pip install yapf in the computer PyCharm runs in.)

bcollazo avatar Oct 26 '18 14:10 bcollazo

Hi @sevvalmehder and @bcollazo I have followed your steps to add a package in the project interpreter and also add it to file watchers. I also pip install yapf and download the plugins. However, when I use Code -> Reformat code with YARF, it always say "YAPF executable is not found." Do I miss any step?

davislf2 avatar Nov 14 '18 00:11 davislf2

Hey @davislf2, it can be a number of things. Using the 'Reformat Code' seems like you are using the action provided by the plugin. I think that plugin expects yapf to be available in /usr/local/bin/yapf (https://github.com/binh-vu/yapf-pycharm/blob/master/src/ReformatCode.java#L45).

Do you have yapf there? yapf should be at the very least accessible from the command line in a new terminal for you. Also the output for which yapf in your command line should be helpful.

bcollazo avatar Nov 15 '18 19:11 bcollazo

See autopep8-on-pycharm, they are the same except that you use yapf instead of autopep8. In addition, isort is configurated the same way. Best Wishes.

BaksiLi avatar Jan 02 '19 03:01 BaksiLi

  1. install it via pip or conda
  2. install the plugin in pycharm e.g. from marketplace or from file
  3. Make sure your executable is at the right location:
  • In Pycharm choose Preferences->Tools:
  • look at YAPF Settings section -> Executable path
  • You have to put your yapf executable at this location/path if it isn't already.

spectralLight avatar Jan 13 '19 10:01 spectralLight

@spectralLight PyCharm 2018.3 in your step #3 there is no YAPF present anywhere in the settings: screenshot 2019-02-10 at 18 26 41

The default location using pip install yapf is:

(base) Glenns-MBP:~ glennjocher$ which yapf
/anaconda3/bin/yapf

glenn-jocher avatar Feb 10 '19 17:02 glenn-jocher

Could not get it to work using any of these suggestions. BTW, @spectralLight in PyCharm 2018.3 Community nothing shows up in this panel after installing the plugin, so I'm quite confused. There is no 'YAPF Settings section' as you mention:

screenshot 2019-02-10 at 18 26 41

glenn-jocher avatar Feb 10 '19 18:02 glenn-jocher

If using the yapf-pycharm plugin (vs. the yapf plugin) then you have to have the yapf executable located at /usr/local/bin/yapf. One way to do this is to create a symbolic link:

ln -s $(which yapf) /usr/local/bin/yapf

dshurick avatar Mar 08 '19 14:03 dshurick

I have it installed with anaconda, and I tried both the /usr/local/bin/yapf path with the softlink and the /anaconda3/bin/yapf path, but I'm still not seeing anything change on save. I'm on professional 2019.1

Screen Shot 2019-06-21 at 3 47 11 PM

szeitlin avatar Jun 21 '19 22:06 szeitlin

While the "yapf-pycharm" plugin for Pycharm did work for me after creating the symbolic link, I had issues combining it with a file watcher, and the "yapf" plugin simply would not operate the way I desired either.

I eventually added it to my external tools, and created a file watcher using it, and everything works perfectly.

Preferences Preferences

Lygre avatar Feb 26 '20 17:02 Lygre

While the "yapf-pycharm" plugin for Pycharm did work for me after creating the symbolic link, I had issues combining it with a file watcher, and the "yapf" plugin simply would not operate the way I desired either.

I eventually added it to my external tools, and created a file watcher using it, and everything works perfectly.

Preferences Preferences

I tried many things but none of them worked. This is the perfect solution.

ConfuzedCoder avatar Jul 03 '22 16:07 ConfuzedCoder

For whatever reason, I had to set arguments to -i $FilePath$ to pass the full file path to yapf, and leave "Working directory" blank. Otherwise, yapf kept complaining that I wasn't passing in a valid file. Otherwise, @Lygre's solution worked.

anna-hope avatar Apr 12 '23 16:04 anna-hope