Deploy-ng: ImportError: DLL load failed: The specified module could not be found

hi,
when trying to run the executable built by pfreeze tool, i got the following error:

Traceback (most recent call last):
  File "game.py", line 4, in <module>
    from functions import *
  File "E:\projects\python\examplegame\lib\importlib\_bootstrap.py", line 983, in _find_and_load
    return _find_and_load_unlocked(name, import_)
  File "E:\projects\python\examplegame\lib\importlib\_bootstrap.py", line 967, in _find_and_load_unlocked
    module = _load_unlocked(spec)
  File "E:\projects\python\examplegame\lib\importlib\_bootstrap.py", line 677, in _load_unlocked
    spec.loader.exec_module(module)
  File "E:\projects\python\examplegame\lib\importlib\_bootstrap.py", line 819, in exec_module
    exec(code, module.__dict__)
  File "E:\projects\python\the life of crime\functions.py", line 5, in <module>
    from win32api import *
  File "E:\projects\python\examplegame\lib\importlib\_bootstrap.py", line 983, in _find_and_load
    return _find_and_load_unlocked(name, import_)
  File "E:\projects\python\examplegame\lib\importlib\_bootstrap.py", line 967, in _find_and_load_unlocked
    module = _load_unlocked(spec)
  File "E:\projects\python\examplegame\lib\importlib\_bootstrap.py", line 670, in _load_unlocked
    module = module_from_spec(spec)
  File "E:\projects\python\examplegame\lib\importlib\_bootstrap.py", line 583, in module_from_spec
    module = spec.loader.create_module(spec)
  File "E:\projects\python\examplegame\lib\importlib\_bootstrap_external.py", line 1043, in create_module
    _imp.create_dynamic, spec)
  File "E:\projects\python\examplegame\lib\importlib\_bootstrap.py", line 219, in _call_with_frames_removed
    return f(*args, **kwds)
ImportError: DLL load failed: The specified module could not be found.

of course it works with the default python interpreter.
this is the build log:

running build_apps
Building platforms: win32,win_amd64
Gathering wheels for platform: win32
Looking in indexes: https://pypi.org/simple, https://archive.panda3d.org/simple/opt
Collecting panda3d (from -r E:\projects\python\examplegame\requirements.txt (line 1))
  File was already downloaded e:\projects\python\examplegame\build\__whl_cache__\panda3d-1.10.2-cp37-cp37m-win32.whl
Successfully downloaded panda3d
Could not find an optimized wheel (using index https://archive.panda3d.org/simple/opt) for platform: win32
Building runtime for platform: win32
warning: build_apps: could not find dependency VERSION.dll (referenced by python37.dll)

warning: build_apps: could not find dependency VCRUNTIME140.dll (referenced by python37.dll)

warning: build_apps: could not find dependency VCRUNTIME140.dll (referenced by deploy-stub.exe)

warning: build_apps: could not find dependency SETUPAPI.dll (referenced by libpanda.dll)

warning: build_apps: could not find dependency d3dx9_29.dll (referenced by libpandadx9.dll)

warning: build_apps: could not find dependency CRYPT32.dll (referenced by avformat-55.dll)

warning: build_apps: could not find dependency pywintypes37.dll (referenced by win32event.pyd)

warning: build_apps: could not find dependency VERSION.dll (referenced by win32api.pyd)

warning: build_apps: could not find dependency CRYPT32.dll (referenced by _ssl.pyd)

warning: build_apps: could not find dependency UxTheme.dll (referenced by mfc140u.dll)

warning: build_apps: could not find dependency WINSPOOL.DRV (referenced by win32ui.pyd)

There are some missing modules: ['__builtin__', '_posixsubprocess', 'appscript', 'dummy.Process', 'espeak.core', 'exceptions', 'grp', 'libloader.load_library', 'multiprocessing.AuthenticationError', 'multiprocessing.BufferTooShort', 'multiprocessing.TimeoutError', 'multiprocessing.get_context', 'multiprocessing.get_start_method', 'multiprocessing.set_start_method', 'sets', 'sitecustomize', 'win32com.gen_py']
Copying game files for platform: win32
Gathering wheels for platform: win_amd64
Looking in indexes: https://pypi.org/simple, https://archive.panda3d.org/simple/opt
Collecting panda3d (from -r E:\projects\python\examplegame\requirements.txt (line 1))
  File was already downloaded e:\projects\python\examplegame\build\__whl_cache__\panda3d-1.10.2-cp37-cp37m-win_amd64.whl
Successfully downloaded panda3d
Could not find an optimized wheel (using index https://archive.panda3d.org/simple/opt) for platform: win_amd64
Building runtime for platform: win_amd64
warning: build_apps: could not find dependency VERSION.dll (referenced by python37.dll)

warning: build_apps: could not find dependency VCRUNTIME140.dll (referenced by python37.dll)

warning: build_apps: could not find dependency VCRUNTIME140.dll (referenced by deploy-stub.exe)

warning: build_apps: could not find dependency SETUPAPI.dll (referenced by libpanda.dll)

warning: build_apps: could not find dependency d3dx9_43.dll (referenced by libpandadx9.dll)

warning: build_apps: could not find dependency CRYPT32.dll (referenced by avformat-55.dll)

warning: build_apps: could not find dependency CRYPT32.dll (referenced by _ssl.pyd)

warning: build_apps: could not find dependency VERSION.dll (referenced by win32api.pyd)

warning: build_apps: could not find dependency UxTheme.dll (referenced by mfc140u.dll)

warning: build_apps: could not find dependency WINSPOOL.DRV (referenced by win32ui.pyd)

Copying game files for platform: win_amd64

i think the issue is with pywintypes.dll and pythoncom.dll, since pywin32 uses them as far as i know.
and, of course here is my setup script:

from setuptools import *

opts = {
"build_apps":{
"console_apps":{
"game":"game.py"
},
"platforms":["win32", "win_amd64"],
"include_patterns":["sounds.dat"],
"plugins":["pandadx9", "pandagl", "p3openal_audio", "p3ffmpeg"]
}
}

setup(name="game", options=opts)

thanks.

Does the problem go away if you manually copy over the pywin32 dlls into the build folder?

hi,
yes, it did the trick, but another problem with win32com.gen_py:

Traceback (most recent call last):
  File "E:\projects\python\THELIF~1\lib\site-packages\win32com\__init__.py", line 89, in <module>
    import win32com.gen_py
  File "E:\projects\python\THELIF~1\lib\importlib\_bootstrap.py", line 983, in _find_and_load
    return _find_and_load_unlocked(name, import_)
  File "E:\projects\python\THELIF~1\lib\importlib\_bootstrap.py", line 965, in _find_and_load_unlocked
    raise ModuleNotFoundError(_ERR_MSG.format(name), name=name)
ModuleNotFoundError: No module named 'win32com.gen_py'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "loc.py", line 26, in <module>
    from globals import *
  File "E:\projects\python\THELIF~1\lib\importlib\_bootstrap.py", line 983, in _find_and_load
    return _find_and_load_unlocked(name, import_)
  File "E:\projects\python\THELIF~1\lib\importlib\_bootstrap.py", line 967, in _find_and_load_unlocked
    module = _load_unlocked(spec)
  File "E:\projects\python\THELIF~1\lib\importlib\_bootstrap.py", line 677, in _load_unlocked
    spec.loader.exec_module(module)
  File "E:\projects\python\THELIF~1\lib\importlib\_bootstrap.py", line 819, in exec_module
    exec(code, module.__dict__)
  File "E:\projects\python\the life of crime\globals.py", line 1, in <module>
    from accessible_output2.outputs import auto
  File "E:\projects\python\THELIF~1\lib\importlib\_bootstrap.py", line 983, in _find_and_load
    return _find_and_load_unlocked(name, import_)
  File "E:\projects\python\THELIF~1\lib\importlib\_bootstrap.py", line 967, in _find_and_load_unlocked
    module = _load_unlocked(spec)
  File "E:\projects\python\THELIF~1\lib\importlib\_bootstrap.py", line 677, in _load_unlocked
    spec.loader.exec_module(module)
  File "E:\projects\python\THELIF~1\lib\importlib\_bootstrap.py", line 819, in exec_module
    exec(code, module.__dict__)
  File "E:\projects\python\THELIF~1\lib\site-packages\accessible_output2\outputs\__init__.py", line 5, in <module>
    from . import jaws
  File "E:\projects\python\THELIF~1\lib\importlib\_bootstrap.py", line 1035, in _handle_fromlist
    _call_with_frames_removed(import_, from_name)
  File "E:\projects\python\THELIF~1\lib\importlib\_bootstrap.py", line 219, in _call_with_frames_removed
    return f(*args, **kwds)
  File "E:\projects\python\THELIF~1\lib\importlib\_bootstrap.py", line 983, in _find_and_load
    return _find_and_load_unlocked(name, import_)
  File "E:\projects\python\THELIF~1\lib\importlib\_bootstrap.py", line 967, in _find_and_load_unlocked
    module = _load_unlocked(spec)
  File "E:\projects\python\THELIF~1\lib\importlib\_bootstrap.py", line 677, in _load_unlocked
    spec.loader.exec_module(module)
  File "E:\projects\python\THELIF~1\lib\importlib\_bootstrap.py", line 819, in exec_module
    exec(code, module.__dict__)
  File "E:\projects\python\THELIF~1\lib\site-packages\accessible_output2\outputs\jaws.py", line 3, in <module>
    from libloader.com import load_com
  File "E:\projects\python\THELIF~1\lib\importlib\_bootstrap.py", line 983, in _find_and_load
    return _find_and_load_unlocked(name, import_)
  File "E:\projects\python\THELIF~1\lib\importlib\_bootstrap.py", line 967, in _find_and_load_unlocked
    module = _load_unlocked(spec)
  File "E:\projects\python\THELIF~1\lib\importlib\_bootstrap.py", line 677, in _load_unlocked
    spec.loader.exec_module(module)
  File "E:\projects\python\THELIF~1\lib\importlib\_bootstrap.py", line 819, in exec_module
    exec(code, module.__dict__)
  File "E:\projects\python\THELIF~1\lib\site-packages\libloader\com.py", line 2, in <module>
    from win32com.client import gencache
  File "E:\projects\python\THELIF~1\lib\importlib\_bootstrap.py", line 983, in _find_and_load
    return _find_and_load_unlocked(name, import_)
  File "E:\projects\python\THELIF~1\lib\importlib\_bootstrap.py", line 953, in _find_and_load_unlocked
    _call_with_frames_removed(import_, parent)
  File "E:\projects\python\THELIF~1\lib\importlib\_bootstrap.py", line 219, in _call_with_frames_removed
    return f(*args, **kwds)
  File "E:\projects\python\THELIF~1\lib\importlib\_bootstrap.py", line 983, in _find_and_load
    return _find_and_load_unlocked(name, import_)
  File "E:\projects\python\THELIF~1\lib\importlib\_bootstrap.py", line 967, in _find_and_load_unlocked
    module = _load_unlocked(spec)
  File "E:\projects\python\THELIF~1\lib\importlib\_bootstrap.py", line 677, in _load_unlocked
    spec.loader.exec_module(module)
  File "E:\projects\python\THELIF~1\lib\importlib\_bootstrap.py", line 819, in exec_module
    exec(code, module.__dict__)
  File "E:\projects\python\THELIF~1\lib\site-packages\win32com\__init__.py", line 98, in <module>
    __gen_path__ = os.path.abspath(os.path.join(__path__[0], "gen_py"))
IndexError: list index out of range

in pyInstaller, it has a hook for it called hook-win32com.py which i paste it’s code here:

#-----------------------------------------------------------------------------
# Copyright (c) 2005-2018, PyInstaller Development Team.
#
# Distributed under the terms of the GNU General Public License with exception
# for distributing bootloader.
#
# The full license is in the file COPYING.txt, distributed with this software.
#-----------------------------------------------------------------------------


hiddenimports = [
    # win32com client and server util
    # modules could be hidden imports
    # of some modules using win32com.
    # Included for completeness.
    'win32com.client.util',
    'win32com.server.util',
]

also, i will paste the hook for hook-pywintypes.py:

#-----------------------------------------------------------------------------
# Copyright (c) 2005-2018, PyInstaller Development Team.
#
# Distributed under the terms of the GNU General Public License with exception
# for distributing bootloader.
#
# The full license is in the file COPYING.txt, distributed with this software.
#-----------------------------------------------------------------------------


"""
pywin32 module supports frozen mode. In frozen mode it is looking
in sys.path for file pywintypesXX.dll. Include the pywintypesXX.dll
as a data file. The path to this dll is contained in __file__
attribute.
"""

from PyInstaller.utils.hooks import get_pywin32_module_file_attribute

_pth = get_pywin32_module_file_attribute('pywintypes')
binaries = [(_pth, '.')]

also, it has a run-time hook, in order to load the win32com.gen_py:

#-----------------------------------------------------------------------------
# Copyright (c) 2013-2018, PyInstaller Development Team.
#
# Distributed under the terms of the GNU General Public License with exception
# for distributing bootloader.
#
# The full license is in the file COPYING.txt, distributed with this software.
#-----------------------------------------------------------------------------


# The win32.client.gencache code must be allowed to create the cache in %temp%
# (user's temp). It is necessary to get the gencache code to use a suitable
# directory other than the default in lib\site-packages\win32com\client\gen_py.
# PyInstaller does not provide this directory structure and the frozen
# executable could be placed in a non-writable directory like 'C:\Program Files.
# That's the reason for %temp% directory.
#
# http://www.py2exe.org/index.cgi/UsingEnsureDispatch


import atexit
import os
import shutil
import tempfile


# Put gen_py cache in temp directory.
supportdir = tempfile.mkdtemp()
# gen_py has to be put into directory 'gen_py'.
genpydir = os.path.join(supportdir, 'gen_py')


# Create 'gen_py' directory. This directory does not need
# to contain '__init__.py' file.
try:
    # win32com gencache cannot be put directly to 'supportdir' with any
    # random name. It has to be put in a directory called 'gen_py'.
    # This is the reason why to create this directory in supportdir'.
    os.makedirs(genpydir)
    # Remove temp directory at application exit and ignore any errors.
    atexit.register(shutil.rmtree, supportdir, ignore_errors=True)
except OSError:
    pass


# Override the default path to gen_py cache.
import win32com
win32com.__gen_path__ = genpydir


# The attribute __loader__ makes module 'pkg_resources' working but On Windows
# it breaks pywin32 (win32com) and test 'basic/test_pyttsx' will fail. Just
# removing that attribute for win32com fixes that and gencache is created properly.
if hasattr(win32com, '__loader__'):
    del win32com.__loader__


# Ensure genpydir is in 'gen_py' module paths.
import win32com.gen_py
win32com.gen_py.__path__.insert(0, genpydir)

thanks.

also, maybe it is good to have a way of loading module hooks like pyinstaller.
it can be placed in something like “hooks” item in the build_apps dictionary, in order to specify custom actions when compiling and embedding python scripts.