Repository: pyinstaller/pyinstaller

Issue:
Cryptography ImportError
Pyinstaller version: 4.7
Cryptography version: 36.0.0

hi, when i try to create exe with pyinstaller i get this error:
from cryptography.hazmat.bindings._rust import
ImportError: DLL load failed while importing _rust: The specified module could not be found.
when i go to this path i see the file _rust.pyd

when i try other Cryptography version i get error with _padding import.


Discussion:
> ImportError: DLL load failed while importing _rust: The specified module could not be found.

This usually means that one of dependent DLLs could not be loaded. But I can't seem to reproduce it here.

What python version are you using (and where was it installed from), and on what Windows version are you trying to run the program?
> > ImportError: DLL load failed while importing _rust: The specified module could not be found.
> 
> This usually means that one of dependent DLLs could not be loaded. But I can't seem to reproduce it here.
> 
> What python version are you using (and where was it installed from), and on what Windows version are you trying to run the program?

python 3.8 installed from python.org, windows 10.
Hmm, in that case, can you try making a simple `onedir` frozen application with just

```python
import cryptography.hazmat.bindings._rust
print(cryptography.hazmat.bindings._rust)
```

and upload it somewhere so I can take a look at it?
> Hmm, in that case, can you try making a simple `onedir` frozen application with just
> 
> ```python
> import cryptography.hazmat.bindings._rust
> print(cryptography.hazmat.bindings._rust)
> ```
> 
> and upload it somewhere so I can take a look at it?

https://drive.google.com/drive/folders/1SePb1efGtw1mVUDngp3GI10hu6KyXiM3?usp=sharing
Hmm, `python3.dll` did not get collected for some reason, and it is required by `_rust.pyd`. But looking at your `build\Analysis-00.toc` file, it looks like you are using WIndows App store version of python, not python.org one. Is that intentional? (I'll check if I can reproduce missing `python3.dll` with that...)
Yeah, we miss the `python3.dll` on Windows App Store python; presumably because it is launched via shim and so the actual executable and its DLLs are not automatically in the search path when we try to resolve DLLs...

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
@@ -140,6 +140,15 @@ def find_binary_dependencies(binaries, binding_redirects, import_packages):
     :return: expanded list of binaries and then dependencies.
     """
     from PyInstaller.depend import bindepend
+    from PyInstaller import compat
+
+    # In the case of MS App Store python, add compat.base_prefix to extra library search paths. In addition to
+    # python38.dll (that we manage to resolve by other means, if necessary), this directory also contains
+    # python3.dll that might be required by some 3rd-party extension modules, and would otherwise end up missing
+    # during the dependency analysis.
+    extra_libdirs = []
+    if compat.is_ms_app_store:
+        extra_libdirs.append(compat.base_prefix)
 
     # Import collected packages to set up environment
     for package in import_packages:
@@ -149,7 +158,7 @@ def find_binary_dependencies(binaries, binding_redirects, import_packages):
             pass
 
     # Search for dependencies of the given binaries
-    return bindepend.Dependencies(binaries, redirects=binding_redirects)
+    return bindepend.Dependencies(binaries, redirects=binding_redirects, xtrapath=extra_libdirs)
 
 
 class Analysis(Target):


Test patch: