macOS运行说明.md 1.5 KB

macOS 运行说明

问题描述

在 macOS 上使用 Anaconda 环境运行程序时,可能会遇到 Qt 库冲突问题,错误信息如下:

objc[...]: Class ... is implemented in both .../libQt5Core.5.15.2.dylib and .../PyQt5/Qt5/lib/QtCore.framework/...
qt.qpa.plugin: Could not load the Qt platform plugin "cocoa"

这是因为 Anaconda 自带的 Qt 库与 PyQt5 的 Qt 库发生冲突。

解决方案

方案 1:使用提供的启动脚本(推荐)

使用项目根目录下的 run_mac.sh 脚本启动程序:

./run_mac.sh

这个脚本会在启动 Python 之前设置正确的环境变量。

方案 2:使用 Conda 安装 PyQt5(最佳方案)

使用 Conda 安装 PyQt5 可以避免库冲突:

conda install pyqt

然后直接运行:

python main.py

方案 3:创建独立的 Conda 环境

创建一个新的 conda 环境,只安装必要的包:

conda create -n dibang python=3.12
conda activate dibang
conda install pyqt
pip install pandas openpyxl pymysql pyserial
python main.py

方案 4:使用虚拟环境(不推荐,但可行)

如果必须使用 pip 安装的 PyQt5,可以尝试:

python -m venv venv
source venv/bin/activate
pip install -r requirements.txt
python main.py

注意事项

  • macOS 的 System Integrity Protection (SIP) 可能会阻止在运行时修改 DYLD_LIBRARY_PATH
  • 如果使用 Anaconda,建议使用 Conda 安装 PyQt5 而不是 pip
  • 如果问题持续存在,请考虑使用方案 2 或方案 3