简体   繁体   English

无法导入声音文件

[英]Can't import soundfile

I'm using Anaconda and I'm trying to import soundfile/pysoundfile.我正在使用 Anaconda 并且我正在尝试导入 soundfile/pysoundfile。

I installed the package by running我通过运行安装了该软件包

 conda install -c conda-forge pysoundfile 

and I think it succeeded because when I run我认为它成功了因为当我跑步时

 conda list 

it shows up:它出现:

pyopenssl                 17.2.0           py36h5d7bf08_0  
pyparsing                 2.2.0            py36hb281f35_0  
pyqt                      5.6.0            py36he5c6137_6  
pysocks                   1.6.7            py36hfa33cec_1  
pysoundfile               0.10.1           py_0            conda-forge
pytables                  3.4.2            py36hfbd7ab0_2  
pytest                    3.2.1            py36h9963153_1  

To make sure I'm running the "right" python, I've tried running为了确保我运行的是“正确的”python,我试过运行

which python

and I get我得到

 /anaconda3/bin/python

But when I open python and try running但是当我打开 python 并尝试运行时

import soundfile 

I get the following:我得到以下信息:

Traceback (most recent call last):
  File "/anaconda3/lib/python3.6/site-packages/soundfile.py", line 142, in <module>
    raise OSError('sndfile library not found')
OSError: sndfile library not found

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/anaconda3/lib/python3.6/site-packages/soundfile.py", line 163, in <module>
    _path, '_soundfile_data', _libname))
OSError: cannot load library '/anaconda3/lib/python3.6/site-packages/_soundfile_data/libsndfile.dylib': dlopen(/anaconda3/lib/python3.6/site-packages/_soundfile_data/libsndfile.dylib, 2): image not found

Out of curiosity I opened the soundfile.py file with a text editor to see what line 142 looked like, and it looks like this:出于好奇,我用文本编辑器打开了 soundfile.py 文件,看看第 142 行是什么样子,它看起来像这样:

try:
    _libname = _find_library('sndfile')
    if _libname is None:
        raise OSError('sndfile library not found')      # <---- line 142
    _snd = _ffi.dlopen(_libname)
except OSError:
    if _sys.platform == 'darwin':
        _libname = 'libsndfile.dylib'
    elif _sys.platform == 'win32':
        from platform import architecture as _architecture
        _libname = 'libsndfile' + _architecture()[0] + '.dll'
    else:
        raise

    # hack for packaging tools like cx_Freeze, which
    # compress all scripts into a zip file
    # which causes __file__ to be inside this zip file

    _path = _os.path.dirname(_os.path.abspath(__file__))

    while not _os.path.isdir(_path):
        _path = _os.path.abspath(_os.path.join(_path, '..'))

    _snd = _ffi.dlopen(_os.path.join(
        _path, '_soundfile_data', _libname))

In the docs, it says "import soundfile" is the correct way to import, but I also tried在文档中,它说“导入声音文件”是正确的导入方式,但我也尝试过

import pysoundfile 

and only got并且只得到

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'pysoundfile'

Is there anyone who knows why this is happening?有没有人知道为什么会这样? I'm running this on MacOS btw.顺便说一句,我在 MacOS 上运行它。

Read the Breaking Changes section here此处阅读重大更改部分

we changed the import name from import pysoundfile to import soundfile in 0.7.我们在 0.7 中将导入名称从 import pysoundfile 更改为 import soundfile。

workaround解决方法

pip install SoundFile

在此处输入图片说明

  • I experienced this error as a result of using the librosa library, which uses SoundFile我经历过这种错误是使用的结果librosa库,它使用SoundFile
    • The OSError seems to occur because SoundFile is an older version. OSError似乎是因为SoundFile是旧版本而发生的。
    • GitHub-librosa: Cannot load when importing librosas #1117 GitHub-librosa:导入 librosas 时无法加载 #1117
    • librosa seems to install, and should work with pysoundfile , but in my experience, it did not. librosa似乎已安装,并且应该与pysoundfile一起pysoundfile ,但根据我的经验,它没有。
      • Remove pysoundfile : conda remove pysoundfile --force-remove删除pysoundfile : pysoundfile conda remove pysoundfile --force-remove
  • SoundFile is not available for a conda install. SoundFile是不可用于conda安装。
  • In my case, I had already done pip uninstall SoundFile , which then lead to an import error that the package couldn't be found.就我而言,我已经完成了pip uninstall SoundFile ,然后导致无法找到包的导入错误。
  • Then I did pip install SoundFile , which installed a newer version and resolved the error.然后我做了pip install SoundFile ,它安装了一个更新的版本并解决了错误。
  • This worked on Windows 10 with Anaconda, python 3.8.11 , SoundFile 0.10.3.post1 , librosa 0.8.1这适用于带有 Anaconda、 python 3.8.11SoundFile 0.10.3.post1librosa 0.8.1 Windows 10

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM