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)
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.
I used Robert's suggestions, in addition to reading through everyone's comments and have been able to clear up the original error ("No module named os") but now have a new error which reads: "<type 'exceptions.TypeError'>: cannot create weak reference to 'classobj' object"
Based upon the above statements I added a System Environment Variable called PYTHONPATH pointed at C:\Python27\Lib. I determined this location by doing exactly what he said (Opened python in a command prompt, imported the os module and then typed os.path to find out where ntpath.pyc was located because that is needed for importing the os module). Now, when I try to import os, arcpy or others (except math because that is always available) I receive the error "Cannot create weak reference to 'classobj' object" Does anyone have any suggestions?
I will try some of the other suggestions here that require more system changes but I do not have administrative privileges on my computer so that can be difficult to do when it may not work. Please let me know any suggestions! Thanks!
In fact, arcgis surely can import these modules, like os, sys and some other python core and the third-part modules. what you only need to do is import arcpy firstly, then import the other modules, though while importing the python core modules the autocompleting function not work.
I am connecting to the map / gp services remotely over a network. I have completed the steps in post 1 on my local machine, and still get the same error as in post 1. Do the steps to fixpath need to be completed on server machine, even though the gp service is referencing the toolbox / python script in a network directory? Must the toolbox also reside on, and run from, the server machine instead?