Mac系统下使用pip3安装playsound模块失败求助
问题解决:Mac下Python 3.12安装playsound失败
问题背景
playsound是仅包含一个同名函数的Python模块,用于播放MP3文件。在Mac系统执行pip3 install playsound时触发以下错误:
Collecting playsound Using cached playsound-1.3.0.tar.gz (7.7 kB) Installing build dependencies ... done Getting requirements to build wheel ... error error: subprocess-exited-with-error × Getting requirements to build wheel did not run successfully. │ exit code: 1 ╰─> [28 lines of output] Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 353, in <module> main() File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 335, in main json_out['return_val'] = hook(**hook_input['kwargs']) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 118, in get_requires_for_build_wheel return hook(config_settings) ^^^^^^^^^^^^^^^^^^^^^ File "/private/var/folders/5x/63vbr8wd1wn82svxg0tpck7h0000gn/T/pip-build-env-vg88gmen/overlay/lib/python3.12/site-packages/setuptools/build_meta.py", line 355, in get_requires_for_build_wheel return self._get_build_requires(config_settings, requirements=['wheel']) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/private/var/folders/5x/63vbr8wd1wn82svxg0tpck7h0000gn/T/pip-build-env-vg88gmen/overlay/lib/python3.12/site-packages/setuptools/build_meta.py", line 325, in _get_build_requires self.run_setup() File "/private/var/folders/5x/63vbr8wd1wn82svxg0tpck7h0000gn/T/pip-build-env-vg88gmen/overlay/lib/python3.12/site-packages/setuptools/build_meta.py", line 507, in run_setup super(_BuildMetaLegacyBackend, self).run_setup(setup_script=setup_script) File "/private/var/folders/5x/63vbr8wd1wn82svxg0tpck7h0000gn/T/pip-build-env-vg88gmen/overlay/lib/python3.12/site-packages/setuptools/build_meta.py", line 341, in run_setup exec(code, locals()) File "<string>", line 6, in <module> File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/inspect.py", line 1282, in getsource lines, lnum = getsourcelines(object) ^^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/inspect.py", line 1264, in getsourcelines lines, lnum = findsource(object) ^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/inspect.py", line 1093, in findsource raise OSError('could not get source code') OSError: could not get source code [end of output] note: This error originates from a subprocess, and is likely not a problem with pip. error: subprocess-exited-with-error × Getting requirements to build wheel 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.
错误原因
playsound 1.3.0版本的安装脚本依赖inspect.getsource函数,而Python 3.12在构建wheel包的环境下无法正确获取源码,导致安装失败——本质是该版本的playsound不兼容Python 3.12。
解决方案
方案1:安装兼容的旧版本playsound
playsound 1.2.2版本兼容Python 3.12,执行以下命令安装:
pip3 install playsound==1.2.2
方案2:使用替代方案播放MP3
如果不想依赖旧版本,可选择以下替代方式:
调用Mac系统自带命令
无需安装额外库,直接用Python调用系统的afplay命令:import subprocess subprocess.run(["afplay", "你的MP3文件路径.mp3"])使用pygame库
安装:pip3 install pygame播放示例:
import pygame pygame.mixer.init() pygame.mixer.music.load("你的MP3文件路径.mp3") pygame.mixer.music.play() # 等待播放结束 while pygame.mixer.music.get_busy(): pygame.time.Clock().tick(10)
内容的提问来源于stack exchange,提问作者Raghav Coder PY
相关产品推荐
相关产品推荐

