Why am I suddenly getting a no attribute 'CLSIDToPackageMap' error with win32com.client?

After deleting C:\Temp\gen_py, the code above works again. Hope it can save trouble!


The main reason for this attribute error is because your COM-server has shifted from late-binding (dynamic) to early binding (static).

  • In Late Binding, whenever a method is called, the object is queried for the method and if it succeeds, then the call can be made.
  • In Early Binding, the information of the object model is determined in advance from type information supplied by the object call. Early binding makes use of MakePy. Also, early binding is case sensitive.

There are two ways to fix this issue:

  1. Use the dynamic module to force your code to work in a late-bound oriented way. Example use:

    "win32com.client.Dispatch()" instead of "win32.gencache.EnsureDispatch('Excel.Application')" 
    
  2. Use camelcase sensitive keywords for the early bound oriented way. Example use:

    "excel.Visible()" instead of "excel.VISIBLE()" or "excel.visible()"
    

I guess, the code works for the first run after deleting gen_py folder but from the second run throws an error as win32.gencache.EnsureDispatch being an early binding Dispatch, gen_py folder will be created again.