How do I enable auto code formatting for flake8 in PyCharm

你说的曾经没有我的故事 提交于 2019-12-22 02:05:42

问题


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.



来源:https://stackoverflow.com/questions/41899007/how-do-i-enable-auto-code-formatting-for-flake8-in-pycharm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!