[python-win32] Recipe for using McMillan Installer and
win32all.win32com
Thorsten Henninger
henni at brainbot.com
Tue Dec 9 05:04:19 EST 2003
Hi all,
I ran into problems using the McMillan Installer (5b5) together with the
latest win32com (win32all package 163).
The problem is, that the McMillan Installer overwrites Python's import
method.
But win32com does some fancy stuff using Python's import to
automatically generate Python-modules while accessing COM-Objects such
as Outllook.
Well, thanks to Ralf Schmitt, who helped me out finding a solution.
First, we had to save the "old" import method from Python in McMillan's
iu.py module, before the import hook is assigned, for example:
__builtins__.__oldimport__ = __builtins__.__import__
To build a working Windows excetutable, one has to include the attached
Python-module (comhack) and call
set_gen_path(path)
at the beginning of the application, where path specifies the Path,
where the win32com module generates Python modules for COM-Obejevts.
Is it possible, that this solution will be integrated into the next
version of the win32com-package?
regards,
Thorsten
--
brainbot technologies AG
boppstrasse . 64 . 55118 mainz . germany
fon +49 6131 211639-1 . fax +49 6131 211639-2
http://brainbot.com/ mailto:henni at brainbot.com
-------------- next part --------------
#! /usr/bin/env python
"""
[$Id: comhack.py,v 1.2 2003/12/08 19:36:16 henni Exp $]
Use this in confuction with McMillan-Installer, if you need COM-Support in your application.
McMillan Installer as well as PythonCOM do overwrite the import Method for their needs.
_myimport advises the imports to either PythonCOM-Import or McMillan Import.
set_gen_path: use this method to specify the gencache-path, where all Python-COMObjects will be
automatically generated in.
One needs to Patch the McMillan Installer such that the Python-nativ import has to be saved as
__oldimport__
Use this hack in your application with
import comhack
set_gen_path(mypath)
Then everything will work ...
"""
import sys
import __builtin__
#save the import-Method (either McMillan's import or Python's)
mcimport = __builtin__.__import__
def _myimport(name, globals=None, locals=None, fromlist=None):
"""
Tell all modules to imported by McMillan's (or Python's) import.method,
besides win32com modules automatically genrated by win32com.gencache
"""
try:
#fails with ImportError, if McMillan has overwritten Python's native import
#and win32com.gen_py tries to generate a module
return mcimport(name, globals, locals, fromlist)
except ImportError, err:
if name.startswith('win32com.gen_py'):
#this is the Python-Native Import Method, if a patched McMillan-Installer exists
return __oldimport__(name, globals, locals, fromlist)
else:
#win32com needs this ImportError
raise err
def set_gen_path(path):
"""
Set the gencache Path
If not set, all Modules (win32com) will be generated to support/gen_py of your apllication.
"""
import os
import win32com
import win32com.gen_py
path = os.path.abspath(path)
if not os.path.exists(path):
os.makedirs(path)
#check, if McMillan runs ...
frozen = sys.__dict__.get("frozen", 0)
#set the gencache path
win32com.gen_py.__path__ = [path]
win32com.__gen_path__ = path
if not frozen:
return
#setup our import method, if McMillan runs.
__builtin__.__import__ = _myimport
More information about the Python-win32
mailing list