Repository: pyinstaller/pyinstaller

Issue:
Libraries packaged in unexpected location when using `python` installed via `scoop`
## Description of the issue

The following example code fails on my system:

`bug.py`
```python
# pyinstaller --onefile bug.py && dist\bug.exe
import matplotlib
```

The output is:
```
C:\Build\project>dist\bug.exe
Traceback (most recent call last):
  File "C:\Scoop\persist\python\Lib\site-packages\PyInstaller\hooks\rthooks\pyi_rth_mplconfig.py", line 45, in <module>
    _pyi_rthook()
  File "C:\Scoop\persist\python\Lib\site-packages\PyInstaller\hooks\rthooks\pyi_rth_mplconfig.py", line 31, in _pyi_rthook
    import _pyi_rth_utils  # PyInstaller's run-time hook utilities module
    ^^^^^^^^^^^^^^^^^^^^^
  File "PyInstaller\loader\pyimod02_importers.py", line 391, in exec_module
  File "PyInstaller\fake-modules\_pyi_rth_utils\__init__.py", line 23, in <module>
  File "PyInstaller\loader\pyimod02_importers.py", line 391, in exec_module
  File "PyInstaller\fake-modules\_pyi_rth_utils\_win32.py", line 12, in <module>
  File "PyInstaller\loader\pyimod02_importers.py", line 391, in exec_module
  File "ctypes\__init__.py", line 8, in <module>
ImportError: DLL load failed while importing _ctypes: The specified module could not be found.
[4880] Failed to execute script 'pyi_rth_mplconfig' due to unhandled exception!
```

### Context information (for bug reports)

* Output of `pyinstaller --version`: ```6.1.0```
* Version of Python: 3.12.0
* Platform: Windows
* How you installed Python: `scoop install python`
* Did you also try this on another platform? No, `scoop` is Windows-only.
* try the latest development version, using the following command: same result.

`libffi-8.dll` seems to be packaged in the wrong location inside the MEI, see https://github.com/orgs/pyinstaller/discussions/7431#discussioncomment-7335377.


Discussion:
> `libffi-8.dll` seems to be packaged in the wrong location inside the MEI, see https://github.com/orgs/pyinstaller/discussions/7431#discussioncomment-7335377.

Thanks for the initial analysis! This sounds like a DLL parent path preservation bug (which seems to affect either `scoop`-installed python, or perhaps python without virtual environment in general). I'll look into it.

Resolving patch:
diff --git a/PyInstaller/depend/bindepend.py b/PyInstaller/depend/bindepend.py
--- a/PyInstaller/depend/bindepend.py
+++ b/PyInstaller/depend/bindepend.py
@@ -61,9 +61,12 @@ def _get_paths_for_parent_directory_preservation():
     orig_paths.append(site.getusersitepackages())
 
     # Explicitly excluded paths. `site.getsitepackages` seems to include `sys.prefix`, which we need to exclude, to
-    # avoid issue swith DLLs in its sub-directories.
+    # avoid issue swith DLLs in its sub-directories. We need both resolved and unresolved variant to handle cases
+    # where `base_prefix` itself is a symbolic link (e.g., `scoop`-installed python on Windows, see #8023).
     excluded_paths = {
+        pathlib.Path(sys.base_prefix),
         pathlib.Path(sys.base_prefix).resolve(),
+        pathlib.Path(sys.prefix),
         pathlib.Path(sys.prefix).resolve(),
     }
 


Test patch: