I am writing this to possibly help others who may come across this message when running in the ArcGIS Geoprocessor. Here is a test script to illustrate the issue related to importing the OS Module into a script running in ArcGIS (i.e., the script runs with no problem in PythonWin):
print "\n- - - - - - - - - - - - - - - - - - S T A R T - - - - - - - - - - - - - - - - - - - -\n"
# Import modules and create the geoprocessor object (gp)
import sys, os, arcgisscripting as ARC
gp = ARC.create(9.3)
print "\n- - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - -\n"
PythonWIN Results:
- - - - - - - - - - - - - - - - - - S T A R T - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - -
>>>
Geoprocessor Results:
Start Time: Wed Sep 15 16:21:21 2010
Running script testOS...
<type 'exceptions.ImportError'>: No module named os
Failed to execute (testOS).
End Time: Wed Sep 15 16:21:22 2010 (Elapsed Time: 1.00 seconds)
SOLUTION: The ntpath.pyc module, necessary for importing the os module, is not in the C:\Program Files\ArcGIS\bin path. To resolve this, if it happens to you, set the PYTHONPATH Environment variable to include the path to the same directory where PythonWin finds the ntuser.pyc module. You can determine this easily by going to the Interactive Window in PythonWin or IDLE and typing: import os; os.path. It will typically be in the following directory C:\Python25\Lib or something like that. Include that path as a value in your PYTHONPATH Environment variable (separate paths with semicolons) and see if this test script will run as a Script Tool in the ArcGIS Toolbox. It should work. It did for me.
Apparently, when running in PythonWin, the loader searches in the same directory where the python.exe is found and doesn't use (need) PYTHONPATH to resolve the reference. When running under the python.exe in the C:\Program Files\ArcGIS\bin it does need PYTHONPATH to resolve the reference. On the other hand, when running in PythonWin, the loader does use the PYTHONPATH Environment variable to resolve the arcgisscripting reference.
Robert