Comments (8)
Thanks for this detailed investigation. In general, support for bundle_files <=2
is limited to packages in the Python standard library, see:
Line 69 in d34da51
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.
@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.
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.
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.
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.
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.pypywintypes38.dll
- pywintypes.py line 123
__import_pywin32_system_module__("pywintypes", globals())
- search
pywintypes38.dll
insys.path
: pywintypes.py line 35-42
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 insys.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 bypy2exe.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
tosys.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
andpywintypes38.dll
directly from frozen exe in runtime.
from py2exe.
But,
ImportError
exception is swallowd bypy2exe.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
andpywintypes38.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.
#174 2eae517 may work with bundle_files<=2
.
from py2exe.
Related Issues (20)
- AssertionError with Python embeddable package HOT 1
- Py_OptimizeFlag not set correctly during Python initialization
- OSError: [WinError 87] The parameter is incorrect. HOT 2
- PYTHONPATH module location HOT 1
- Exe is created by doesn't run (opens then closes)
- Simple pygame example tries to lookup files inside of `library.zip` HOT 1
- Support Python 3.12 HOT 25
- Impossible to specify different version info for each of the binaries with the new freeze API. HOT 3
- pynput import break the file HOT 1
- PYTHONIOENCODING is ignored
- pendulum package doesn't initialize when unpacking from zipfile
- add version for python 3.12 too
- usage missing
- Does py2exe support 'QtWebEngineWidgets' of PySide6?
- How do I get an app using jaydebapi to work as an executable ?
- Failure to import zipextimporter with error "No module named '_memimporter'" HOT 1
- [freeze][zipfile] Dependent packages and modules are not zipped to library.zip or packed to .exe bin
- ImportError: MemoryLoadLibrary failed loading PyQt5\QtWidgets.pyd: The specified module could not be found. (126)
- no open .whl in windows
- `setuptools.dep_util` is deprecated and will be removed soon. Alternative suggested is `setuptools.modified`. HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from py2exe.