"No module named os" when running Python Script in ArcGIS

4499
17
09-21-2010 12:49 PM
RobertWall
New Contributor
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
Reply
0 Kudos
17 Replies
LauraClemente
New Contributor
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!
Reply
0 Kudos
JamesSkurupey
New Contributor
I had the same error while doing the on line tutorial Basics of Python (for ArcGIS 10).  I typed:

arcpy

hit enter and got >>>

then was able to call import os

May have to do:

import arcpy
Reply
0 Kudos
KimOllivier
Regular Contributor II
The error message came back when I installed the patch for the overlay bug introduced in 10.0 SP 4. It took me a while to remember...

Delete C:\Python26\ArcGIS10.0 folder. (Empty except for a Desktop10.pth)

Start up the Python 2.6.6 installer python-2.6.6.exe and choose repair 2.6.6

Everything now back to normal.
Reply
0 Kudos
nacui
by
New Contributor
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.
Reply
0 Kudos
BarryGuidry
New Contributor III
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?
Reply
0 Kudos
BarryGuidry
New Contributor III
OK, our issue was that ArcGISSOC did not have permissions to the Python directory on the server.
Reply
0 Kudos
DerivenC
New Contributor II
Experienced this same issue when running the Batch Project tool on ArcGIS Desktop 10 with SP5.  I ran the Create File GDB tool and it went fine.  Found that odd.

The PYTHONPATH environment variable solved the problem.
Reply
0 Kudos
dcxia
by
New Contributor
easy,just add PATH as :
PATH=%PATH%:C:\Python26\ArcGIS10.0:C:\Python26\ArcGIS10.0\bin
Reply
0 Kudos