Run model from model builder in python

6123
21
Jump to solution
06-22-2016 08:33 AM
GuillaumeGolay1
New Contributor

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

0 Kudos
1 Solution

Accepted Solutions
FreddieGibson
Occasional Contributor III

So it looks like you have a system tool and a model in your toolbox. The problem is that the model within your toolbox is named "Modèle". When you call ImportToolbox this will cause a conflict because identifiers in python 2.X don't support using he character è. If you remove the è the toolbox imports just fine.

2016-06-24_6-24-54.png

View solution in original post

21 Replies
BlakeTerhune
MVP Regular Contributor

It has to do with the path. the slash u is reserved to identify unicode characters so it's not interpreting your path correctly. Try changing all the path separators (slashes) to a back slash and put an r in the front. Like this...

r"C:\Users\w2sohi\Desktop\test\box.tbx"

Probably should do the same with your other paths.

Further reading on the subject. Filenames and file paths in Python

0 Kudos
DarrenWiens2
MVP Honored Contributor

Buuuuut, they had '/u' not '\u' which should be correct, correct?

0 Kudos
BlakeTerhune
MVP Regular Contributor

I agree. But from the looks of the error message, it's clearly saying there's a UnicodeEncodeError on the line with the path name. I don't know what else it could be.

0 Kudos
MicahBabinski
Occasional Contributor III

Hey Guillaume,

I do agree with Blake's advice on specifying the paths using the 'r'. It may also be useful to specify an alias name in your toolbox properties. I've found importing toolboxes is much more consistent that way. Then, you can ditch the second parameter. Good luck.

Micah

FreddieGibson
Occasional Contributor III

What are the names of the tools in your toolbox? Looking at the error I'd assume that you're using characters in the name of your tools that are invalid ascii characters. According to the error you're using u'\xe8', which should be equivalent to è.

DanPatterson_Retired
MVP Emeritus

To elaborate on Freddie's comment

>>> a = '\xe8' #, which should be equivalent to è.  # python 2.7
>>> a
'\xe8'
>>> print(a)


>>> a = '\xe8' #, which should be equivalent to è.  # python 3.5
>>> print(a)
è

compare line 5 (inviso line) to line 9

Python versions above 3 enforce unicode,  python 2.7 doesn't unless you 'u' it

0 Kudos
FreddieGibson
Occasional Contributor III

Hi Dan,

I'm thinking that their problem may be deeper than needing to make a string unicode. I would assume that if their tool contains unicode characters in the name it would run into a problem where Python 2.x does not support unicode identifiers.

https://docs.python.org/3.3/reference/lexical_analysis.html#identifiers

image3.png

Behind the scenes calling ImportToolbox wraps the tool within a class. This class uses the name of the tool for the name of the class (e.g. arcpy.RebuildAddressLocator_box or arcpy.box.RebuildAddressLocator in the users code). If the name of the tool were something like RèbuildAddrèssLocator I'd assume you'd get the error they're seeing because you can't create an identifier in python 2 with this name.

0 Kudos
DanPatterson_Retired
MVP Emeritus

that was the only thing that was confusing... the statement that everything worked in the model but not in python

0 Kudos
GuillaumeGolay1
New Contributor

Thank you all for helping me!

So, I've changed the paths, following Blake's advise,

add an alias "box" to my toolbox "box.tbx", following Micah's advise.

Now the code looks like this:

# Import arcpy module
import arcpy

# Load required toolboxes
arcpy.ImportToolbox(r"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)

But, the error messages still remain the sames:

Traceback (most recent call last):

  File "C:\Users\w2sohi\Desktop\test\recreer.py", line 13, in <module>

    arcpy.ImportToolbox(r"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)

Freddie and Dan, the name of the tool is: RebuildAddressLocator, Label is: Recréer le localisateur d'adresses (as I live in the french part of Switrzerland).

So, in any case, I don't find any "è"...

Is it possible that there's a problem with roots files as x86)\ArcGIS\Desktop10.1\arcpy\arcpy\toolbox_code.py or x86)\ArcGIS\Desktop10.1\arcpy\arcpy\__init__.py ? (as they are mentionned in the error messages)

Also, I've tried to run the code with only:

# Import arcpy module
import arcpy

# Load required toolboxes
arcpy.ImportToolbox(r"C:\Users\w2sohi\Desktop\test\box.tbx","box")

And also got the same error message. The problem seems to come from here...

0 Kudos