FlorianRhiem / pyGLFW

Python bindings for GLFW
MIT License
232 stars 36 forks source link

Fails to find GLFW on Mac M1 #57

Closed junglie85 closed 2 years ago

junglie85 commented 3 years ago

Fails to find GLFW on Mac M1:

Traceback (most recent call last):
  File "/Users/junglie85/Personal/pygraphics/main.py", line 1, in <module>
    import glfw
  File "/Users/junglie85/Personal/pygraphics/venv/lib/python3.9/site-packages/glfw/__init__.py", line 43, in <module>
    raise ImportError("Failed to load GLFW3 shared library.")
ImportError: Failed to load GLFW3 shared library.

Also cannot load manually installed GLFW PYGLFW_LIBRARY=/opt/homebrew/Cellar/glfw/3.3.4.

FlorianRhiem commented 3 years ago

Hey @junglie85,

thanks for reporting this issue. Is the library really at /opt/homebrew/Cellar/glfw/3.3.4, not somethin like /opt/homebrew/Cellar/glfw/3.3.4/libglfw3.dylib or a similar filename ending in .dylib or .so? If the PYGLFW_LIBRARY location is correct, could you please check the architecture of your python installation and of the glfw library?

junglie85 commented 2 years ago

Hey @FlorianRhiem,

I've checked and all seems to be correct. I've even downloaded directly from GLFW website rather than via Brew or similar.

ls ~/opt/glfw-3.3.4/lib-arm64/
libglfw.3.dylib libglfw3.a
PYGLFW_LIBRARY=/Users/junglie85/opt/glfw-3.3.4/lib-arm64/libglfw.3.dylib
❯ python
Python 3.9.6 (default, Aug 24 2021, 08:50:49) 
[Clang 12.0.5 (clang-1205.0.22.9)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import platform
>>> platform.architecture()
('64bit', '')
>>> platform.machine()
'arm64'
>>> 

Yet, still the same error.

FlorianRhiem commented 2 years ago

What happens if you do the following:

import os
import ctypes
print(ctypes.CDLL(os.environ['PYGLFW_LIBRARY']))
SinaKhalili commented 2 years ago

To fix this, I manually added /opt/homebrew/lib to the glfw search path like so:

    search_paths = [
        '',
        package_path,
        sys.prefix + '/lib',
        '/usr/lib64',
        '/usr/local/lib64',
        '/usr/lib', '/usr/local/lib',
        '/run/current-system/sw/lib',
        '/opt/homebrew/lib/', # <= here 
        '/usr/lib/x86_64-linux-gnu/',
        '/usr/lib/aarch64-linux-gnu/',
    ]

This is because Homebrew installs to /opt/homebrew on apple silicon macs (link) instead of /usr/local/ (well, technically ARM stuff installs to /opt/homebrew).

Opened a PR 😄 https://github.com/FlorianRhiem/pyGLFW/pull/58

FlorianRhiem commented 2 years ago

@junglie85 Does the issue persist?

FlorianRhiem commented 2 years ago

I have just released version 2.4.0, along with wheels for macOS and arm64. This should fix this issue. As @junglie85 hasn't replied, I assume the workaround has worked for him as well.