Hi,
I've built a model with model builder. When I run it from model builder it works but now that i've exported it to python, it doesn't work...
Here is my model:
# Import arcpy module
import arcpy
# Load required toolboxes
arcpy.ImportToolbox("C:/Users/w2sohi/Desktop/test/box.tbx","box")
# Local variables:
INTER = "C:\\Users\\w2sohi\\Desktop\\test\\INTER"
# Process: Recréer le localisateur d’adresses
arcpy.RebuildAddressLocator_box(INTER)
It seems that there's a problem with: arcpy.ImportToolbox("C:/Users/w2sohi/Desktop/test/box.tbx","box")
I have these error messages:
Traceback (most recent call last):
File "C:\Users\w2sohi\Desktop\test\recreer.py", line 13, in <module>
arcpy.ImportToolbox("C:/Users/w2sohi/Desktop/test/box.tbx","box")
File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\__init__.py", line 90, in ImportToolbox
return import_toolbox(input_file, module_name)
File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\toolbox_code.py", line 441, in import_toolbox
mymodule = generate_toolbox_module(toolbox, None, False, False, False, module_name, use_alt_alias)
File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\toolbox_code.py", line 399, in generate_toolbox_module
use_alt_alias))
File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\toolbox_code.py", line 391, in <genexpr>
code = u"\n".join(line if isinstance(line, unicode)
File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\toolbox_code.py", line 311, in code_for_toolbox
yield "__all__ = %r" % ([str(tool.name) for tool in toolbox.tools],)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe8' in position 3: ordinal not in range(128)
Thank you very much!
Guillaume
Solved! Go to Solution.
you should post it, since there is no line 13 in the code you posted and the encoding error is u'\xe8' . Also, do you have
# coding: utf-8
as the first line of your scripts?
Dear Dan,
I modified my script like this:
# Import arcpy module
import arcpy
# Local variables:
INTER = "C:\\Users\\w2sohi\\Desktop\\test\\INTER"
# Process: Recréer le localisateur d’adresses
arcpy.RebuildAddressLocator_geocoding(INTER)
And now it works! The problem came from arcpy.ImportToolbox but I don't know exactly what was happenning.
Thank you very much for your help
Guillaume
The syntax was wrong importing the toolbox:
import arcpy
from: ArcGIS Help (10.2, 10.2.1, and 10.2.2)
# Import custom toolbox
arcpy.ImportToolbox("c:/tools/My_Analysis_Tools.tbx")
try:
# Run tool in the custom toolbox. The tool is identified by
# the tool name and the toolbox alias.
arcpy.GetPoints_myanalysis("c:/data/forest.shp")
except arcpy.ExecuteError:
print(arcpy.GetMessages(2))
Can you explain what was wrong with the syntax?
Easier to call the toolbox and then run the tool (that is in the imported toolbox) by name as shown
Well, the help seems to indicate that you can do it either way, but perhaps it's wrong.
ImportToolbox (input_file, {module_name})
Parameter | Explanation | Data Type |
input_file | The geoprocessing toolbox added to the ArcPy site package. | String |
module_name | If the toolbox does not have an alias, the module_name is required. When a tool is accessed through the ArcPy site package, the toolbox alias where the tool is contained is a required suffix (arcpy.<toolname>_<alias>). Since ArcPy depends on toolbox aliases to access and execute the correct tool, aliases are extremely important when importing custom toolboxes. A good practice is to always define a custom toolbox's alias; however, if the toolbox alias is not defined, a temporary alias can be set as the second parameter. | String |
Hi Darren,
The documentation in this case would be correct. When you build a custom toolbox you are required to call ImportToolbox to load the toolbox. Think of this like when you create a custom class in python. You are require to call import to load the module into your project. When you import the toolbox you have two choices.
A lot of people forget to specify the alias within the toolbox properties. As such, they leverage the second option to ensure that the name of their toolbox is unique. For example, let's say that you called your tool Clip. There are currently two clip tools included with the base install of ArcGIS (i.e. Clip_analysis and Clip_management). Without specifying an alias the system wouldn't necessarily know which version of the clip function you're wanting. As such, you could specify an alias in the import of the tool, such as box, so that the system can easily identify that you can to use the Clip_box tool as opposed to other tools with the same name.
The reason why Guillame was able to get their code change to work is because they changed their code to leverage the system tool that is automatically loaded by arcpy. If they were to dig further into the system they would see that this particular toolbox uses "geocoding" as the alias and doesn't contain any invalid tool/model/script tool names.
Yes, I was saying you could do it either way and the original attempt wasn't a syntax error, but thanks for the further explanation.
Could you upload your toolbox so I can take a look at it?
Hi,
Here's a copy of the folder.
Thanks for helping me! (Just have to say to you that i solved my problem but i'm interesting about understanding why).
My solution was:
# Import arcpy module
import arcpy
# Local variables:
INTER = "C:\\Users\\w2sohi\\Desktop\\test\\INTER"
# Process: Recréer le localisateur d’adresses
arcpy.RebuildAddressLocator_geocoding(INTER)
So not calling the toolbox, and replace RebuildAddressLocator_box(INTER) with RebuildAddressLocator_geocoding(INTER)