Mac 11.0.1 上的 AttributeError 运行 Django 站点

AttributeError running Django site on Mac 11.0.1

在我将 Mac OS 更新到 11.0.1 之前,我在本地运行的 django 站点 运行 收到一个错误。我认为此更新是问题的原因,因为在它工作时和现在之间没有其他真正改变。

10:15:05 worker.1 | Traceback (most recent call last):
10:15:05 worker.1 |   File "/usr/local/bin/celery", line 5, in <module>
10:15:05 worker.1 |     from celery.__main__ import main
10:15:05 worker.1 |   File "/usr/local/lib/python2.7/site-packages/celery/__init__.py", line 133, in <module>
10:15:05 worker.1 |     from celery import five  # noqa
10:15:05 worker.1 |   File "/usr/local/lib/python2.7/site-packages/celery/five.py", line 20, in <module>
10:15:05 worker.1 |     from kombu.five import monotonic
10:15:05 worker.1 |   File "/usr/local/lib/python2.7/site-packages/kombu/five.py", line 56, in <module>
10:15:05 worker.1 |     absolute_to_nanoseconds = CoreServices.AbsoluteToNanoseconds
10:15:05 worker.1 |   File "/usr/local/Cellar/python@2/2.7.17_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ctypes/__init__.py", line 379, in __getattr__
10:15:05 worker.1 |     func = self.__getitem__(name)
10:15:05 worker.1 |   File "/usr/local/Cellar/python@2/2.7.17_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ctypes/__init__.py", line 384, in __getitem__
10:15:05 worker.1 |     func = self._FuncPtr((name_or_ordinal, self))
10:15:05 worker.1 | AttributeError: dlsym(RTLD_DEFAULT, AbsoluteToNanoseconds): symbol not found

这是我的 brew 配置

HOMEBREW_VERSION: 2.6.0
ORIGIN: https://github.com/Homebrew/brew
HEAD: 1d5e354cc2ff048bd7161d95b3fa7f91dc9dd081
Last commit: 2 days ago
Core tap ORIGIN: https://github.com/Homebrew/homebrew-core
Core tap HEAD: fdb83fcfb482e5ed1f1c3c442a85b99223fcabeb
Core tap last commit: 27 hours ago
Core tap branch: master
HOMEBREW_PREFIX: /usr/local
HOMEBREW_CASK_OPTS: []
HOMEBREW_DISPLAY: /private/tmp/com.apple.launchd.rZ1F30XomO/org.macosforge.xquartz:0
HOMEBREW_MAKE_JOBS: 8
Homebrew Ruby: 2.6.3 => /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/bin/ruby
CPU: octa-core 64-bit icelake
Clang: 12.0 build 1200
Git: 2.24.3 => /Applications/Xcode-beta.app/Contents/Developer/usr/bin/git
Curl: 7.64.1 => /usr/bin/curl
Java: 14.0.2, 1.8.0_265
macOS: 11.0.1-x86_64
CLT: 12.3.0.0.1.1605054730
Xcode: 12.3 => /Applications/Xcode-beta.app/Contents/Developer
XQuartz: 2.7.11 => /opt/X11

通常我会 运行 使用 virtualenv 运行ning python 2.7.15 的站点,我遇到了同样的错误。我用 pyenv 重新安装了 python 并重新制作了 virtualenv 但出现了同样的错误。我正在 运行宁 Django 1.10.8 与 Kombu 3.0.37

好的,这是 Big Sur 兼容性的肮脏解决方法:

https://developer.apple.com/documentation/macos-release-notes/macos-big-sur-11_0_1-release-notes

New in macOS Big Sur 11.0.1, the system ships with a built-in dynamic linker cache of all system-provided libraries. As part of this change, copies of dynamic libraries are no longer present on the filesystem. Code that attempts to check for dynamic library presence by looking for a file at a path or enumerating a directory will fail. Instead, check for library presence by attempting to dlopen() the path, which will correctly check for the library in the cache. (62986286)

所以为了找到这些库,我只是将静态路径放在 find_library 函数中 <path to your Python 2 installation>/lib/python2.7/ctypes/util.py 就在 os.name == "posix" and sys.platform == "darwin":

下面
if name == 'CoreServices':
    return '/System/Library/Frameworks/CoreServices.framework/CoreServices'
elif name == 'libSystem.dylib':
    return '/usr/lib/libSystem.dylib'

最后看起来像这样:

if os.name == "posix" and sys.platform == "darwin":
    from ctypes.macholib.dyld import dyld_find as _dyld_find
    def find_library(name):
        if name == 'CoreServices':
            return '/System/Library/Frameworks/CoreServices.framework/CoreServices'
        elif name == 'libSystem.dylib':
            return '/usr/lib/libSystem.dylib'

        possible = ['@executable_path/../lib/lib%s.dylib' % name,
                    'lib%s.dylib' % name,
                    '%s.dylib' % name,
                    '%s.framework/%s' % (name, name)]
        for name in possible:
            try:
                return _dyld_find(name)
            except ValueError:
                continue
        return None

我在 Bigsur 11.5.1 更新后遇到了同样的问题(也许,只是巧合)。上面提供的答案仍然有效,但我很困惑,因为我是新手。以下是要遵循的步骤:

  1. 查看错误,找到安装 python 2.7 的文件 util.py 的路径。使用文本编辑器打开它。
  2. 打开文件后,搜索“posix”,确保它看起来像这样 "posix" and sys.platform == "darwin"
  3. 找到行 def find_library(name):,并在上面的答案中添加脚本。

我附上了截图以供参考。