How do I enable auto code formatting for flake8 in

2019-04-20 12:29发布

问题:

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?

回答1:

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.



回答2:

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