Repository: pyinstaller/pyinstaller

Issue:
Hook __PyInstaller_hooks_0_pytest missing
I am trying to build a .py script to an .exe with this command:
`C:\Users\surfer\Desktop\horizons2xyzv>pyinstaller horizons2xyzv.py --onefile`
but it crashes:
```
503 INFO: PyInstaller: 5.11.0
503 INFO: Python: 3.11.1
513 INFO: Platform: Windows-10-10.0.22621-SP0
513 INFO: wrote C:\Users\surfer\Desktop\horizons2xyzv\horizons2xyzv.spec
513 INFO: UPX is not available.
513 INFO: Extending PYTHONPATH with paths
['C:\\Users\\surfer\\Desktop\\horizons2xyzv']
pygame-ce 2.2.1 (SDL 2.26.4, Python 3.11.1)
1633 INFO: checking Analysis
1635 INFO: Building Analysis because Analysis-00.toc is non existent
1635 INFO: Initializing module dependency graph...
1635 INFO: Caching module graph hooks...
1651 INFO: Analyzing base_library.zip ...
4604 INFO: Loading module hook 'hook-heapq.py' from 'C:\\Users\\surfer\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\PyInstaller\\hooks'...
4762 INFO: Loading module hook 'hook-encodings.py' from 'C:\\Users\\surfer\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\PyInstaller\\hooks'...
6904 INFO: Loading module hook 'hook-pickle.py' from 'C:\\Users\\surfer\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\PyInstaller\\hooks'...
8663 INFO: Caching module dependency graph...
8793 INFO: running Analysis Analysis-00.toc
8803 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
  required by C:\Users\surfer\AppData\Local\Programs\Python\Python311\python.exe
8844 INFO: Analyzing C:\Users\surfer\Desktop\horizons2xyzv\horizons2xyzv.py
9033 INFO: Loading module hook 'hook-platform.py' from 'C:\\Users\\surfer\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\PyInstaller\\hooks'...
9458 INFO: Loading module hook 'hook-pygments.py' from 'C:\\Users\\surfer\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\PyInstaller\\hooks'...
11888 INFO: Loading module hook 'hook-importlib_metadata.py' from 'C:\\Users\\surfer\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\PyInstaller\\hooks'...
12174 INFO: Loading module hook 'hook-pkg_resources.py' from 'C:\\Users\\surfer\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\PyInstaller\\hooks'...
14213 INFO: Loading module hook 'hook-xml.py' from 'C:\\Users\\surfer\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\PyInstaller\\hooks'...
14323 INFO: Loading module hook 'hook-sysconfig.py' from 'C:\\Users\\surfer\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\PyInstaller\\hooks'...
15554 INFO: Loading module hook 'hook-IPython.py' from 'C:\\Users\\surfer\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\_pyinstaller_hooks_contrib\\hooks\\stdhooks'...
17833 INFO: Loading module hook 'hook-difflib.py' from 'C:\\Users\\surfer\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\PyInstaller\\hooks'...
18223 INFO: Loading module hook 'hook-xml.dom.domreg.py' from 'C:\\Users\\surfer\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\PyInstaller\\hooks'...
18673 INFO: Loading module hook 'hook-matplotlib.py' from 'C:\\Users\\surfer\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\PyInstaller\\hooks'...
19064 INFO: Loading module hook 'hook-numpy.py' from 'C:\\Users\\surfer\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\numpy\\_pyinstaller'...
20867 INFO: Loading module hook 'hook-multiprocessing.util.py' from 'C:\\Users\\surfer\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\PyInstaller\\hooks'...
22688 INFO: Loading module hook 'hook-xml.etree.cElementTree.py' from 'C:\\Users\\surfer\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\PyInstaller\\hooks'...
24174 INFO: Loading module hook 'hook-packaging.py' from 'C:\\Users\\surfer\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\PyInstaller\\hooks'...
24543 INFO: Processing pre-safe import module hook gi from 'C:\\Users\\surfer\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\PyInstaller\\hooks\\pre_safe_import_module\\hook-gi.py'.
24754 INFO: Loading module hook 'hook-PIL.py' from 'C:\\Users\\surfer\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\PyInstaller\\hooks'...
24827 INFO: Loading module hook 'hook-PIL.Image.py' from 'C:\\Users\\surfer\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\PyInstaller\\hooks'...
25869 INFO: Loading module hook 'hook-pycparser.py' from 'C:\\Users\\surfer\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\_pyinstaller_hooks_contrib\\hooks\\stdhooks'...
26403 INFO: Processing pre-find module path hook distutils from 'C:\\Users\\surfer\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\PyInstaller\\hooks\\pre_find_module_path\\hook-distutils.py'.
26742 INFO: Loading module hook 'hook-distutils.py' from 'C:\\Users\\surfer\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\PyInstaller\\hooks'...
26793 INFO: Loading module hook 'hook-distutils.util.py' from 'C:\\Users\\surfer\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\PyInstaller\\hooks'...
27063 INFO: Loading module hook 'hook-setuptools.py' from 'C:\\Users\\surfer\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\PyInstaller\\hooks'...
30983 INFO: Loading module hook 'hook-PIL.ImageFilter.py' from 'C:\\Users\\surfer\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\PyInstaller\\hooks'...
32866 INFO: Processing pre-safe import module hook six.moves from 'C:\\Users\\surfer\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\PyInstaller\\hooks\\pre_safe_import_module\\hook-six.moves.py'.
34618 INFO: Loading module hook 'hook-matplotlib.backends.py' from 'C:\\Users\\surfer\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\PyInstaller\\hooks'...
35723 INFO: Loading module hook 'hook-certifi.py' from 'C:\\Users\\surfer\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\_pyinstaller_hooks_contrib\\hooks\\stdhooks'...
36376 INFO: Loading module hook 'hook-astroid.py' from 'C:\\Users\\surfer\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\_pyinstaller_hooks_contrib\\hooks\\stdhooks'...
38553 INFO: Loading module hook 'hook-lib2to3.py' from 'C:\\Users\\surfer\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\PyInstaller\\hooks'...
40833 INFO: Loading module hook 'hook-cryptography.py' from 'C:\\Users\\surfer\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\_pyinstaller_hooks_contrib\\hooks\\stdhooks'...
43583 INFO: Loading module hook 'hook-bcrypt.py' from 'C:\\Users\\surfer\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\_pyinstaller_hooks_contrib\\hooks\\stdhooks'...
45143 INFO: Loading module hook 'hook-wcwidth.py' from 'C:\\Users\\surfer\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\PyInstaller\\hooks'...
46923 INFO: Loading module hook 'hook-jedi.py' from 'C:\\Users\\surfer\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\_pyinstaller_hooks_contrib\\hooks\\stdhooks'...
48161 INFO: Loading module hook 'hook-parso.py' from 'C:\\Users\\surfer\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\_pyinstaller_hooks_contrib\\hooks\\stdhooks'...
50203 INFO: Loading module hook 'hook-sqlite3.py' from 'C:\\Users\\surfer\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\PyInstaller\\hooks'...
51485 INFO: Loading module hook 'hook-PyQt6.py' from 'C:\\Users\\surfer\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\PyInstaller\\hooks'...
53484 INFO: Processing module hooks...
53733 INFO: Loading module hook 'hook-pytest.py' from 'C:\\Users\\surfer\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\_pyinstaller_hooks_contrib\\hooks\\stdhooks'...
Traceback (most recent call last):
  File "C:\Users\surfer\AppData\Local\Programs\Python\Python311\Lib\site-packages\PyInstaller\depend\imphook.py", line 383, in _load_hook_module
    self._hook_module = importlib_load_source(self.hook_module_name, self.hook_filename)
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\surfer\AppData\Local\Programs\Python\Python311\Lib\site-packages\PyInstaller\compat.py", line 612, in importlib_load_source
    return mod_loader.load_module()
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap_external>", line 605, in _check_name_wrapper
  File "<frozen importlib._bootstrap_external>", line 1120, in load_module
  File "<frozen importlib._bootstrap_external>", line 945, in load_module
  File "<frozen importlib._bootstrap>", line 290, in _load_module_shim
  File "<frozen importlib._bootstrap>", line 721, in _load
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 940, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "C:\Users\surfer\AppData\Local\Programs\Python\Python311\Lib\site-packages\_pyinstaller_hooks_contrib\hooks\stdhooks\hook-pytest.py", line 18, in <module>
    import pytest
  File "C:\Users\surfer\AppData\Local\Programs\Python\Python311\Lib\site-packages\pytest\__init__.py", line 6, in <module>
    from _pytest.assertion import register_assert_rewrite
  File "C:\Users\surfer\AppData\Local\Programs\Python\Python311\Lib\site-packages\_pytest\assertion\__init__.py", line 9, in <module>
    from _pytest.assertion import rewrite
  File "C:\Users\surfer\AppData\Local\Programs\Python\Python311\Lib\site-packages\_pytest\assertion\rewrite.py", line 22, in <module>
    from _pytest._io.saferepr import saferepr
  File "C:\Users\surfer\AppData\Local\Programs\Python\Python311\Lib\site-packages\_pytest\_io\__init__.py", line 4, in <module>
    from py.io import TerminalWriter as BaseTerminalWriter  # noqa: F401
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ImportError: cannot import name 'TerminalWriter' from 'py.io' (unknown location)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "C:\Users\surfer\AppData\Local\Programs\Python\Python311\Scripts\pyinstaller.exe\__main__.py", line 7, in <module>
  File "C:\Users\surfer\AppData\Local\Programs\Python\Python311\Lib\site-packages\PyInstaller\__main__.py", line 194, in _console_script_run
    run()
  File "C:\Users\surfer\AppData\Local\Programs\Python\Python311\Lib\site-packages\PyInstaller\__main__.py", line 180, in run
    run_build(pyi_config, spec_file, **vars(args))
  File "C:\Users\surfer\AppData\Local\Programs\Python\Python311\Lib\site-packages\PyInstaller\__main__.py", line 61, in run_build
    PyInstaller.building.build_main.main(pyi_config, spec_file, **kwargs)
  File "C:\Users\surfer\AppData\Local\Programs\Python\Python311\Lib\site-packages\PyInstaller\building\build_main.py", line 1006, in main
    build(specfile, distpath, workpath, clean_build)
  File "C:\Users\surfer\AppData\Local\Programs\Python\Python311\Lib\site-packages\PyInstaller\building\build_main.py", line 928, in build
    exec(code, spec_namespace)
  File "C:\Users\surfer\Desktop\horizons2xyzv\horizons2xyzv.spec", line 7, in <module>
    a = Analysis(
        ^^^^^^^^^
  File "C:\Users\surfer\AppData\Local\Programs\Python\Python311\Lib\site-packages\PyInstaller\building\build_main.py", line 428, in __init__
    self.__postinit__()
  File "C:\Users\surfer\AppData\Local\Programs\Python\Python311\Lib\site-packages\PyInstaller\building\datastruct.py", line 184, in __postinit__
    self.assemble()
  File "C:\Users\surfer\AppData\Local\Programs\Python\Python311\Lib\site-packages\PyInstaller\building\build_main.py", line 587, in assemble
    self.graph.process_post_graph_hooks(self)
  File "C:\Users\surfer\AppData\Local\Programs\Python\Python311\Lib\site-packages\PyInstaller\depend\analysis.py", line 329, in process_post_graph_hooks
    module_hook.post_graph(analysis)
  File "C:\Users\surfer\AppData\Local\Programs\Python\Python311\Lib\site-packages\PyInstaller\depend\imphook.py", line 450, in post_graph
    self._process_hidden_imports()
  File "C:\Users\surfer\AppData\Local\Programs\Python\Python311\Lib\site-packages\PyInstaller\depend\imphook.py", line 506, in _process_hidden_imports
    self.module_graph.import_hook(import_module_name, caller)
  File "C:\Users\surfer\AppData\Local\Programs\Python\Python311\Lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 1518, in import_hook
    submodule = self._safe_import_module(head, mname, submodule)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\surfer\AppData\Local\Programs\Python\Python311\Lib\site-packages\PyInstaller\depend\analysis.py", line 479, in _safe_import_module
    return super()._safe_import_module(module_basename, module_name, parent_package)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\surfer\AppData\Local\Programs\Python\Python311\Lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 2062, in _safe_import_module
    self._process_imports(n)
  File "C:\Users\surfer\AppData\Local\Programs\Python\Python311\Lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 2881, in _process_imports
    target_modules = self._safe_import_hook(*import_info, **kwargs)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\surfer\AppData\Local\Programs\Python\Python311\Lib\site-packages\PyInstaller\depend\analysis.py", line 432, in _safe_import_hook
    return super()._safe_import_hook(target_module_partname, source_module, target_attr_names, level, edge_attr)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\surfer\AppData\Local\Programs\Python\Python311\Lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 2332, in _safe_import_hook
    target_modules = self.import_hook(
                     ^^^^^^^^^^^^^^^^^
  File "C:\Users\surfer\AppData\Local\Programs\Python\Python311\Lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 1505, in import_hook
    target_package, target_module_partname = self._find_head_package(
                                             ^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\surfer\AppData\Local\Programs\Python\Python311\Lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 1684, in _find_head_package
    target_package = self._safe_import_module(
                     ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\surfer\AppData\Local\Programs\Python\Python311\Lib\site-packages\PyInstaller\depend\analysis.py", line 479, in _safe_import_module
    return super()._safe_import_module(module_basename, module_name, parent_package)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\surfer\AppData\Local\Programs\Python\Python311\Lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 2062, in _safe_import_module
    self._process_imports(n)
  File "C:\Users\surfer\AppData\Local\Programs\Python\Python311\Lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 2881, in _process_imports
    target_modules = self._safe_import_hook(*import_info, **kwargs)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\surfer\AppData\Local\Programs\Python\Python311\Lib\site-packages\PyInstaller\depend\analysis.py", line 368, in _safe_import_hook
    excluded_imports = self._find_all_excluded_imports(source_module.identifier)
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\surfer\AppData\Local\Programs\Python\Python311\Lib\site-packages\PyInstaller\depend\analysis.py", line 356, in _find_all_excluded_imports
    excluded_imports.update(module_hook.excludedimports)
                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\surfer\AppData\Local\Programs\Python\Python311\Lib\site-packages\PyInstaller\depend\imphook.py", line 316, in __getattr__
    self._load_hook_module()
  File "C:\Users\surfer\AppData\Local\Programs\Python\Python311\Lib\site-packages\PyInstaller\depend\imphook.py", line 386, in _load_hook_module
    raise ImportErrorWhenRunningHook(self.hook_module_name, self.hook_filename)
PyInstaller.exceptions.ImportErrorWhenRunningHook: Failed to import module __PyInstaller_hooks_0_pytest required by hook for module C:\Users\surfer\AppData\Local\Programs\Python\Python311\Lib\site-packages\_pyinstaller_hooks_contrib\hooks\stdhooks\hook-pytest.py. Please check whether module __PyInstaller_hooks_0_pytest actually exists and whether the hook is compatible with your version of C:\Users\surfer\AppData\Local\Programs\Python\Python311\Lib\site-packages\_pyinstaller_hooks_contrib\hooks\stdhooks\hook-pytest.py: You might want to read more about hooks in the manual and provide a pull-request to improve PyInstaller.
```
The script uses these `import`s:
```
import re, time, sys, os
from rich import print as printx
import urllib.request as request
import urllib.parse as parse
```
I've tried putting an empty file with the name of the required module (`__PyInstaller_hooks_0_pytest.py`) into the folders `C:\Users\surfer\AppData\Local\Programs\Python\Python311\Lib\site-packages\_pyinstaller_hooks_contrib\hooks\stdhooks\` and `C:\Users\surfer\AppData\Local\Programs\Python\Python311\Lib\site-packages\_pyinstaller_hooks_contrib\hooks\`, but all of this didn't help.

The normal Python script runs fine.

(Just a small note if you wonder about the name of the .py script: the script is intended to load data from a website (an API), process it and write it to a file for later use with another program.)

I am using PyInstaller 5.11.0 on CPython 3.11.1


Discussion:
Hmm, if you open python prompt and run
```
import pytest
```
does it also return an error?
When I `import pytest`, I get this error:
```
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\surfer\AppData\Local\Programs\Python\Python311\Lib\site-packages\pytest\__init__.py", line 6, in <module>
    from _pytest.assertion import register_assert_rewrite
  File "C:\Users\surfer\AppData\Local\Programs\Python\Python311\Lib\site-packages\_pytest\assertion\__init__.py", line 9, in <module>
    from _pytest.assertion import rewrite
  File "C:\Users\surfer\AppData\Local\Programs\Python\Python311\Lib\site-packages\_pytest\assertion\rewrite.py", line 22, in <module>
    from _pytest._io.saferepr import saferepr
  File "C:\Users\surfer\AppData\Local\Programs\Python\Python311\Lib\site-packages\_pytest\_io\__init__.py", line 4, in <module>
    from py.io import TerminalWriter as BaseTerminalWriter  # noqa: F401
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ImportError: cannot import name 'TerminalWriter' from 'py.io' (unknown location)
```
I updated `pyinstaller-hooks-contrib` to the latest version.
When I try to build my script again, I get the same error as in the initial post.
>I updated pyinstaller-hooks-contrib to the latest version.
> When I try to build my script again, I get the same error as in the initial post.

Yeah, sorry, the `pyinstaller-hooks-contrib` reply was meant for a different issue.

> When I import pytest, I get this error:

So you have a defunct `pytest` package installed, which is breaking the corresponding hook. 

`pytest` has removed that dependency on `py.io` in https://github.com/pytest-dev/pytest/commit/276405a0394e6ade96b23aba493fef0e6fccdbd5 in v6.0.0 three years ago. So you probably have older version installed, which requires `py` (and which you are missing for some reason). Try updating `pytest`, or install `py`.
So, after updating PyInstaller, pytest and py, the building process works fine. But, when I start the resulting .exe, I get this error:
```
Traceback (most recent call last):
  File "PyInstaller\hooks\rthooks\pyi_rth_win32comgenpy.py", line 46, in <module>
  File "PyInstaller\hooks\rthooks\pyi_rth_win32comgenpy.py", line 25, in _pyi_rthook
  File "PyInstaller\loader\pyimod02_importers.py", line 391, in exec_module
  File "win32com\__init__.py", line 8, in <module>
  File "PyInstaller\loader\pyimod02_importers.py", line 391, in exec_module
  File "pythoncom.py", line 2, in <module>
  File "PyInstaller\loader\pyimod02_importers.py", line 391, in exec_module
  File "pywintypes.py", line 126, in <module>
  File "pywintypes.py", line 116, in __import_pywin32_system_module__
ImportError: DLL load failed while importing pywintypes: Das angegebene Modul wurde nicht gefunden.
[13284] Failed to execute script 'pyi_rth_win32comgenpy' due to unhandled exception!
```

(One translation because my System runs in German:
`Das angegebene Modul wurde nicht gefunden.` means `The specified module could not be found.`)

I tried reinstalling (and thus updating) `pywin32` and adding `import pywintypes` to my module's imports, but nothing helped :(
Can you try with latest PyInstaller version (v6), or if you prefer to stay on v5, with latest v5 release (5.13.2)? If that does not fix this problem, can you freeze a simple program:
```
import win32com
print(win32com)
``` 
and upload the resulting `build` and `dist` directory somewhere so I can take a look at them? EDIT: build in onedir mode, since that's easier to debug and modify.
A fresh, clean one-dir build (without modifying the result), containing the `build` and `dist` directories:
https://www.mediafire.com/file/4putuz8tps420xl/horizons2xyzv_onedir.zip/file (169.51 MB)
Created with the command `pyinstaller horizons2xyzv.py` (Pyinstaller v6)
Thanks! 

The problem is that `vcruntime140_1.dll` is missing from top-level application directory, preventing `pywintypes` DLL from loading. 

We do collect the two copies bundled with `PyQt5` and `PyQt6` (on unrelated note, you should exclude the one that you are not using via `--exclude PyQt5` or `--exclude PyQt6`; both are collected because they are referenced in `matplotlib` and available in your environment - that's why we usually advise to build in virtual environments with minimal set of dependencies installed). 

But the reference from `pywintypes` should be resolved to python's copy.

So as a work-around, you could try adding `--add-binary "C:\Users\surfer\AppData\Local\Programs\Python\Python311\vcruntime140_1.dll;.` to your PyInstaller command.

But I'd still like to know what happened to `pywintypes`' reference - can you rebuild the program with `pyinstaller --clean --noconfirm --log-level DEBUG horizons2xyzv.py` and save the complete program output, and either attach it here or upload it again.

> But I'd still like to know what happened to `pywintypes`' reference - can you rebuild the program with `pyinstaller --clean --noconfirm --log-level DEBUG horizons2xyzv.py` and save the complete program output, and either attach it here or upload it again.

Nevermind, I managed to reproduce a faulty build with my minimal example. Will look into what's going on.
After adding the `--add-binary` argument with the `vcruntime140_1.dll`, it works, in both `--onefile` and `--onedir`! Thx!
I've found the culprit:
https://github.com/pyinstaller/pyinstaller/blob/126a2a02d4b233df49b6ddc421e8ead1d7ac692e/PyInstaller/building/build_main.py#L226-L227

This de-duplication does not preserve order of search paths, and even worse, randomizes it on each run. So sometimes we would first search python's home directory and pick the correct copy for `pywintypes` reference, and sometimes we would first search Qt5 or Qt6 directory and collect that - and consequently sometimes you would get a working build, and other times you would not.

Resolving patch:
diff --git a/PyInstaller/building/build_main.py b/PyInstaller/building/build_main.py
--- a/PyInstaller/building/build_main.py
+++ b/PyInstaller/building/build_main.py
@@ -224,7 +224,9 @@ def process_search_paths():
         extra_libdirs += added_path_directories
 
     # Deduplicate search paths
-    extra_libdirs = list(set(extra_libdirs))
+    # NOTE: `list(set(extra_libdirs))` does not preserve the order of search paths (which matters here), so we need to
+    # de-duplicate using `list(dict.fromkeys(extra_libdirs).keys())` instead.
+    extra_libdirs = list(dict.fromkeys(extra_libdirs).keys())
 
     # Search for dependencies of the given binaries
     return bindepend.binary_dependency_analysis(binaries, search_paths=extra_libdirs)


Test patch: