Example 2. Printing the diff without change:
However, I don’t think reading the PEP 8 is a waste of time. Black may ignore some things that you still should pay attention to in order to have a clean style. Still, it is really good for general style revisions.
Some errors in the code won’t be noticed until execution, but they can be prevented with a meticulous review beforehand. The same happens with dead code that’s been left after some changes. Pyflakes and Vulture can help you with that.
Pyflakes is a passive code checker; it parses the code without executing it, so it won’t break anything. It points out variable conflicts and, in contrast to other checkers, it doesn’t complain about code style (we have black for that and more).
Vulture finds dead code: unused variables, functions and imports. But doesn’t Pyflakes include this? Not exactly, because Pyflakes follows the principle of trying as hard as possible to avoid false positives. Vulture, on the other hand, adds to each warning a percentage of confidence. In Python is hard to tell when some function is unused, because it’s a dynamic language, and in most cases, Pyflakes doesn’t warn you about them.
Output comparison:
Lastly, if you want your project to be used or modified by other programmers, it is better if they can install the dependencies directly, instead of installing them one by one, each one after a module not found error
. For this purpose you have to add a requirements.txt
file to your project.
You could do it manually (but that’s not fun) or use pip freeze
, but that writes all your installed packages, many more than necessary. Instead, using pipreqs, a requirements.txt file is generated with only the packages that your project uses.
Pipreqs example:
Now if someone clones your project, they can just use pip install -r requirements.txt
and the dependencies will be installed.
I hope you find some of these as useful as I did. I’m pretty sure there are other great tools out there to use in Python projects. Do you know any?