Repository: pyinstaller/pyinstaller

Issue:
Not able to create mac executable if we have used PyautoGUI in python script
I am facing issue in pyinstaller if I try to make executable file from mac when I use pyautogui library in Python script(Working fine on windows).
After debugging I got to know that issue is when I use pyautogui and pyinstaller together.
So, I've tried this simple program which has just import of pyautogui
demo.py
```
import pyautogui as pg
if __name__ == "__main__":
    `print("Hello,` WOrld!")
```

It was giving me below error if I just type pyinstaller demo.py --onefile
exit;
Traceback (most recent call last):
  File "PyInstaller/hooks/rthooks/pyi_rth__tkinter.py", line 20, in <module>
FileNotFoundError: Tcl data directory "/var/folders/83/vlr93lcs4bg1r6ytzy1vvjy40000gn/T/_MEIyqvIJz/tcl" not found.
[57142] Failed to execute script pyi_rth__tkinter

So, I've tried this
pyinstaller --onefile --add-binary='/System/Library/Frameworks/Tk.framework/Tk':'tk' --add-binary='/System/Library/Frameworks/Tcl.framework/Tcl':'tcl' demo.py

below is the stacktrace of error:

exit;
Traceback (most recent call last):
  File "PyInstaller/loader/pyiboot01_bootstrap.py", line 144, in __init__
  File "ctypes/__init__.py", line 348, in __init__
OSError: dlopen(/var/folders/83/vlr93lcs4bg1r6ytzy1vvjy40000gn/T/_MEIjmFz1e/Foundation, 6): no suitable image found.  Did find:
	/var/folders/83/vlr93lcs4bg1r6ytzy1vvjy40000gn/T/_MEIjmFz1e/Foundation: not a file
	/private/var/folders/83/vlr93lcs4bg1r6ytzy1vvjy40000gn/T/_MEIjmFz1e/Foundation: not a file

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "demo.py", line 1, in <module>
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "/Users/admin/Documents/django_infoware/venv_demo/lib/python3.6/site-packages/PyInstaller/loader/pyimod03_importers.py", line 493, in exec_module
    exec(bytecode, module.__dict__)
  File "pyautogui/__init__.py", line 241, in <module>
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "/Users/admin/Documents/django_infoware/venv_demo/lib/python3.6/site-packages/PyInstaller/loader/pyimod03_importers.py", line 493, in exec_module
    exec(bytecode, module.__dict__)
  File "mouseinfo/__init__.py", line 100, in <module>
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "/Users/admin/Documents/django_infoware/venv_demo/lib/python3.6/site-packages/PyInstaller/loader/pyimod03_importers.py", line 493, in exec_module
    exec(bytecode, module.__dict__)
  File "rubicon/objc/__init__.py", line 15, in <module>
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "/Users/admin/Documents/django_infoware/venv_demo/lib/python3.6/site-packages/PyInstaller/loader/pyimod03_importers.py", line 493, in exec_module
    exec(bytecode, module.__dict__)
  File "rubicon/objc/runtime.py", line 81, in <module>
  File "rubicon/objc/runtime.py", line 58, in load_library
  File "PyInstaller/loader/pyiboot01_bootstrap.py", line 146, in __init__
__main__.PyInstallerImportError: Failed to load dynlib/dll '/var/folders/83/vlr93lcs4bg1r6ytzy1vvjy40000gn/T/_MEIjmFz1e/Foundation'. Most probably this dynlib/dll was not found when the application was frozen.
[57474] Failed to execute script demo

[Process completed]

It seems like there is some windows dependency when I use pyautoGUI. Even I am on mac




Discussion:
@bwoodsend Why their is dependency of windows library on mac when I use pyautoGUI?
Ahh, fair point. Could you fill out the issue template properly.
There is no dependency on Windows library - `Foundation` is a system library on OSX, located in `/System/Library/Frameworks/Foundation.framework/Foundation`.

However, it is not found due to a subtle bug in the pyinstaller's library loading, which prevents a system library from being loaded if a directory with a same name exists in the program's directory (and `Foundation` directory ends up created due to one of pyautoGUI's dependencies, probably `pyobjc` or `rubicon`).

I'll shorty prepare a fix for this.

Resolving patch:
diff --git a/PyInstaller/loader/pyiboot01_bootstrap.py b/PyInstaller/loader/pyiboot01_bootstrap.py
--- a/PyInstaller/loader/pyiboot01_bootstrap.py
+++ b/PyInstaller/loader/pyiboot01_bootstrap.py
@@ -126,7 +126,7 @@ def isatty(self):
     def _frozen_name(name):
         if name:
             frozen_name = os.path.join(sys._MEIPASS, os.path.basename(name))
-            if os.path.exists(frozen_name):
+            if os.path.exists(frozen_name) and not os.path.isdir(frozen_name):
                 name = frozen_name
         return name
 


Test patch: