This blog post looks at how ArcPy is organized, in order to understand how Python deals with ArcGIS’ geospatial data and tools.
Site packages vs modules
ArcPy is a site package that allows for a close integration of ArcGIS and Python. Now, it is also often described as a python module, but technically this is not correct. So what is the difference between a site package and a module?
The answer has to do with hierarchy: a site package can be defined as a library of functions that add functionality to Python. One site package consists of multiple modules, that are, in turn, made of functions and classes. The confusion between site packages and modules is caused by the fact that they both work similarly: if you write ‘import arcpy’ into a Python shell or script, you are importing a site package that contains various modules as well – a site package acts as a wrapper of a series of modules. To understand more of ArcPy, it helps to know that it is organized into modules, functions, tools, classes and environments. These are covered next.
Tools available with arcpy
Almost all Python scripts start with the ‘import arcpy’ statement. This statement is necessary to access all geoprocessing tools found in the standard toolboxes installed with ArcGIS, more than 500 in total. But this is not all, you are also importing a number of modules and functions not found in ArcMap, adding value to the site package and software when you use them.
Working with modules in arcpy
The arcpy site package offers various modules, for example a map automation module (arcpy.mapping), an ArcGIS Network Analyst extension module (arcpy.na), a map algebra module (arcpy.sa) and a data access module (arcpy.da). It´s also possible to import only a module instead of the while arcpy site package, saving memory.
Scripts and modules both share the same file extension (.py), but their use is different: a script is generally a directly executable piece of code, run by itself and small in size. A module is generally a library, imported by other pieces of code (such as a script). Run by itself, it doesn´t do anything, that´s why you need to import it somewhere else.
Functions in arcpy
Functions have their origin in programming languages where they refer to a bit of functionality with a name that does a specific task. By giving a function a name, you can easily repeat it whenever necessary. The tools and toolboxes earlier are also functions as they match their definition. But arcpy has many more available functions, such as functions to describe data: something that comes very handy when you work outside of a graphical UI and in a text-based environment such as a Python editor.
Anyone familiar with the concept of object-oriented programming can tell you what a class is, but anyone new to Python probably won´t. A short explanation is that classes can be used to create objects, making your scripting work with arcpy easier. An example is the Point class in arcpy, which returns a single point object instead of an array of point objects. This saves you a lot of space and work – now you can simply refer to an object of four letters instead of a long array of numbers. The ArcGIS Help offers a list of all arcpy classes.