Giter Site home page Giter Site logo

Comments (8)

albertosottile avatar albertosottile commented on July 3, 2024

Thanks for this detailed investigation. In general, support for bundle_files <=2 is limited to packages in the Python standard library, see:

- <b>``bundle_files <=2``</b>: these values are supported only for packages in the Python standard library. Issues occurring with external packages and lower values of `bundle_files` will not be investigated.
)

because I simply do not have the capacity to provide support for the whole Python ecosystem.

That being said, you are more than welcome to submit a PR with patches for py2exe.hooks that are suitable for your needs.

from py2exe.

marunguy avatar marunguy commented on July 3, 2024

@albertosottile Thank you for your reply.

The frozen exe with bundle_files=0,1,2 (test.exe in this case) include both pythoncom38.dll and pywintypes38.dll.
(When the frozen exe is extracted using 7zip, there are pythoncom38.dll and pywintypes38.dll.)

Although the extension of pythoncom38.dll and pywintypes38.dll is .dll, these are Python extension modules.

How to import pythoncom38.dll and pywintypes38.dll as module dynamically from the frozen exe(test.exe) in runtime?

from py2exe.

albertosottile avatar albertosottile commented on July 3, 2024

DLL loading should be done automatically when you import win32com from Python, assuming that everything works. If you are looking for a test for pywin32, you can borrow the one I use in CI: https://github.com/py2exe/py2exe/blob/master/tests/functional/pywin32_test/pywin32_test.py

from py2exe.

marunguy avatar marunguy commented on July 3, 2024

I'm looking for a way to import pythoncom38.dll and pywintypes38.dll directly from frozen exe in runtime.

pywintypes_dll_mod = import_from_frozen("pywintypes38.dll", ...)

from py2exe.

albertosottile avatar albertosottile commented on July 3, 2024

I do not follow you, why would you want to load the DLL manually? The pywin32 package has to load the DLLs when needed. If you really want to do so, I'd inspect the code of pywin32 to check how the DLLs are loaded (I suspect ctypes is used for that).

EDIT: by looking at the code you pasted above, you should inspect _win32sysloader in the pywin32 code.

from py2exe.

marunguy avatar marunguy commented on July 3, 2024

Following is the reason to import pythoncom38.dll and pywintypes38.dll directly from frozen exe in runtime.

  • pywintypes38.dll is imported using __import_pywin32_system_module__ function pywintypes.py
  • pywintypes38.dll - pywintypes.py line 123
__import_pywin32_system_module__("pywintypes", globals())
        for look in sys.path:
            # If the sys.path entry is a (presumably) .zip file, use the
            # directory
            if os.path.isfile(look):
                look = os.path.dirname(look)
            found = os.path.join(look, filename)
            if os.path.isfile(found):
                break
  • Because pywintypes38.dll not in sys.path, ImportError raised. : pywintypes.py line 44-45
raise ImportError(
                "Module '%s' isn't in frozen sys.path %s" % (modname, sys.path)
  • But, ImportError exception is swallowd by py2exe.hooks.hook_pywintypes
  • As a result, pywintypes module don't have __import_pywin32_system_module__ attribute.
  • pythoncom38.dll - pythoncom.py line 4
pywintypes.__import_pywin32_system_module__("pythoncom", globals())
  • Because pywintypes module don't have __import_pywin32_system_module__ attribute, AttributeError is raised.
AttributeError: module 'pywintypes' has no attribute '__import_pywin32_system_module__'
  • If continue by copying pywintypes38.dll to sys.path, pywintypes38.dll is directly imported using absolute path - pywintypes.py line 110-114
    # Load the DLL.
    loader = importlib.machinery.ExtensionFileLoader(modname, found)
    spec = importlib.machinery.ModuleSpec(name=modname, loader=loader, origin=found)
    mod = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(mod)
  • So, I decide to try to replace __import_pywin32_system_module__ function.
  • And, It need a way to import pythoncom38.dll and pywintypes38.dll directly from frozen exe in runtime.

from py2exe.

albertosottile avatar albertosottile commented on July 3, 2024

But, ImportError exception is swallowd by py2exe.hooks.hook_pywintypes

Why do you think the hook is swallowing this exception? I cannot see an except clause in the hook nor in its chain. Unfortunately, I am on holiday and so I cannot run this myself directly. Can you show me a stack trace that points exactly where this exception is caught?

And, It need a way to import pythoncom38.dll and pywintypes38.dll directly from frozen exe in runtime.

In general, the code you pasted here:

    # Load the DLL.
    loader = importlib.machinery.ExtensionFileLoader(modname, found)
    spec = importlib.machinery.ModuleSpec(name=modname, loader=loader, origin=found)
    mod = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(mod)

should also work from "frozen exe in runtime". This works OOTB when bundle_files==3 because the DLL is in the file system, and it automatically makes use of zipextimporter when bundle_files<=2.

To facilitate debugging, I would advise you to start with bundle_files=2 (so that you can manually inspect the zip file) and adjust found to point to the path of e.g. pythoncom38.dll inside the zip file (e.g. C:\Users\yourname\Documents\test\dist\library.zip\pythoncom38.dll).

from py2exe.

SeaHOH avatar SeaHOH commented on July 3, 2024

#174 2eae517 may work with bundle_files<=2.

from py2exe.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.