Have you ever thought about formatting and styling your Python code consistently? Are you often running your code only to find simple, avoidable errors? Maybe you’ve even reached out for help from a Python linter or formatter, only to realize it is missing from the default Python environment for ArcGIS Pro. Well, whether you’ve heard of linting and autoformatting or not, this blog is for you.
If you answered yes to any of the above questions, you’ve likely come to the realization that readability of code matters. A considerable part of any programming task is reading code, whether to remember what it does, to find errors, or to comprehend it for the first time. For one, switching between various projects and finding problems is less difficult when the code is formatted consistently. Even more important, consistent formatting makes it easier for you and others to read your code. Clean, consistent code also presents itself professionally.
Avoiding errors is one thing, but who decides how Python code should be styled? Well, Python creator and “Benevolent Dictator for Life” (until 2018) Guido van Rossum also formulated the original Python Style Guide, which has since been expanded and morphed into PEP-8. And luckily, the community provides tools that help you find programming errors and simplify implementing the PEP-8 coding convention in the form of linters and formatters.
Linters and code formatters are a great tool for keeping your code clean and consistent with established coding conventions. A linter passively analyzes your code and alerts you to common programming and syntax errors. Many also provide rails for your formatting aberrations by identifying improper styling. On the other hand, a code formatter will actively restructure the appearance of your code to match style guidelines, but it won’t analyze your code for errors.
What does any of this have to do with ArcGIS Pro? If you’re writing scripts or large Python projects for ArcGIS Pro, you probably use the default Python environment, arcgispro-py3, as the interpreter for your preferred integrated development environment (IDE). Until now, the default environment did not contain a linter or code formatter. It used to be that if you wanted to use one, you had to first clone the default environment, and then install the linter or autoformatter. No more with ArcGIS Pro 2.7—it now includes open source linter flake8 and code formatter black.
How it works
Both flake8 and black help you format your code according to PEP-8. Take the example script below (if you want to follow along, save it to a local Python file like my_script.py):
The quickest way to see linting and autoformatting in action is to open the Python Command Prompt and enter the following commands (make sure that arcgispro-py3 is your active environment and you have ArcGIS Pro 2.7 or later installed):
Note that you’ll need to substitute your own path. You’ll see that programming and style errors will be printed below the flake8 command along with details on their location in the script. If you have the script open, you’ll note that the code hasn’t been changed. Now try running black:
You’ll see that after running black, your code has been reformatted to correct the styling. Try running the flake8 command again to see how many of the flagged formatting errors have been taken care of. Some of the remaining items are flags for unconventional programming practices, but some may still be formatting errors that black thinks you should fix yourself. Can you remove all of them?
Take the next steps
Of course, switching back and forth between your code and the command prompt to locate flagged errors is inconvenient. That’s why all but the simplest IDEs integrate linting and code formatting. Good IDEs will not only save you the steps of running the linter and autoformatter commands, but they will also directly highlight programming and style errors identified by your linter. Unfortunately, there are too many IDEs to choose from to get into detail here on setting them up, but most IDEs have excellent documentation on integrating linters and code formatters to help you get started. With flake8 and black now part of the default Python environment shipped with ArcGIS Pro 2.7, we’ve taken the first step for you, so make yourself a coffee (or tea), start up your preferred IDE, and get set up—you’ll be glad you did!