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

pip install playsound安装报错,pip install flask正常,求解决以播放MP3

解决pip安装playsound失败的问题

问题说明

执行pip install playsound时出现构建wheel失败的错误,核心报错为OSError: could not get source code,但pip install flask可正常运行,目标是用playsound播放MP3文件。完整报错信息如下:

PS D:\100 day python> pip 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 "C:\Users\aadit\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\pip\_vendor\pyproject_hooks\_in_process\_in_process.py", line 353, in <module>
          main()
        File "C:\Users\aadit\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\pip\_vendor\pyproject_hooks\_in_process\_in_process.py", line 335, in main
          json_out['return_val'] = hook(**hook_input['kwargs'])
                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "C:\Users\aadit\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\pip\_vendor\pyproject_hooks\_in_process\_in_process.py", line 118, in get_requires_for_build_wheel
          return hook(config_settings)
                 ^^^^^^^^^^^^^^^^^^^^^
        File "C:\Users\aadit\AppData\Local\Temp\pip-build-env-_apbfavu\overlay\Lib\site-packages\setuptools\build_meta.py", line 325, in get_requires_for_build_wheel
          return self._get_build_requires(config_settings, requirements=['wheel'])
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "C:\Users\aadit\AppData\Local\Temp\pip-build-env-_apbfavu\overlay\Lib\site-packages\setuptools\build_meta.py", line 295, in _get_build_requires
          self.run_setup()
        File "C:\Users\aadit\AppData\Local\Temp\pip-build-env-_apbfavu\overlay\Lib\site-packages\setuptools\build_meta.py", line 480, in run_setup
          super(_BuildMetaLegacyBackend, self).run_setup(setup_script=setup_script)
        File "C:\Users\aadit\AppData\Local\Temp\pip-build-env-_apbfavu\overlay\Lib\site-packages\setuptools\build_meta.py", line 311, in run_setup
          exec(code, locals())
        File "<string>", line 6, in <module>
        File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.2032.0_x64__qbz5n2kfra8p0\Lib\inspect.py", line 1262, in getsource    
          lines, lnum = getsourcelines(object)
                        ^^^^^^^^^^^^^^^^^^^^^^
        File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.2032.0_x64__qbz5n2kfra8p0\Lib\inspect.py", line 1244, in getsourcelines
          lines, lnum = findsource(object)
                        ^^^^^^^^^^^^^^^^^^
        File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.2032.0_x64__qbz5n2kfra8p0\Lib\inspect.py", line 1081, 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.

原因分析

  1. 使用的是微软商店版本的Python,该版本源码受系统权限限制,导致inspect.getsource无法读取源码,而playsound 1.3.0的安装脚本依赖此功能。
  2. playsound 1.3.0版本的构建脚本存在兼容性问题,不支持Python 3.11及微软商店版Python。

解决方法

方法一:安装兼容版本的playsound

直接指定安装1.2.2版本,该版本安装脚本不依赖inspect.getsource,兼容性更好:

pip install playsound==1.2.2

方法二:替换Python版本

卸载微软商店版Python,安装官方版Python(勾选"Add Python to PATH"选项),之后重新执行pip install playsound即可。

方法三:使用替代库播放MP3

如果上述方法无效,可换用更稳定的音频库如pygame:

  1. 安装pygame:
pip install pygame
  1. 播放MP3的示例代码:
import pygame
pygame.mixer.init()
pygame.mixer.music.load("你的MP3文件路径.mp3")
pygame.mixer.music.play()
while pygame.mixer.music.get_busy():
    continue

内容的提问来源于stack exchange,提问作者Aaditya Shah

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 20:02:05