ARM64 macOS下用Poetry/Pip安装Pydantic遇架构及编译错误求助
问题:ARM64 macOS下Poetry安装Pydantic的架构不兼容及编译错误
问题1:Pydantic架构不兼容导入错误
使用Poetry安装Pydantic后运行时出现如下导入错误:
E ImportError: dlopen(/Users/sazzad/Library/Caches/pypoetry/virtualenvs/my-test-project-qHFD2Grb-py3.9/lib/python3.9/site-packages/pydantic/__init__.cpython-39-darwin.so, 0x0002): tried: '/Users/sazzad/Library/Caches/pypoetry/virtualenvs/my-test-project-qHFD2Grb-py3.9/lib/python3.9/site-packages/pydantic/__init__.cpython-39-darwin.so' (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64')), '/System/Volumes/Preboot/Cryptexes/OS/Users/sazzad/Library/Caches/pypoetry/virtualenvs/my-test-project-qHFD2Grb-py3.9/lib/python3.9/site-packages/pydantic/__init__.cpython-39-darwin.so' (no such file), '/Users/sazzad/Library/Caches/pypoetry/virtualenvs/my-test-project-qHFD2Grb-py3.9/lib/python3.9/site-packages/pydantic/__init__.cpython-39-darwin.so' (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64'))
问题2:手动编译安装Pydantic时的Rust缺失错误
尝试在Poetry虚拟环境中执行以下命令重新安装:
pip uninstall pydantic && python3.9 -m pip install --no-binary :all: pydantic --nocache-dir
得到如下错误:
creating build/lib.macosx-10.9-universal2-cpython-39/maturin copying maturin/__init__.py -> build/lib.macosx-10.9-universal2-cpython-39/maturin copying maturin/import_hook.py -> build/lib.macosx-10.9-universal2-cpython-39/maturin copying maturin/__main__.py -> build/lib.macosx-10.9-universal2-cpython-39/maturin running egg_info creating maturin.egg-info writing maturin.egg-info/PKG-INFO writing dependency_links to maturin.egg-info/dependency_links.txt writing requirements to maturin.egg-info/requires.txt writing top-level names to maturin.egg-info/top_level.txt writing manifest file 'maturin.egg-info/SOURCES.txt' reading manifest file 'maturin.egg-info/SOURCES.txt' reading manifest template 'MANIFEST.in' warning: no files found matching '*.json' under directory 'src/python_interpreter' writing manifest file 'maturin.egg-info/SOURCES.txt' running build_ext running build_rust error: can't find Rust compiler If you are using an outdated pip version, it is possible a prebuilt wheel is available for this package but pip is not able to install from it. Installing from the wheel would avoid the need for a Rust compiler. To update pip, run: pip install --upgrade pip and then retry package installation. If you did intend to build this package from source, try installing a Rust compiler from your system package manager and ensure it is on the PATH during installation. Alternatively, rustup (available at https://rustup.rs) is the recommended way to download and update the Rust compiler toolchain. [end of output] note: This error originates from a subprocess, and is likely not a problem with pip. ERROR: Failed building wheel for maturin Failed to build maturin ERROR: Could not build wheels for maturin, which is required to install pyproject.toml-based projects [end of output] note: This error originates from a subprocess, and is likely not a problem with pip. error: subprocess-exited-with-error × pip subprocess to install build dependencies did not run successfully. │ exit code: 1 ╰─> See above for output. note: This error originates from a subprocess, and is likely not a problem with pip.
解决思路
- 优先安装ARM64预编译wheel,避免源码编译:
- 升级pip到最新版本,确保能正确识别ARM64架构的预编译包:
pip install --upgrade pip - 重新用Poetry安装Pydantic,或直接用pip强制重装:
pip install pydantic --force-reinstall --no-cache-dir
- 升级pip到最新版本,确保能正确识别ARM64架构的预编译包:
- 若必须源码编译:
安装Rust工具链,完成后重启终端确保Rust在系统PATH中,再重新执行编译安装命令。 - 验证Python与系统架构一致性:
执行以下命令确认当前Python是ARM64版本:
若输出不是python3 -c "import platform; print(platform.machine())"arm64,需重新安装适配ARM64的Python版本,再重建Poetry虚拟环境。 - 清理旧虚拟环境重新安装:
- 删除现有Poetry虚拟环境:
poetry env remove python3.9 - 重新创建虚拟环境并安装依赖:
poetry env use python3.9 poetry install
- 删除现有Poetry虚拟环境:
内容的提问来源于stack exchange,提问作者Sazzad
相关产品推荐
相关产品推荐

