Python Forum

Full Version: Updating Code And Having Issue With Keys
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello, I'm currently updating some code from a plug-in for Maya (the animation software) from Python 2 to Python 3 and noticed I was having some issues with keys... specifically I am getting an error message saying " dict_keys' object has no attribute 'sort' "

I wound up tracing this to code on line 191-192 which states:

shapes = CONTROL_SHAPES.keys()
shapes.sort()
I am struggling to figure out what is going on? I have looked on a few forums and the closest thin to an answer I have found so far was to retype 191 as:

shapes = list(CONTROL_SHAPES.keys())
However, that has not yielded any results for me. Does anyone here have any idea as to what part of this code might need to be updated or how I might go about it?
In python2 dict.keys(), dict.values() and dict.items() return list objects. In python3 they return dict view objects that are lazily evaluated and thus don't have .sort() method.

I don't know what you mean by

(May-23-2023, 04:51 AM)Xileron Wrote: [ -> ]that has not yielded any results for me.
What you did is one option (i.e. turning the view object into a list and thus getting the same as in python2), but depending on what you do with shapes down the line there might be better approach, i.e. without constructing the whole list in memory
This should work with Python 2.7 and 3.x:

shapes = sorted(CONTROL_SHAPES.keys())
I am merely updating some code that was stuck in Python 2. I'm happy to send the file over if you would like to see the scripts I'm referencing in greated detail if need be though.

Also, I tried the write shapes = sorted(CONTROL_SHAPES.keys()) however this has not worked out on my end.
I meant that this had not worked for me. It had not fixed what was wrong with the code and I am still getting the same error message:

'dict_keys' object has no attribute 'sort'
Try something along the lines of
shapes = list(CONTROL_SHAPES.keys())
Wait, I apologize. @DeaD_EyE's approach actually was the solution. I found out I just needed to close and open Maya for it to register the changes in the code. Thanks for all the help
(May-24-2023, 03:54 AM)Xileron Wrote: [ -> ]I found out I just needed to close and open Maya for it to register the changes in the code.
I guess your approach didn't work for the same reason
(May-23-2023, 04:51 AM)Xileron Wrote: [ -> ]Hello, I'm currently updating some code from a plug-in for Maya (the animation software) from Python 2 to Python 3 and noticed I was having some issues with keys... specifically I am getting an error message saying " dict_keys' object has no attribute 'sort' "

I wound up tracing this to code on line 191-192 which states:

shapes = CONTROL_SHAPES.keys()
shapes.sort()
I am struggling to figure out what is going on? I have looked on a few forums and the closest thin to an answer I have found so far was to retype 191 as:

shapes = list(CONTROL_SHAPES.keys())
However, that has not yielded any results for me. Does anyone here have any idea as to what part of this code might need to be updated or how I might go about it?

The issue you're facing is related to changes in Python 3's dictionary behavior. In Python 3, the keys() method of a dictionary returns a dict_keys object, which is a view object that provides a dynamic view of the dictionary's keys. The dict_keys object does not have a sort() method because it does not support in-place sorting.

To resolve this issue and sort the keys, you can convert the dict_keys object to a list using the list() function, and then call the sort() method on the list. Here's an updated version of your code that should work in Python 3:

shapes = list(CONTROL_SHAPES.keys())
shapes.sort()

Alternatively, you can use the sorted() function, which returns a new sorted list without modifying the original dict_keys object. Here's how you can achieve the same result using sorted():

shapes = sorted(CONTROL_SHAPES.keys())

Both of these approaches will give you a sorted list of keys from the CONTROL_SHAPES dictionary in Python 3.