faqts : Computers : Programming : Languages : Python : Snippets

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

3 of 3 people (100%) answered Yes
Recently 2 of 2 people (100%) answered Yes

Entry

Using win32com without registry (magic_import)

Jul 5th, 2000 10:01
Nathan Wallace, Hans Nowak, Snippet 218, Mark Hammond


"""
Packages: operating_systems.windows
"""
"""
> Actually about a month ago, you sent me e-mail saying that you had posted
> code that would make it possible to use win32com without having to use the
> registry and that I could find it using DejaNews.  I was unable to find it
> (I thought because of my own incompetence) so if you have it handy I'd
> appreciate a re-post.
Actually, the post I am referring to was about 6 months ago (which
still doesnt mean it made it :-(.  Here it is again:
"""
import win32api, imp, sys
def magic_import(modulename, filename):
	# win32 can find the DLL name.
	h = win32api.LoadLibrary(filename)
	found = win32api.GetModuleFileName(h)
	# Python can load the module
	mod = imp.load_module(modulename, None, found, ('.dll', 'rb', 
      imp.C_EXTENSION))
	# inject it into the global module list.
	sys.modules[modulename] = mod
	# And finally inject it into the namespace.
	globals()[modulename] = mod
	win32api.FreeLibrary(h)
try:
	import pywintypes
except ImportError:
	magic_import("pywintypes", "pywintypes15.dll")
try:
	import pythoncom
except ImportError:
	magic_import("pythoncom", "pythoncom15.dll")