• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Using YAPF with your editor
2
3YAPF is supported by multiple editors via community extensions or plugins.
4
5- [IntelliJ/PyCharm](#intellijpycharm)
6- [IPython](#ipython)
7- [VSCode](#vscode)
8
9## IntelliJ/PyCharm
10
11Use the `File Watchers` plugin to run YAPF against a file when you perform a save.
12
131.  Install the [File Watchers](https://www.jetbrains.com/help/idea/using-file-watchers.html) Plugin
141.  Add the following `.idea/watcherTasks.xml` to your project. If you already have this file just add the `TaskOptions` section from below. This example uses Windows and a virtual environment, modify the `program` option as appropriate.
15    ```xml
16    <?xml version="1.0" encoding="UTF-8"?>
17    <project version="4">
18        <component name="ProjectTasksOptions">
19            <TaskOptions isEnabled="true">
20                <option name="arguments" value="-i $FilePathRelativeToProjectRoot$" />
21                <option name="checkSyntaxErrors" value="true" />
22                <option name="description" />
23                <option name="exitCodeBehavior" value="ERROR" />
24                <option name="fileExtension" value="py" />
25                <option name="immediateSync" value="true" />
26                <option name="name" value="yapf" />
27                <option name="output" value="" />
28                <option name="outputFilters">
29                    <array />
30                </option>
31                <option name="outputFromStdout" value="false" />
32                <option name="program" value="$PROJECT_DIR$/.venv/Scripts/yapf.exe" />
33                <option name="runOnExternalChanges" value="true" />
34                <option name="scopeName" value="Project Files" />
35                <option name="trackOnlyRoot" value="false" />
36                <option name="workingDir" value="$Projectpath$" />
37                <envs />
38            </TaskOptions>
39        </component>
40    </project>
41    ```
42
43## IPython
44
45IPython supports formatting lines automatically when you press the `<Enter>` button to submit the current code block.
46
47Make sure that the YAPF module is available to the IPython runtime:
48
49```shell
50pip install ipython yapf
51```
52
53pipx example:
54
55```shell
56pipx install ipython
57pipx inject ipython yapf
58```
59
60Add following to `~/.ipython/profile_default/ipython_config.py`:
61
62```python
63c.TerminalInteractiveShell.autoformatter = 'yapf'
64```
65
66## VSCode
67
68VSCode has deprecated support for YAPF in its official Python extension [in favor of dedicated formatter extensions](https://github.com/microsoft/vscode-python/wiki/Migration-to-Python-Tools-Extensions).
69
701. Install EeyoreLee's [yapf](https://marketplace.visualstudio.com/items?itemName=eeyore.yapf) extension.
711. Install the yapf package from pip.
72   ```
73   pip install yapf
74   ```
751. Add the following to VSCode's `settings.json`:
76   ```jsonc
77   "[python]": {
78       "editor.formatOnSaveMode": "file",
79       "editor.formatOnSave": true,
80       "editor.defaultFormatter": "eeyore.yapf"  # choose this extension
81   },
82   ```
83