PyOxidizer icon indicating copy to clipboard operation
PyOxidizer copied to clipboard

No module named 'win32api' when importing win32com

Open AngelEzquerra opened this issue 4 years ago • 2 comments

I am trying to use Pyoxidizer to build a python program that tries to use the win32com module from pywin32. As soon as I try to import win32com I get the following error:

No module named 'win32api'

Is this there a way to fix this problem?

AngelEzquerra avatar Sep 07 '21 18:09 AngelEzquerra

pywin32 relies on .pth files and dlls to provide modules, so it won't work out of the box. I was able to get things running by calling the following code prior to trying to import a pywin32 module:

import os
import site
import sys


def fix_pywin32_in_frozen_build() -> None:
    if sys.platform != "win32" or not getattr(sys, "frozen", False):
        return

    site.addsitedir(sys.path[0])

    # sys.path has been extended; use final
    # path to locate dll folder and add it to path
    path = sys.path[-1]
    path = path.replace("Pythonwin", "pywin32_system32")
    os.environ["PATH"] += ";" + path

    # import pythoncom module
    import importlib
    import importlib.machinery

    for name in "pythoncom", "pywintypes":
        filename = os.path.join(path, name + "39.dll")
        loader = importlib.machinery.ExtensionFileLoader(name, filename)
        spec = importlib.machinery.ModuleSpec(name=name, loader=loader, origin=filename)
        _mod = importlib._bootstrap._load(spec)  # type: ignore

sys.path[0] is the external path where the pywin32 files were stored (eg using pyoxidizer's 'files' mode or a resource callback hook, and python_config.module_search_paths = ["$ORIGIN/ext"]).

dae avatar Oct 12 '21 09:10 dae

sys.path[0] is the external path where the pywin32 files were stored (eg using pyoxidizer's 'files' mode or a resource callback hook, and python_config.module_search_paths = ["$ORIGIN/ext"]).

Could you give example .bzl file that will make pywin32 work with pyoxidizer?

graf0 avatar Jan 28 '23 12:01 graf0