23

The code

import win32com.client as win32 
Excel = win32.gencache.EnsureDispatch('Excel.Application') 

used to work, but now it produces the error:

AttributeError: 'module' object has no attribute 'CLSIDToPackageMap'

what's going on?

4 Answers 4

50

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

5
  • OMG... Simple as that!
    – Ferrarezi
    May 22, 2018 at 14:18
  • 25
    This worked! For me the relevant folder was "C:\Users\<my username>\AppData\Local\Temp\gen_py" Feb 5, 2019 at 18:03
  • 5
    I also had to restart my Jupyter kernel - other than that this worked a treat 👍 Oct 9, 2019 at 12:35
  • 3
    FTR @RafaelZayas's path is %TEMP% for most people.
    – Leo
    Aug 4, 2021 at 2:02
  • this has totally saved my life as well. I was so panicked! Mar 30, 2022 at 19:38
8

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.

1
  • 2
    I recently had the same problem, and deleting gen_py fixed it, with no recurrence. The gen_py folder is recreated, but whatever was causing the error was fixed when it was recreated. Nov 25, 2018 at 22:01
8

I found a more elegant solution on a Github discussion and incorporated it into a function. Worked for me.

def dispatch(app_name:str):
    try:
        from win32com import client
        app = client.gencache.EnsureDispatch(app_name)
    except AttributeError:
        # Corner case dependencies.
        import os
        import re
        import sys
        import shutil
        # Remove cache and try again.
        MODULE_LIST = [m.__name__ for m in sys.modules.values()]
        for module in MODULE_LIST:
            if re.match(r'win32com\.gen_py\..+', module):
                del sys.modules[module]
        shutil.rmtree(os.path.join(os.environ.get('LOCALAPPDATA'), 'Temp', 'gen_py'))
        from win32com import client
        app = client.gencache.EnsureDispatch(app_name)
    return app
1
  • This approach worked for me without having to manually delete cache files - thanks @pelelter
    – Rob Irwin
    Oct 20, 2021 at 18:16
-1

Thanks - worked like a charm after adding your function and making this change:

#excel = win32.gencache.EnsureDispatch('Excel.Application')    
excel = dispatch('Excel.Application')

Sincerely,

js

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.