I have a chance to develop the server-side by Django with other developers. I collaborate with many developers, so we decide to use the static code analysis
flake8 to make the code same style and reduce the potential bugs.
In this blog post, we’ll see how to configure the
flake8 and how to use it on Django project.
- flake8: https://flake8.pycqa.org/
To use flake8 for Django, we need to install flake8. Execute the command below to install flake8.
pip install flake8
After installaing, don’t forget to save it on
pip freeze > requirements.txt
Done! we’ve installed flake8.
How to use flake8
We can execute the flake8 with the command below.
When I executed the flake8 on Django project folder, I can see the result like below.
./venv/lib/python3.8/site-packages/pyflakes/checker.py:153:31: F821 undefined name 'PercentFormat' ./venv/lib/python3.8/site-packages/pyflakes/checker.py:160:9: F821 undefined name 'Generator' ./venv/lib/python3.8/site-packages/pyflakes/checker.py:160:9: F821 undefined name 'PercentFormat' ./venv/lib/python3.8/site-packages/pyflakes/checker.py:180:47: F821 undefined name 'Optional' ./venv/lib/python3.8/site-packages/pyflakes/checker.py:759:35: F821 undefined name 'List' ./venv/lib/python3.8/site-packages/pyflakes/checker.py:760:35: F821 undefined name 'Dict'
the flake8 result is like
checker.py:153:31: F821 undefined name 'PercentFormat'. It is
FileName:ErroLocation ErrorID ErrorDetail.
How to configure flake8
virtualenv to create the virtual enviroment in Django project folder, so the flake8 analyzed
virtualenv and I got the error messages like above. It is unnecessary to analyze the viertualenv folder, so I need to configure flake8 to execlude the folder.
To ignore the virtualenv folder by flake8, create
.flake8 file in Django project folder and modify it like below.
[flake8] exclude = .git, .gitignore, *.pot, *.py[co], __pycache__, venv, .env
After configuring it, execute the command below to check the configuration works well.
We can see the result like below unlike previous results.
./petmeeting/settings.py:98:80: E501 line too long (91 > 79 characters) ./petmeeting/settings.py:101:80: E501 line too long (81 > 79 characters) ./petmeeting/settings.py:104:80: E501 line too long (82 > 79 characters) ./petmeeting/settings.py:107:80: E501 line too long (83 > 79 characters) ./petmeeting/settings.py:132:33: W292 no newline at end of file
The flake8 checks many things. But some rules is over-checking the code and makes errors. So I added the below to ignore some rules.
[flake8] exclude = .git, .gitignore, *.pot, *.py[co], __pycache__, venv, .env ignore = E121, E126, E127, E128, E203, E225, E226, E231, E241, E251, E261, E265, E302, E303, E305, E402, E501, E741, W291, W292, W293, W391, W503, W504, F403, B007, B950, max-line-length = 200
This rules are from Sider company’s recommend rules.
I think they don’t decide the rules simple. They study the rules and make the recommended rules based on the result of the research. You can see the details on the link below.
We’ve seen how to configure flake8 and how to use it on Djang project. Also, we’ve configured the flake8 to ignore unnecessary rules and folders. I hope the flake8 helps us to make same style of coding and reduce the potential bugs.
Was my blog helpful? Please leave a comment at the bottom. it will be a great help to me!
Deku. 지금 보고 계신 블로그를 작성하는
Deku가 개발한 앱을 한번 사용해보세요.
Dekucreated the applications with Flutter.
If you have interested, please try to download them for free.