[GIS] Avoiding import arcpy error (RuntimeError: NotInitialized) when using IDLE

arcpyidle

ArcGIS Version: ArcGIS 10, Python 2.6.5
Machine: Windows 7 64bit (Intel)

I'm receiving the following error when importing arcpy using the the IDLE (gui and cmd prompt) and when using pythonwin.

I had removed python26 and python25 from my system and have done a fresh install of ArcGIS 10 and python. Therefore am calling IDLE from here: "C:\Python26\ArcGIS10.0\pythonw.exe"

The Error:

**IDLE 2.6.5

import arcpy
Traceback (most recent call last):
File "", line 1, in
import arcpy
File "C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy__init__.py", line 17, in
from geoprocessing import gp
File "C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\geoprocessing__init__.py", line 14, in
from _base import *
File "C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\geoprocessing_base.py", line 568, in
env = GPEnvironments(gp)
File "C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\geoprocessing_base.py", line 565, in GPEnvironments
return GPEnvironment(geoprocessor)
File "C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\geoprocessing_base.py", line 521, in init
self._refresh()
File "C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\geoprocessing_base.py", line 523, in _refresh
envset = (set(env for env in self._gp.listEnvironments()))
RuntimeError: NotInitialized**

EsriUK and Esri tech support have recommended fresh reinstall and registry edits = but this has not worked. I'm waiting on them for a solution.

I've investigated whether it was a module arcpy path issue. There are a couple of links on the Esri forums and on the ArcGIS help regarding paths. Also, a possible related thread on gis.stackexchange.

I followed the advise from 'kimo' on the Esri forum thread. But still the import error.

  1. My system variable (that I've edited) is:
    PYTHONPATH = "c:\python26";"C:\Python26\ArcGIS10.0";"C:\Python26\ArcGIS10.0\Lib\site-packages";"C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy"

  2. I have checked I have the contents of "C:\Python26\ArcGIS10.0\Lib\site-packages\Desktop10.pth". The contents are:
    C:\Program Files (x86)\ArcGIS\Desktop10.0\bin
    C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy
    C:\Program Files (x86)\ArcGIS\Desktop10.0\ArcToolbox\Scripts

However, the traceback message shows that a .py script is found in the folder: C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy. Therefore, suggests the arcpy module is found by the interpreter but then not initialized(?).

Arcpy can be called from the Python Window within ArcMap and ArcCatalog okay. It is only when called from a Python interpreter that the 'import arcpy' fails. Therefore, arcpy is initalized okay by ArcMap/ArcCatalog, but not IDLE – how can this be?

I previously had an install of python25, then python26 then after that upgraded to ArcGIS 10. Therefore, there was quite a lot of legacy python on my machine. However, I've uninstalled all and registry cleaned using ccleaner.

Can anyone suggest another line of enquiry as I'm not experienced with python enough to solve initialize/runtime errors? I'd like to avoid reinstalling the OS too!

Best Answer

Looking at your posts here and on the ESRI Forums, I think you have some muddled environment settings.

You want to put C:\Python26\ArcGIS10.0 in your system PATH variable, that gets you to the correct executables from IDLE, PythonWin or the command line.

Your PYTHONPATH could be empty. Or, you could just include the 3 stanzas of your Desktop10.pth file: C:\Program Files (x86)\ArcGIS\Desktop10.0\bin C:\Program Files; (x86)\ArcGIS\Desktop10.0\arcpy; C:\Program Files (x86)\ArcGIS\Desktop10.0\ArcToolbox\Scripts--those paths will then take precedence over the the default search for modules (below the python.exe or pythonw.exe being called).

Finally, since you've uninstalled and installed several versions of Python. At this point, there should be nothing under the C:\Python26 folder except the ArcGIS10.0 folder of the ESRI provided Python 2.6.5 32-bit install. If there is a Lib file there it is a residual of an earlier install and will interfere. It would be a bigger problem if any or your prior Python26 installs were 64-bit.

Also, since you're on 64-bit Windows 7, the Registry path the to 32-bit Python keys are HKLM\SOFTWARE\Wow6432Node\Python If you have a HKLM\SOFTWARE\Python, they would be Keys for a 64-bit install. They also would interfere with the 32-bit install.