M1芯片Mac下Python环境配置与机器学习包兼容问题咨询
M1 Mac 上 Python 环境配置与机器学习依赖兼容问题
背景
有三年TypeScript和Node.js开发经验,正在拓展技能学习Python,后续计划深入计算机视觉、机器学习领域,但在M1芯片Mac上配置稳定Python环境时遇到诸多困扰,找不到问题根源。问题多集中在机器学习包(如TensorFlow、Whisper、OpenCV,其中OpenCV已解决)的安装与运行阶段,不确定是否和M1对相关依赖的支持有关。
当前对Python环境的认知
- Python对M1的支持高度依赖版本
- venv是唯一需使用的环境管理器
- 应使用pyenv安装Python版本,避免与macOS自带Python冲突
设备与环境
Mac Pro M1,macOS v12.6(Monterey)
pyenv 2.3.9
Python 3.7.13
Fish shell 3.5.1
项目启动流程与遇到的问题
虚拟环境激活问题
项目启动步骤:
- 用venv创建虚拟环境:
python3 -m venv <some_environment_name> - 用Visual Studio Code打开目录并激活虚拟环境
执行激活命令时出错:
source /Users/paal/src/whisper_transcription/bin/activate.fish
报错输出:
functions: Function '_old_fish_prompt' does not exist ~/src/whisper_transcription/bin/activate.fish (line 18): functions -c _old_fish_prompt fish_prompt ^ in function 'deactivate' with arguments 'nondestructive' called on line 30 of file ~/src/whisper_transcription/bin/activate.fish from sourcing file ~/src/whisper_transcription/bin/activate.fish (Type 'help functions' for related documentation) functions: Function 'fish_prompt' does not exist ~/src/whisper_transcription/bin/activate.fish (line 47): functions -c fish_prompt _old_fish_prompt ^ from sourcing file ~/src/whisper_transcription/bin/activate.fish (Type 'help functions' for related documentation) fish: Unknown command: _old_fish_prompt ~/src/whisper_transcription/bin/activate.fish (line 71): _old_fish_prompt ^ in function 'fish_prompt' in command substitution (whisper_transcription)
临时解决方法:在fish.config中添加以下语句
if type -q $program _old_fish_prompt end
或切换到Z shell(执行zsh),这是Fish shell的常见问题。
依赖版本兼容问题
切换到Z shell激活环境后,能运行简单脚本,但安装机器学习包时出现兼容性问题。例如导入whisper时报错:
import whisper ... # 其余代码
错误输出:
Traceback (most recent call last): File "main.py", line 1, in <module> import whisper File "/Users/paal/src/whisper_transcription/lib/python3.7/site-packages/whisper/__init__.py", line 12, in <module> from .decoding import DecodingOptions, DecodingResult, decode, detect_language File "/Users/paal/src/whisper_transcription/lib/python3.7/site-packages/whisper/decoding.py", line 514 if prefix := self.options.prefix: ^ SyntaxError: invalid syntax
原因是:=运算符需要Python3.8及以上版本支持,但Whisper依赖的PyTorch当时仅支持到Python3.7.9,陷入版本兼容循环。
求助需求
现在陷入循环困境:所需包的依赖要么不支持M1平台,要么不兼容Python版本,现有知识无法解决。希望获得环境管理的通用建议,能可靠开展Python开发,避免每次学习都陷入耗时的环境配置问题。另外想了解:
- 是否有资源能理清这类版本兼容问题?
- 当前M1芯片上的Python开发是否普遍存在这类问题?
- 如何快速克服基础环境问题,进入实际学习阶段?
内容的提问来源于stack exchange,提问作者this
相关产品推荐
相关产品推荐

