I have a Python 3.7 module that registers a COM server, exposing one method
to the Windows environment.

It works well when running from the ,py script (Python 3.7), and the
generated COM server also works fine.

Now I want to distribute it to users that do not have Python installed in
their machines, so I convert the module to an exe file (using
https://github.com/albertosottile/py2exe that ports py2exe from Py3.4 to
P3.7),

The registration of the COM works OK when running from the exe file, but
the COM is no longer functional.  This workflow used to work well with
pywin and py2exe in their old 2.7 versions.

The module:

 #!/usr/bin/python3
# -*- coding: utf-8 -*-

import win32com.client
import win32traceutil
import os, sys, imp
import pythoncom
import win32api

class COM_test:
    _reg_progid_ = "PythonZ.CondaTest"
    _reg_clsid_ = '{5937C658-485D-4B22-B17A-DDE89E2157F4}'
    _reg_desc_ = "COM server Py37"
    _public_methods_ = ['where_am_i']

    def __init__(self):
        self.dict = {}

    def where_am_i(self):
        if (hasattr(sys, "frozen") or imp.is_frozen("__main__")):
            hi = "COM server called from an EXE (FROZEN)"
        else:
            hi = "COM server called from a .PY (UNFROZEN)"
        print ("from server ==>", hi)
        return hi

if __name__=='__main__':
    print ("Registering COM Server")
    import win32com.server.register
    win32com.server.register.UseCommandLine(COM_test, debug=True)

the setup file (to be run as >python setup.py p2exe)
#!/usr/bin/python3
# -*- coding: utf-8 -*-

from distutils.core import setup
import py2exe

py2exe_options = dict(
    includes = [],
    packages = [],
    optimize=0,
    compressed=False,
    bundle_files=3,
    dist_dir='dist01',
    verbose = True
    )

setup(name="name",
      ctypes_com_server=[{ "modules": "COMbuilder_simpler",},],
      console=[{ "script": "COMbuilder_simpler.py",},],
      options={"py2exe": py2exe_options},
      )

Running the resulting exe file will register the COM.

And here a test script to request an object from the COM server and access
the public method:

import win32com.client
import pythoncom
import sys

def main():
    try:
        COMobj = win32com.client.DispatchEx('PythonZ.CondaTest')
        r = COMobj.where_am_i()
        print("from client  ==>",r)
    except:
        print("COM server failed")


if __name__=='__main__':
    main()


Any idea of what is going wrong with the registration of the COM server
from the exe file?

Appreciate any hint or direction indication

Enzo
_______________________________________________
python-win32 mailing list
python-win32@python.org
https://mail.python.org/mailman/listinfo/python-win32

Reply via email to