After deleting C:\Temp\gen_py, the code above works again. Hope it can save trouble!
PS. Or, you can try searching gen_py in your %TEMP% folder;
python 3.x - Why am I suddenly getting a no attribute 'CLSIDToPackageMap' error with win32com.client? - Stack Overflow
python-win32com excel com model started generating errors - Stack Overflow
AttributeError: module 'win32com.gen_py.00020813-0000-0000-C000-000000000046x0x1x9' has no attribute 'CLSIDToClassMap'
Weird errorr trying to read outlook calendar
After deleting C:\Temp\gen_py, the code above works again. Hope it can save trouble!
PS. Or, you can try searching gen_py in your %TEMP% folder;
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
I had the same issue and I resolved it by following the instructions here: https://mail.python.org/pipermail/python-win32/2007-August/006147.html
Deleting the gen_py output directory and re-running makepy SUCCEEDS and subsequently the test application runs OK again.
So the symptom is resolved, but any clues as to how this could have happened. This is a VERY long running application (think 24x7 for years) and I'm concerned that whatever caused this might occur again.
To find the output directory, run this in your python console / python session:
import win32com
print(win32com.__gen_path__)
or, even better, a one-liner in the command line:
python -c "import win32com; print(win32com.__gen_path__)"
Based on the exception message in your post, the directory you need to remove will be titled '00020813-0000-0000-C000-000000000046x0x1x9'. So delete this directory and re-run the code. And if you're nervous about deleting it (like I was) just cut the directory and paste it somewhere else.
💡Note that this directory is usually in your "TEMP" directory (copy-paste %TEMP%/gen_py in Windows File Explorer and you will arrive there directly).
I have no idea why this happens nor do I know how to prevent it from happening again, but the directions in the link I provided seemed to work for me.
A more straightforward solution was posted in a related question Issue in using win32com to access Excel file.
Basically, you just need to delete the folder C:\Users\<your username>\AppData\Local\Temp\gen_py and rerun your code.
💡TIP: You can also put in your Windows file explorer %TEMP%\gen_py to access it directly, and then delete its content.
I have written a program to read my outlook calendar (and write it to my google calendar), mostly just to practice some python/learn from it/for fun. Now this was all fun and games and it worked, although it needed some improvements.
However it stopped beging fun and games when it decided to break itself. Literally. I didn't chage a letter of code and suddenly it returns an errormessage.
The faulty bit of code seems to be:
import win32com.client
def read_outlook_calendar(some arguments):
Outlook = win32com.client.Dispatch("Outlook.Application")
more of this functionwhere this worked before, it now returns the following error:
File "syncal2.py", line 17, in <module>
appointments = fun.read_outlook_calendar(begin = begin, end = end)
File "C:\Users\Gebruiker\Documents\Agendaproject\tidy\fun.py", line 8, in read_outlook_calendar
Outlook = win32com.client.gencache.EnsureDispatch("Outlook.Application")
File "C:\Users\Gebruiker\anaconda3\lib\site-packages\win32com\client\gencache.py", line 527, in EnsureDispatch
disp = win32com.client.Dispatch(prog_id)
File "C:\Users\Gebruiker\anaconda3\lib\site-packages\win32com\client\__init__.py", line 96, in Dispatch
return __WrapDispatch(dispatch, userName, resultCLSID, typeinfo, clsctx=clsctx)
File "C:\Users\Gebruiker\anaconda3\lib\site-packages\win32com\client\__init__.py", line 37, in __WrapDispatch
klass = gencache.GetClassForCLSID(resultCLSID)
File "C:\Users\Gebruiker\anaconda3\lib\site-packages\win32com\client\gencache.py", line 183, in GetClassForCLSID
mod = GetModuleForCLSID(clsid)
File "C:\Users\Gebruiker\anaconda3\lib\site-packages\win32com\client\gencache.py", line 226, in GetModuleForCLSID
mod = GetModuleForTypelib(typelibCLSID, lcid, major, minor)
File "C:\Users\Gebruiker\anaconda3\lib\site-packages\win32com\client\gencache.py", line 266, in GetModuleForTypelib
AddModuleToCache(typelibCLSID, lcid, major, minor)
File "C:\Users\Gebruiker\anaconda3\lib\site-packages\win32com\client\gencache.py", line 552, in AddModuleToCache
dict = mod.CLSIDToClassMap
AttributeError: module 'win32com.gen_py.00062FFF-0000-0000-C000-000000000046x0x9x6' has no attribute 'CLSIDToClassMap'
So yeah, how do I unfuck this?