Python is a general-purpose high-level programming language, designed to be easily read.
- 1 Python compared to other languages
- 2 Python in ArcGIS
- 3 Python and GDAL
- 4 References
- 5 Further Reading
Python compared to other languages
Python is said to be much shorter than other scripting languages such as Java and C++, but it requires longer run time, which can slow things down in large programs. Python's code is much more flexible as far as syntax goes compared to these "lower-level" languages. Many sources say that Python is 3-5 times shorter than Java and over 5 times shorter than C++. There is no definite say on which program is better. It is mostly a matter of preference and depends largely on the type of programs that are being written. One of the great advantages to Python is that it is free due to its open source license. It is also transferrable from multiple operating systems including Windows, Mac OS X, and Linux/Unix. It is also "ported to the Java and .NET virtual machines."
"Python can be written quickly and maintained easier than Java." However, it is important to keep in mind that, “Programming languages are tools, and different tools are appropriate for different jobs.”
Python in ArcGIS
Python was introduced to ArcGIS with version 9.0. A full ArcGIS installation includes Python, its standard libraries, and the NumPy package. Starting with ArcGIS 9.2, a compatible version of PythonWin is included in the ArcGIS distribution, but must be installed separately.
ESRI recommends using the version of Python (and additional packages) shipped together with the specific version of ArcGIS.
Python is used as the primary ArcGIS scripting language to perform geoprocessing tasks. It is capable of accessing all the ArcToolbox tools as well as the methods on the Geoprocessor Programming Model.
Python support in different versions of ArcGIS
The geoprocessor python object is accessed differently depending on the version of ArcGIS:
These versions of ArcGIS use Python 2.1 with the PythonWin package to access a Geoprocessor using a COM interface:
import win32com # PythonWin gp = win32com.client.Dispatch("esriGeoprocessing.GpDispatch.1")
The GpDispatch COM interface is available in later versions of ArcGIS, though it is only available to Python if PythonWin is installed.  GpDispatch provides access to geoprocessing using any language that supports COM.
This version of ArcGIS uses Python 2.4 and brought a major change to how the Geoprocessor was created. The win32com.client is no longer needed, replaced by the Python-native (non-COM)
arcgisscripting module. This both provides better performance and allows platform independence; Python geoprocessing scripts using the
arcgisscripting module are supported on non-Windows versions of ArcGIS Server.
import arcgisscripting gp = arcgisscripting.create()
These versions of ArcGIS use Python 2.5.1 and can be used with both the 9.3 version of
arcgisscripting or the original 9.2 version of
import arcgisscripting gp = arcgisscripting.create() # 9.2 (default for ArcGIS 9.2,9.3) gp = arcgisscripting.create(9.3) # 9.3
You can determine the version of the geoprocessor from its ScriptVersion property:
print "GP version: " + str(gp.ScriptVersion)
This version of ArcGIS adds the new
arcpy module. ArcPy is a site-package that builds on (and is a successor to) the arcgisscripting module. Its goal is to create the corner-stone for a useful and productive way to perform data analysis, data conversion, data management, and map automation with Python.
The older versions of the geoprocessor (GpDispatch and the
arcgisscripting module) are still supported to allow for forward compatibility of Python scripts.
Python Map Algebra in 10.0
import arcpy arcpy.CheckOutExtension("spatial") from arcpy.sa import * rasElevMeters = Raster("E:\\work\\elevfeet") * 0.3048
Python and GDAL
The GDAL/OGR libraries have python bindings. This means that you can use all of the GDAL/OGR functions and object classes via python. Being open source software, you don't have to worry about license availability, or not having the latest versions.
Installing the GDAL bindings
There are install instructions and tutorials at http://trac.osgeo.org/gdal/wiki/GdalOgrInPython, for both linux and windows systems.
Pre-packaged python+GDAL bundles
Python and GDAL are also included on the OSGeo Live DVDs, which can be tried out without the need for any installation.
- ESRI KB 31909: FAQ: Can both Python versions 2.1 and 2.4.1 be installed for use with ArcGIS 9.2?
- PythonWin distribution at sourceforge.net
- ESRI KB 31910: HowTo: Install PythonWin 2.4.1 and make correct file associations to work with ArcGIS 9.2
- Arc 10.0 Documentation: What is Map Algebra?