Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I enable auto code formatting for flake8 in PyCharm

Tags:

I use Tox to run unit tests, with a flake8 command that checks for code formatting errors. Each time I code in PyCharm, I run tox then realise I have a bunch of annoying formatting errors I have to back and manually fix. I would like PyCharm to automatically format the code (according to flake8 google for me each time it auto-saves after I stop typing.

my tox testenv looks like this:

[testenv:flake8] commands=flake8 <my_code_directory> deps =        flake8==2.4.1       flake8-import-order==0.11       pep8-naming==0.4.1   [flake8]  max-line-length = 120  import-order-style = google 

Is this possible? Do I have to download a specific plugin somewhere? If not with flake8, what about just PEP-8?

like image 889
Ryu S. Avatar asked Jan 27 '17 16:01

Ryu S.


People also ask

How to use flake8 as an external tool in PyCharm?

There is a way to use it as an external tool. First, make sure that flake8 is installed in the current environment. For a Windows venv project the path will be: Then, go to the PyCharm’s File menu and browse to File > Settings. In the “Settings” window, find Tools > External Tools and add a new external tool configuration.

How does flake8 interact with the formatter?

Flake8 interacts with the formatter in two ways: It creates the formatter and provides it the options parsed from the configuration files and command-line It uses the instance of the formatter and calls handle with the error. By default flake8.formatting.base.BaseFormatter.handle () simply calls the format method and then write.

How to auto format Your Python code?

How to Auto-Format Your Python Code with Black 1 Pylint and Flake8. Most Python developers enjoy using Pylint or Flake8 to check their code for errors and style guides. 2 Introduction to Black. By using Black, you agree to cede control over minutiae of hand-formatting. ... 3 Black in Jupyter Notebook. ... 4 Editor Integration. ...

How do I reformat code in PyCharm?

If you don't select a code fragment, PyCharm will reformat the whole file. Either open your file in the editor and press Ctrl+Alt+Shift+L or in the Project tool window, right-click the file and select Reformat Code.


2 Answers

Flake8 and import ordering are not auto-fixable in a way that complies with what you're seeing. You can auto-fix pep8 with autopep8.

There are discussions here about implementing this for Flake8 though.

like image 170
Ian Stapleton Cordasco Avatar answered Sep 17 '22 20:09

Ian Stapleton Cordasco


For automatically sorting import statements use isort. Consider using black to auto-format your Python code.

like image 45
jnns Avatar answered Sep 20 '22 20:09

jnns