With any programming language or script, there are always going to be times when things just don’t work. When that happens, what exactly can be done to fix it?
To fix the script, you first have to determine the exact problem, since it’s not always evident. Below are three issues or problem areas to check when your Python script is not functioning.
Problem 1: Error in the code
One of the most common problems when using a programming language is that there is an error somewhere in the code. The easiest way to check for an error is to find the error message (if any) that’s being returned. Most error messages are about a specific line in the code that is not working.
Finding out what the error message is can be very easy to do, especially when you’re running the script in an Integrated Development Environment (IDE) like PythonWin or Python IDLE. Unless you have programmed try and except statements* into the script, the script will output the error and the number of the line on which it occurred. This makes it very easy to track down.
Below is an example of what a typical error response looks like in PythonWin:
The first thing to look at is the line number. Note that it reports back two lines, but the one to look for is the one directly after the name of the script being used (Blog_samples.py in this case) – See Arrow 1. The line directly below that is the actual line from the code itself - See Arrow 2.
If the problem is with a tool or method, it may give you an ArcGIS error code that has information about the error – See Arrow 3. In the sample’s case, the file path did not exist, so the fix would be to check the path. If you are unsure about the tool errors, then you can search for the error message in ArcGIS Desktop Help by typing in the numeric code (for example, 000732). Help documents include possible solutions to each error.
Note: Be sure to double and triple check file paths or any long strings, as one typo will cause errors. When possible, copy/paste long strings to minimize typos.
Note: If the error reports a problem on Line 1, then it might not be the code but another dependency (for example, the module being imported does not exist).
*Why do try and except cause issues when reviewing error messages? Try and except are used for error checking and validation procedures, and if included without a way to output the error message, it can disguise the error, therefore making it very hard to find. For more information on Try/Except statements in error handling, see the ArcGIS Desktop 9.3 Help topic entitled, Error handling in Python.
Problem 2: Incorrect Python Version(s)
Python is a flexible language and allows multiple versions to be installed on one machine. This can cause problems when programs like ArcGIS require the use of a certain version, and the machine is trying to use a different one. Each version of ArcGIS requires a specific version of Python to be present on the machine and will install that version when ArcGIS Desktop, ArcGIS Engine, or ArcGIS Server is installed. Below is a table of ArcGIS versions and the respective Python version:
Okay, so now that we know what version we are supposed to have, how do we go about checking which version is being used? Every file type in Windows uses a default program to run that file type. In Python’s case, it is python.exe, which is typically located in the system folder where Python was installed. By default, ArcGIS installs its version in the C:PythonXX directory, and the python.exe is located in that directory.
To check which version of Python is being used, open Windows Explorer and go to Tools > Folder Options > File Types tab, or if you are using Windows Vista or 7, go to Control Panel > Default Programs > Associate a file type and find the PY extension listed there. Selecting it will tell you what program is being used to run that particular file type, and on Windows XP, clicking the “Advanced” button will allow you to see the path to that file under the “open” action.
File Type dialog boxes on Windows XP
In addition to the Python version being incorrect, there is also the possibility that the IDE version being used is incorrect. This is especially true with PythonWin. To verify which version of PythonWin is being used and to make sure it matches the correct ArcGIS version, start PythonWin with the Interactive Window open and it will display the version.
Problem 3: Missing dependencies or modules
When working in any programming language there are times when using outside libraries or modules is necessary. If you are using these libraries, the script needs to be able to find them in the defined location.
Python, by default, installs all the modules (PY files) in the Lib folder of the Python installation directory. If you are using a custom module, be sure to verify that it is installed in the correct location on the machine, as it can vary. For example, the arcgisscripting module is installed in the bin directory of the ArcGIS installation folder, not in the Python installation directory.
Remember that modules are just other PY, PYC, or PYD files on the machine and are added by the import statement in code.
In closing, remember that the ArcGIS user community and Esri Support Services, both in the U.S. and our international distributors, are here to help. Be sure to check out the Geoprocessing forum and the Script Gallery for tips and ideas. Happy Pythoning!
- James U., Support Analyst - SDK group, Esri Support Services