Select to view content in your preferred language

Import Toolbox Problem with "umlaute"

3466
8
01-14-2011 05:58 AM
karinweixler
Deactivated User
Hello,

i would like to Import a Toolbox into my Python Script to use some of the functions from the Toolbox. The Problem is that I get the following error-message:

<type 'exceptions.UnicodeEncodeError'>: 'ascii' codec can't encode character u'\xfc' in position 9: ordinal not in range(128)

I gues the reason is, that some of the functions in the called Toolbox uses "Umlaute" in the naming. I tried to set the coding like this in my python script:

# -*- coding: iso-8859-15 -*-

but nothing changed. Does someone knows a way to Import a Toolbox in Python with "Umlaute"?

Tanks for helping.

Karin
Tags (2)
0 Kudos
8 Replies
AndrewChapkowski
Esri Regular Contributor
Try adding at the top of your imports
# -*- coding: cp1252 -*-

# Some other code


# Some simple example
print "ä"


I was able to use the 'import script' successfully at v10 sp1 after I added this.

Hope this helps!
0 Kudos
karinweixler
Deactivated User
Thanks for your reply.
Unfortunately this is not working with the Toolbox i like to import.
I also have installed AG 10 with ServicePack 1.
Do i have to consider something else?

Karin
0 Kudos
NiklasNorrthon
Frequent Contributor
This error has nothing to do with the toolbox, and everything to do with the python code, and the data it's reading. It is a pretty common problem among people that haven't yet grasped unicode and encodings.

The # -*- coding-comment at the first line tells the interpreter in which encoding the source code is saved. This might be related to the problem if the source contains non-ascii characters in string literals. But I suspect the problem lies in the source code somewhere, but it's impossible to say without seing relevant code and the complete traceback.

A very good link to read up on unicode and encodings: http://www.joelonsoftware.com/articles/Unicode.html

And here is some more specific information about unicode in python:
http://farmdev.com/talks/unicode/
0 Kudos
karinweixler
Deactivated User
Thanks for the hints and links.
If i have understand it correct, i have to know in which coding the toolbox was saved? But where and how do i tell python what encoding to use when importing the toolbox?
Thanks for your help!
Karin
0 Kudos
NiklasNorrthon
Frequent Contributor
No!

The toolbox is a container for system tools, script tools, models and toolsets. It's a binary ArcGIS file, and has nothing to do with the error message from the python interpreter.

What you need to know, is which encoding your python script is stored in, and if that is different from ascii, it should say so in the special coding comment in the script's first line. And you need to know the encoding of all data that you read or write.
0 Kudos
karinweixler
Deactivated User
Ok. But the thing is that the error occur at the following code:

arcpy.ImportToolbox(<Toolboxname>)

And the reason is that some of the modelnames in the toolbox are with umlaute. And i thought that this information (the models name and label) is saved in the toolbox? I can not change the names of the models, because it is not my toolbox.
0 Kudos
NiklasNorrthon
Frequent Contributor
Does the path to the toolbox name (including the its path) contain non-ascii characters?

If not: Can you reproduce the error with this two-liner:
import arcpy
arcpy.ImportToolbox(<Toolboxname>)

If so it's definitly an arcpy bug, and should be reported to esri. I can't think of a workaround right now.
0 Kudos
karinweixler
Deactivated User
No the path to the Toolbox does not contain non-ascii characters.
And Yes i can reproduce the error only with the 2-line code.
I also testet to import the Toolbox when i delete all the models with umlaute-name. Then it worked.
So unfortunally it looks like an arcpy.bug.
Thanks for helping and making the coding thing more clear!
0 Kudos