You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Apple M2 macOS环境下Heroku Local运行Pydantic架构不兼容问题求助

Apple M2上运行heroku local web出现Pydantic架构不兼容问题的排查与解决

系统环境配置

  • macOS Ventura 13.0
  • CPU: Apple M2(ARM架构)
  • Python版本: 3.10.0
  • Heroku版本: heroku/7.67.1 darwin-x64 node-v14.19.0
  • Pydantic版本: 1.10.2

执行heroku local web时的错误信息

9:19:25 PM web.1 |      from fastapi import params
9:19:25 PM web.1 |    File "/Users/wj/Library/Caches/pypoetry/virtualenvs/fuse-hotel-MQQZRb-z-py3.11/lib/python3.11/site-packages/fastapi/params.py", line 4, in <module>
9:19:25 PM web.1 |      from pydantic.fields import FieldInfo, Undefined
9:19:25 PM web.1 |  ImportError: dlopen(/Users/wj/Library/Caches/pypoetry/virtualenvs/fuse-hotel-MQQZRb-z-py3.11/lib/python3.11/site-packages/pydantic/__init__.cpython-311-darwin.so, 0x0002): tried: '/Users/wj/Library/Caches/pypoetry/virtualenvs/fuse-hotel-MQQZRb-z-py3.11/lib/python3.11/site-packages/pydantic/__init__.cpython-311-darwin.so' (mach-o file, but is an incompatible architecture (have 'arm64', need 'x86_64')), '/System/Volumes/Preboot/Cryptexes/OS/Users/wj/Library/Caches/pypoetry/virtualenvs/fuse-hotel-M
9:19:25 PM web.1 |  >  QQZRb-z-py3.11/lib/python3.11/site-packages/pydantic/__init__.cpython-311-darwin.so' (no such file), '/Users/wj/Library/Caches/pypoetry/virtualenvs/fuse-hotel-MQQZRb-z-py3.11/lib/python3.11/site-packages/pydantic/__init__.cpython-311-darwin.so' (mach-o file, but is an incompatible architecture (have 'arm64', need 'x86_64'))
[DONE] Killing all processes with signal  SIGINT
9:19:25 PM web.1 Exited with exit code null

项目依赖列表(poetry show输出)

alembic            1.8.1     A database migration tool for SQLAlchemy.
amqp               5.1.1     Low-level AMQP client for Python (fork of amqplib).
anyio              3.6.2     High level compatibility layer for multiple asynchronous event loop implementations
asgiref            3.5.2     ASGI specs, helper code, and adapters
async-timeout      4.0.2     Timeout context manager for asyncio programs
asyncpg            0.27.0    An asyncio PostgreSQL driver
attrs              22.1.0    Classes Without Boilerplate
billiard           3.6.4.0   Python multiprocessing fork with improvements and bugfixes
black              22.12.0   The uncompromising code formatter.
celery             5.2.7     Distributed Task Queue.
certifi            2022.12.7 Python package for providing Mozilla's CA Bundle.
click              8.1.3     Composable command line interface toolkit
click-didyoumean   0.3.0     Enables git-like *did-you-mean* feature in click
click-plugins      1.1.1     An extension module for click to enable registering CLI commands via setuptools entry-points.
click-repl         0.2.0     REPL plugin for Click
fastapi            0.86.0    FastAPI framework, high performance, easy to learn, fast to code, ready for production
fastapi-camelcase  1.0.5     Package provides an easy way to have camelcase request/response bodies for Pydantic
flake8             3.9.2     the modular source code checker: pep8 pyflakes and co
gevent             22.10.2   Coroutine-based network library
greenlet           2.0.1     Lightweight in-process concurrent programming
h11                0.14.0    A pure-Python, bring-your-own-I/O implementation of HTTP/1.1
httpcore           0.16.2    A minimal low-level HTTP client.
httptools          0.5.0     A collection of framework independent HTTP protocol utils.
httpx              0.23.1    The next generation HTTP client.
idna               3.4       Internationalized Domain Names in Applications (IDNA)
iniconfig          1.1.1     iniconfig: brain-dead simple config-ini parsing
kombu              5.2.4     Messaging library for Python.
mako               1.2.4     A super-fast templating language that borrows the best ideas from the existing templating languages.
markupsafe         2.1.1     Safely add untrusted strings to HTML/XML markup.
mccabe             0.6.1     McCabe checker, plugin for flake8
mypy-extensions    0.4.3     Experimental type system extensions for programs checked with the mypy typechecker.
packaging          22.0      Core utilities for Python packages
pathspec           0.10.3    Utility library for gitignore style pattern matching of file paths.
platformdirs       2.6.0     A small Python package for determining appropriate platform-specific dirs, e.g. a "user data dir".
pluggy             1.0.0     plugin and hook calling mechanisms for python
prompt-toolkit     3.0.36    Library for building powerful interactive command lines in Python
pycodestyle        2.7.0     Python style guide checker
pycountry          22.3.5    ISO country, subdivision, language, currency and script definitions and their translations
pydantic           1.10.2    Data validation and settings management using python type hints
pyflakes           2.3.1     passive checker of Python programs
pyhumps            3.8.0     🐫  Convert strings (and dictionary keys) between snake case, camel case and pascal case in Python. Inspired by Humps for Node
pytest             7.2.0     pytest: simple powerful testing with Python
python-dotenv      0.21.0    Read key-value pairs from a .env file and set them as environment variables
python-json-logger 2.0.4     A python library adding a json log formatter
pytz               2022.6    World timezone definitions, modern and historical
pyyaml             6.0       YAML parser and emitter for Python
redis              4.4.0     Python client for Redis database and key-value store
rfc3986            1.5.0     Validating URI References per RFC 3986
setuptools         65.6.3    Easily download, build, install, upgrade, and uninstall Python packages
six                1.16.0    Python 2 and 3 compatibility utilities
sniffio            1.3.0     Sniff out which async library your code is running under
sqlalchemy         1.4.45    Database Abstraction Library
starlette          0.20.4    The little ASGI library that shines.
typing-extensions  4.4.0     Backported and Experimental Type Hints for Python 3.7+
uvicorn            0.19.0    The lightning-fast ASGI server.
uvloop             0.17.0    Fast implementation of asyncio event loop on top of libuv
vine               5.0.0     Promises, promises, promises.
watchfiles         0.18.1    Simple, modern and high performance file watching and code reload in python.
wcwidth            0.2.5     Measures the displayed width of unicode strings in a terminal
websockets         10.4      An implementation of the WebSocket Protocol (RFC 6455 & 7692)
zope-event         4.6       Very basic event publishing system
zope-interface     5.5.2     Interfaces for Python

Pydantic版本检查结果

执行命令:

python3 -c "import pydantic.utils; print(pydantic.utils.version_info())"

输出:

pydantic version: 1.10.2
            pydantic compiled: True
                 install path: /Users/wj/Library/Caches/pypoetry/virtualenvs/fuse-hotel-MQQZRb-z-py3.11/lib/python3.11/site-packages/pydantic
               python version: 3.11.0 (v3.11.0:deaf509e8f, Oct 24 2022, 14:43:23) [Clang 13.0.0 (clang-1300.0.29.30)]
                     platform: macOS-13.0-arm64-arm-64bit
     optional deps. installed: ['dotenv', 'typing-extensions']

疑问

Apple M2属于ARM架构,为何错误提示需要x86_64架构?该如何解决这个问题?


问题原因与解决方案

原因分析

  1. Heroku CLI架构不匹配:当前安装的Heroku CLI是darwin-x64版本,会通过Rosetta 2模拟x86_64环境运行,导致启动的web进程继承x86_64架构要求,但Pydantic是针对ARM64编译安装的,二者架构冲突。
  2. Python版本环境混乱:系统默认Python为3.10.0,但虚拟环境使用了Python 3.11.0,加剧了环境不一致性。

解决步骤

步骤1:替换为ARM64版本的Heroku CLI

卸载现有x86_64版本Heroku CLI,重新安装适配Apple Silicon的ARM64版本。

步骤2:重建匹配架构的虚拟环境

  • 删除现有虚拟环境:
    poetry env remove python3.11
    
  • 指定系统Python 3.10.0创建新虚拟环境:
    poetry env use /usr/bin/python3.10
    
  • 重新安装所有依赖:
    poetry install
    

步骤3:验证环境架构

执行以下命令确认虚拟环境的Python为ARM64架构:

python -c "import platform; print(platform.machine())"

输出应为arm64。

步骤4:重新运行服务

完成上述步骤后,再次执行heroku local web,架构不兼容问题即可解决。

内容的提问来源于stack exchange,提问作者Jie Wu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 15:05:20