Python: Create Python Module from existing Python Toolbox

3793
9
Jump to solution
02-01-2016 11:09 AM
PeterWilson
Occasional Contributor III

I posted the following article: HEC-GeoHMS: Importing HEC-GeoHMS Toolbox   asking for assistance in how to import HEC-GeoHMS into python. I received an email a while back from Christine Dartiguenave from the Arc Hydro Team, suggesting to run the following:

import arcpy
import arcpy.toolbox_code
arcpy.toolbox_code.generate_toolbox_module (r'C:\Program Files (x86)\ArcGIS\Desktop10.2\ArcToolbox\Toolboxes\GeoHMS Tools.tbx', r'C:\Projects\GeoHMSTools.py',False, False,True,'geohms')

When I try to import GeoHMSTools within Python I receive the following error message:

import GeoHMSTools
Runtime error 
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: No module named GeoHMSTools

I'm not sure if I'm meant to copy the python script that is created under C:\Projects i.e. GeoHMSTools.py into another directory, before trying to import it into Python.

Tags (3)
1 Solution

Accepted Solutions
Luke_Pinner
MVP Regular Contributor

Peter Wilson wrote:

I don't seem to have the following directory under C Drive: %AppData%\Python\Python27\site-packages.

You can create it and python will use it automatically (PEP370). I strongly recommend against putting stuff in the ArcGIS folder.

View solution in original post

9 Replies
Luke_Pinner
MVP Regular Contributor

Python doesn't know where to find C:\Projects\GeoHMSTools.py

A few ways to tell python where to look:

  • Save your python script to C:\Projects (python will look in the same directory as your script for any modules);
  • Append C:\Projects to your sys.path;
  • Install the module to your USER SITE directory (%AppData%\Python\Python27\site-packages)
DanPatterson_Retired
MVP Emeritus

Luke has made an important point should you wish to distribute the toolset.  His first point about about where python "look" when it needs to find stuff.  You can examine this aspect by placing two scripts in the same folder and having one script import functions etc from the other.  You can zip and ship everything in a folder and python will have no issues of doing the relative imports

PeterWilson
Occasional Contributor III

Hi Luke

Thanks for the following. I copied the GeoHMSTools.py under the following directory:

C:\Program Files (x86)\ArcGIS\Desktop10.2\ArcToolbox\Scripts

I was then able to import GeoHMSTools within Python, but when I try to run any of the tools Python crashes and I recieve the following error message:

GeoHMSTools.CentroidalLongestFlowpath(r'E:\Python\Temp\Model02\Model02.gdb\Layers\Subwatershed',r'E:\Python\Temp\Model02\Model02.gdb\Layers\Centroid' , r'E:\Python\Temp\Model02\Model02.gdb\Layers\LongestFlowPath_2D', r'E:\Python\Temp\Model02\Model02.gdb\Layers\CentroidalLongestFlowPath6')
Runtime error  Traceback (most recent call last):   File "<string>", line 1, in <module>   File "c:\program files (x86)\arcgis\desktop10.2\ArcToolbox\Toolboxes\GeoHMSTools.py", line 113, in CentroidalLongestFlowpath ExecuteError: System.Runtime.InteropServices.COMException (0x8002000A): Out of present range. (Exception from HRESULT: 0x8002000A (DISP_E_OVERFLOW))    at ESRI.ArcGIS.esriSystem.IStepProgressor.set_StepValue(Int32 Step)    at ESRI.APWR.HECGeoHMS10.GPCentroidalLongestFlowPath.CentroidalLongestFlowpathWk(ApLayers apLayers, Int32 debug, ITrackCancel& trackcancel, IGPMessages& messages, String& exMessage) Failed to execute (CentroidalLongestFlowpath).
Luke_Pinner
MVP Regular Contributor

I wouldn't put your script in the ArcGIS directory. You're better off moving it to %AppData%\Python\Python27\site-packages.

I think you should focus on your earlier question and try and get HEC-GeoHMS running from its toolbox. Instead of exporting a model, run the tool from the GUI, open the results window, right-click the result from the tool you just ran and select "copy as python snippet"

0 Kudos
PeterWilson
Occasional Contributor III

Hi Luke

Thanks for getting back to me. I don't seem to have the following directory under C Drive: %AppData%\Python\Python27\site-packages.

I've run the script tool within ArcMap and ArcCatalog with success. I tried Copy As Python Snippet and ran it within the ArcMap Python Interpreter:

import arcpy
import GeoHMSTools
arcpy.CentroidalLongestFlowpath_geohms(in_subbasin_features="E:/Python/Temp/Model02/Model02.gdb/Layers/Subwatershed",in_centroid_features="E:/Python/Temp/Model02/Model02.gdb/Layers/Centroid",in_longestflowpath_features="E:/Python/Temp/Model02/Model02.gdb/Layers/LongestFlowPath_2D",out_centroidallongestflowpath_raster="E:/Python/Temp/Model02/Model02.gdb/Layers/CentroidalLongestFlowPath1")
Runtime error 
Traceback (most recent call last):
  File "<string>", line 1, in <module>
AttributeError: 'module' object has no attribute 'CentroidalLongestFlowpath_geohms'

As you can see the syntax  is different for some reason or other, than when I call the tool from within the ArcMap Python Interpreter:

import arcpy
import GeoHMSTools
GeoHMSTools.CentroidalLongestFlowpath(r'E:\Python\Temp\Model02\Model02.gdb\Layers\Subwatershed', r'E:\Python\Temp\Model02\Model02.gdb\Layers\Centroid', r'E:\Python\Temp\Model02\Model02.gdb\Layers\LongestFlowPath_2D', r'E:\Python\Temp\Model02\Model02.gdb\Layers\CentroidalLongestFlowPath2')
Runtime error  Traceback (most recent call last):   File "<string>", line 1, in <module>   File "C:\Program Files (x86)\ArcGIS\Desktop10.2\ArcToolbox\Scripts\GeoHMSTools.py", line 125, in CentroidalLongestFlowpath     raise e ExecuteError: System.Runtime.InteropServices.COMException (0x8002000A): Out of present range. (Exception from HRESULT: 0x8002000A (DISP_E_OVERFLOW))    at ESRI.ArcGIS.esriSystem.IStepProgressor.set_StepValue(Int32 Step)    at ESRI.APWR.HECGeoHMS10.GPCentroidalLongestFlowPath.CentroidalLongestFlowpathWk(ApLayers apLayers, Int32 debug, ITrackCancel& trackcancel, IGPMessages& messages, String& exMessage) Failed to execute (CentroidalLongestFlowpath).

I have found that although the GeoHMSTools.CentroidalLongestFlowpath() failed, if I open the results window and open the failed script and rerun it, it completes successfully.

0 Kudos
Luke_Pinner
MVP Regular Contributor

Peter Wilson wrote:

I don't seem to have the following directory under C Drive: %AppData%\Python\Python27\site-packages.

You can create it and python will use it automatically (PEP370). I strongly recommend against putting stuff in the ArcGIS folder.

DanPatterson_Retired
MVP Emeritus

Nice PEP... I missed that one. Not finding anything for iOS since I use Pythonista for pure python,numpy,scipy stuff.

0 Kudos
PeterWilson
Occasional Contributor III

Hi Luke

I created the following directory and placed my GeoHMSTools.py module within the following directory. When I try to import GeoHMSTools module within Python it says it cant find the following module.

>>> import arcpy
>>> import GeoHMSTools
Runtime error 
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: No module named GeoHMSTools
>>>

I tried adding the path of the following directory to the environment settings, but it had no effect.

0 Kudos
PeterWilson
Occasional Contributor III

Hi Luke and Dan

I'm going to close the following as i feel the error messages that I'm receiving while trying to call Geo-HMS tools within Python are due to HEC-GeoHMS and not the Python Module that I've created. I've did a bit of research and found that ESRI has placed the ArcHydroTools.py module under the following directory:

C:\Program Files\ESRI\WaterUtils\ArcHydro\Bin

They generated a path file under the following directory that points to the module:

C:\Python27\ArcGISx6410.2\Lib\site-packages

So I've replicated the same for HEC-GeoHMS and I'm able to call GeoHMSTools within Python. The Centroidal Longest Flowpath tool still fails though and crashes Python. I'm going to open a new thread to see if anyone has gotten Centroidal Longest Flowpath to run from within Python. Thanks for all your assisstance.

0 Kudos