Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix importing newly-created modules on Python 3 #172

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions comtypes/client/_generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import sys
import comtypes.client
import comtypes.tools.codegenerator
import importlib

import logging
logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -143,6 +144,9 @@ def GetModule(tlib):
ofi = open(os.path.join(comtypes.client.gen_dir, modulename + ".py"), "w")
ofi.write(code)
ofi.close()
# clear the import cache to make sure Python sees newly created modules
if hasattr(importlib, "invalidate_caches"):
importlib.invalidate_caches()
return _my_import("comtypes.gen." + modulename)

def _CreateWrapper(tlib, pathname=None):
Expand Down Expand Up @@ -181,6 +185,9 @@ def _CreateWrapper(tlib, pathname=None):
setattr(comtypes.gen, modname, mod)
else:
ofi.close()
# clear the import cache to make sure Python sees newly created modules
if hasattr(importlib, "invalidate_caches"):
importlib.invalidate_caches()
mod = _my_import(fullname)
return mod

Expand Down