''webcolors' has no attribute 'css3_hex_to_names'

I am using this Function

def closest_colour(requested_colour):
    min_colours = {}
    for key, name in webcolors.css3_hex_to_names.items():
        r_c, g_c, b_c = webcolors.hex_to_rgb(key)
        rd = (r_c - requested_colour[0]) ** 2
        gd = (g_c - requested_colour[1]) ** 2
        bd = (b_c - requested_colour[2]) ** 2
        min_colours[(rd + gd + bd)] = name
    return min_colours[min(min_colours.keys())]

I am using Webcolor 1.11.1 Python 3.8.8 and I am getting this Error in line

for key, name in webcolors.css21_hex_to_names.items():

AttributeError: module 'webcolors' has no attribute 'css21_hex_to_names'



Solution 1:[1]

Luckily, I found the solution to this. Instead of webcolors.css21_hex_to_names I used this webcolors.CSS3_HEX_TO_NAMES. CSS3_HEX_TO_NAMES in Capitals

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Owais