I experienced this error when using pytorch on macOS Sonoma with Python 3.12.10. What fixed it for me is doing any other import before pytorch and for some reason that works.