catalystneuro / vame-desktop

A desktop application for the Variational Animal Motion Encoding project.
GNU General Public License v3.0
1 stars 0 forks source link

Fix Numpy Related Errors in Pyinstaller Build (Mac) #20

Open garrettmflynn opened 3 weeks ago

garrettmflynn commented 3 weeks ago

I've been getting a set of Pyinstaller issues with Numpy when testing out the Mac builds for VAME Desktop.

I was able to fix one by adding src/hooks/hook-numpy.py, but this has resulted in a second, more tricky error.

INTEL MKL ERROR: . Intel MKL FATAL ERROR: Cannot load libmkl_intel_thread.2.dylib.

This is reminiscent of an issue seen on the GUIDE that mysteriously resolved itself, though that was happening on my local builds. @CodyCBakerPhD may have some ideas about fixing this.

In the interim, I've ensured that VAME Desktop can load even if the /ready (preload) endpoint fails. Though this does mean that users can't actually run any of the steps in a VAME pipeline when this occurs.

garrettmflynn commented 3 weeks ago

My current hunch is changing runs-on: macos-latest to runs-on: macos-13 results in dependencies in the single Mac build that can't be resolved on my M2.

I've had success only creating one build for both Intel and ARM Macs—though we may have to switch to a two-build solution for this. We'll see!

CodyCBakerPhD commented 3 weeks ago

And you're installing numpy from conda forge on Mac? https://github.com/NeurodataWithoutBorders/nwb-guide/blob/main/environments/environment-MAC-apple-silicon.yml#L11

CodyCBakerPhD commented 3 weeks ago

Otherwise maybe try pyinstaller hooks contrib, which can occasionally fix things like this https://github.com/NeurodataWithoutBorders/nwb-guide/blob/main/environments/environment-Windows.yml#L15

garrettmflynn commented 3 weeks ago

Yes I installed from Conda Forge to fix one of the initial issues I was having. Worked like a charm.

If changing the Mac it builds on doesn't work, I'll try pyinstaller-hooks-contrib. Thank you for taking a look at this!

garrettmflynn commented 3 weeks ago

After reverting back to macos-latest, it seems that I'm back to square on M2. Dumping the original error:

"PyInstaller/loader/pyimod02_importers.py", line 419, in exec_module
  File "numpy/core/multiarray.py", line 10, in <module>
  File "PyInstaller/loader/pyimod02_importers.py", line 419, in exec_module
  File "numpy/core/overrides.py", line 8, in <module>
ImportError: dlopen(/private/var/folders/yz/qfd0v1fn1mvd68pxdj6wvg3w0000gn/T/AppTranslocation/7AAD1412-3979-47B1-8DF7-A88230E32775/d/VAME.app/Contents/Resources/.commoners/.temp/electron/build/python/vame/_internal/numpy/core/_multiarray_umath.cpython-311-darwin.so, 0x0002): Library not loaded: @rpath/libcblas.3.dylib
  Referenced from: <D90C4C7D-DDE8-366A-AE6B-00CE1D04A0D6> /private/var/folders/yz/qfd0v1fn1mvd68pxdj6wvg3w0000gn/T/AppTranslocation/7AAD1412-3979-47B1-8DF7-A88230E32775/d/VAME.app/Contents/Resources/.commoners/.temp/electron/build/python/vame/_internal/numpy/core/_multiarray_umath.cpython-311-darwin.so
  Reason: tried: '/private/var/folders/yz/qfd0v1fn1mvd68pxdj6wvg3w0000gn/T/AppTranslocation/7AAD1412-3979-47B1-8DF7-A88230E32775/d/VAME.app/Contents/Resources/.commoners/.temp/electron/build/python/vame/_internal/numpy/core/../../libcblas.3.dylib' (no such file), '/private/var/folders/yz/qfd0v1fn1mvd68pxdj6wvg3w0000gn/T/AppTranslocation/7AAD1412-3979-47B1-8DF7-A88230E32775/d/VAME.app/Contents/Resources/.commoners/.temp/electron/build/python/vame/_internal/numpy/core/../../libcblas.3.dylib' (no such file), '/usr/local/lib/libcblas.3.dylib' (no such file), '/usr/lib/libcblas.3.dylib' (no such file, not in dyld cache)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "numpy/__init__.py", line 130, in <module>
  File "PyInstaller/loader/pyimod02_importers.py", line 419, in exec_module
  File "numpy/__config__.py", line 4, in <module>
  File "PyInstaller/loader/pyimod02_importers.py", line 419, in exec_module
  File "numpy/core/__init__.py", line 50, in <module>
ImportError: 

IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!

Importing the numpy C-extensions failed. This error can happen for
many reasons, often due to issues with your setup or how NumPy was
installed.

We have compiled some common reasons and troubleshooting tips at:

    https://numpy.org/devdocs/user/troubleshooting-importerror.html

Please note and check the following:

  * The Python version is: Python3.11 from "/private/var/folders/yz/qfd0v1fn1mvd68pxdj6wvg3w0000gn/T/AppTranslocation/7AAD1412-3979-47B1-8DF7-A88230E32775/d/VAME.app/Contents/Resources/.commoners/.temp/electron/build/python/vame/vame"
  * The NumPy version is: "1.26.4"

and make sure that they are the versions you expect.
Please carefully study the documentation linked above for further help.

Original error was: dlopen(/private/var/folders/yz/qfd0v1fn1mvd68pxdj6wvg3w0000gn/T/AppTranslocation/7AAD1412-3979-47B1-8DF7-A88230E32775/d/VAME.app/Contents/Resources/.commoners/.temp/electron/build/python/vame/_internal/numpy/core/_multiarray_umath.cpython-311-darwin.so, 0x0002): Library not loaded: @rpath/libcblas.3.dylib
  Referenced from: <D90C4C7D-DDE8-366A-AE6B-00CE1D04A0D6> /private/var/folders/yz/qfd0v1fn1mvd68pxdj6wvg3w0000gn/T/AppTranslocation/7AAD1412-3979-47B1-8DF7-A88230E32775/d/VAME.app/Contents/Resources/.commoners/.temp/electron/build/python/vame/_internal/numpy/core/_multiarray_umath.cpython-311-darwin.so
  Reason: tried: '/private/var/folders/yz/qfd0v1fn1mvd68pxdj6wvg3w0000gn/T/AppTranslocation/7AAD1412-3979-47B1-8DF7-A88230E32775/d/VAME.app/Contents/Resources/.commoners/.temp/electron/build/python/vame/_internal/numpy/core/../../libcblas.3.dylib' (no such file), '/private/var/folders/yz/qfd0v1fn1mvd68pxdj6wvg3w0000gn/T/AppTranslocation/7AAD1412-3979-47B1-8DF7-A88230E32775/d/VAME.app/Contents/Resources/.commoners/.temp/electron/build/python/vame/_internal/numpy/core/../../libcblas.3.dylib' (no such file), '/usr/local/lib/libcblas.3.dylib' (no such file), '/usr/lib/libcblas.3.dylib' (no such file, not in dyld cache)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "app.py", line 18, in <module>
  File "PyInstaller/loader/pyimod02_importers.py", line 419, in exec_module
  File "matplotlib/__init__.py", line 159, in <module>
  File "PyInstaller/loader/pyimod02_importers.py", line 419, in exec_module
  File "matplotlib/cbook.py", line 24, in <module>
  File "PyInstaller/loader/pyimod02_importers.py", line 419, in exec_module
  File "numpy/__init__.py", line 135, in <module>
ImportError: Error importing numpy: you should not try to import numpy from
        its source directory; please exit the numpy source tree, and relaunch
        your python interpreter from there.
[45587] Failed to execute script 'app' due to unhandled exception!
garrettmflynn commented 2 weeks ago

Now on macos-13, I'm getting a similar but slightly different error:

File "PyInstaller/loader/pyimod02_importers.py", line 419, in exec_module
  File "numpy/core/multiarray.py", line 10, in <module>
  File "PyInstaller/loader/pyimod02_importers.py", line 419, in exec_module
  File "numpy/core/overrides.py", line 8, in <module>
ImportError: dlopen(/private/var/folders/xk/h70n9w7s5lg_ljy4kwy9bmh40000gn/T/AppTranslocation/15239DB9-4832-497C-ABF2-4E0A6E7FFBB2/d/VAME.app/Contents/Resources/.commoners/.temp/electron/build/python/vame/_internal/numpy/core/_multiarray_umath.cpython-311-darwin.so, 0x0002): Library not loaded: @rpath/libgfortran.5.dylib
  Referenced from: <C8E0D37A-A539-3550-80F1-75FEA197008F> /private/var/folders/xk/h70n9w7s5lg_ljy4kwy9bmh40000gn/T/AppTranslocation/15239DB9-4832-497C-ABF2-4E0A6E7FFBB2/d/VAME.app/Contents/Resources/.commoners/.temp/electron/build/python/vame/_internal/libcblas.3.dylib
  Reason: tried: '/private/var/folders/xk/h70n9w7s5lg_ljy4kwy9bmh40000gn/T/AppTranslocation/15239DB9-4832-497C-ABF2-4E0A6E7FFBB2/d/VAME.app/Contents/Resources/.commoners/.temp/electron/build/python/vame/_internal/libgfortran.5.dylib' (no such file), '/private/var/folders/xk/h70n9w7s5lg_ljy4kwy9bmh40000gn/T/AppTranslocation/15239DB9-4832-497C-ABF2-4E0A6E7FFBB2/d/VAME.app/Contents/Resources/.commoners/.temp/electron/build/python/vame/_internal/numpy/core/../../libgfortran.5.dylib' (no such file), '/private/var/folders/xk/h70n9w7s5lg_ljy4kwy9bmh40000gn/T/AppTranslocation/15239DB9-4832-497C-ABF2-4E0A6E7FFBB2/d/VAME.app/Contents/Resources/.commoners/.temp/electron/build/python/vame/_internal/numpy/core/../../libgfortran.5.dylib' (no such file), '/usr/local/lib/libgfortran.5.dylib' (no such file), '/usr/lib/libgfortran.5.dylib' (no such file, not in dyld cache)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "numpy/__init__.py", line 130, in <module>
  File "PyInstaller/loader/pyimod02_importers.py", line 419, in exec_module
  File "numpy/__config__.py", line 4, in <module>
  File "PyInstaller/loader/pyimod02_importers.py", line 419, in exec_module
  File "numpy/core/__init__.py", line 50, in <module>
ImportError: 

IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!

Importing the numpy C-extensions failed. This error can happen for
many reasons, often due to issues with your setup or how NumPy was
installed.

We have compiled some common reasons and troubleshooting tips at:

    https://numpy.org/devdocs/user/troubleshooting-importerror.html

Please note and check the following:

  * The Python version is: Python3.11 from "/private/var/folders/xk/h70n9w7s5lg_ljy4kwy9bmh40000gn/T/AppTranslocation/15239DB9-4832-497C-ABF2-4E0A6E7FFBB2/d/VAME.app/Contents/Resources/.commoners/.temp/electron/build/python/vame/vame"
  * The NumPy version is: "1.26.4"

and make sure that they are the versions you expect.
Please carefully study the documentation linked above for further help.

Original error was: dlopen(/private/var/folders/xk/h70n9w7s5lg_ljy4kwy9bmh40000gn/T/AppTranslocation/15239DB9-4832-497C-ABF2-4E0A6E7FFBB2/d/VAME.app/Contents/Resources/.commoners/.temp/electron/build/python/vame/_internal/numpy/core/_multiarray_umath.cpython-311-darwin.so, 0x0002): Library not loaded: @rpath/libgfortran.5.dylib
  Referenced from: <C8E0D37A-A539-3550-80F1-75FEA197008F> /private/var/folders/xk/h70n9w7s5lg_ljy4kwy9bmh40000gn/T/AppTranslocation/15239DB9-4832-497C-ABF2-4E0A6E7FFBB2/d/VAME.app/Contents/Resources/.commoners/.temp/electron/build/python/vame/_internal/libcblas.3.dylib
  Reason: tried: '/private/var/folders/xk/h70n9w7s5lg_ljy4kwy9bmh40000gn/T/AppTranslocation/15239DB9-4832-497C-ABF2-4E0A6E7FFBB2/d/VAME.app/Contents/Resources/.commoners/.temp/electron/build/python/vame/_internal/libgfortran.5.dylib' (no such file), '/private/var/folders/xk/h70n9w7s5lg_ljy4kwy9bmh40000gn/T/AppTranslocation/15239DB9-4832-497C-ABF2-4E0A6E7FFBB2/d/VAME.app/Contents/Resources/.commoners/.temp/electron/build/python/vame/_internal/numpy/core/../../libgfortran.5.dylib' (no such file), '/private/var/folders/xk/h70n9w7s5lg_ljy4kwy9bmh40000gn/T/AppTranslocation/15239DB9-4832-497C-ABF2-4E0A6E7FFBB2/d/VAME.app/Contents/Resources/.commoners/.temp/electron/build/python/vame/_internal/numpy/core/../../libgfortran.5.dylib' (no such file), '/usr/local/lib/libgfortran.5.dylib' (no such file), '/usr/lib/libgfortran.5.dylib' (no such file, not in dyld cache)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "app.py", line 21, in <module>
  File "PyInstaller/loader/pyimod02_importers.py", line 419, in exec_module
  File "matplotlib/__init__.py", line 159, in <module>
  File "PyInstaller/loader/pyimod02_importers.py", line 419, in exec_module
  File "matplotlib/cbook.py", line 24, in <module>
  File "PyInstaller/loader/pyimod02_importers.py", line 419, in exec_module
  File "numpy/__init__.py", line 135, in <module>
ImportError: Error importing numpy: you should not try to import numpy from
        its source directory; please exit the numpy source tree, and relaunch
        your python interpreter from there.
[79875] Failed to execute script 'app' due to unhandled exception!
garrettmflynn commented 2 weeks ago

Worth noting that none of this happens on local builds with new environments.

CodyCBakerPhD commented 2 weeks ago

I highly recommend trying to isolate the problem outside of the rest of the app

Back when we ran into this kind of thing on GUIDE I just started by making PyInstaller bundles of numpy, then a really simple Flask that used numpy, then PyInstaller bundle of that flask, etc. etc.

Also it doesn't look like you're building the PyInstaller from a spec file but rather from the CLI helper, that was one thing that I'm really glad we stopped doing on the GUIDE (which makes the bundling of these kinds of subpackages much easier to handle)

garrettmflynn commented 2 weeks ago

Gotcha. Thank you for the suggestion! That makes a lot of sense.