Cant import soundfile

2024/11/13 14:04:08

I'm using Anaconda and I'm trying to import 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

which python

and I get

 /anaconda3/bin/python

But when I open python and try running

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 foundDuring 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:

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.

Answer
  • I experienced this error as a result of using the librosa library, which uses SoundFile
    • The OSError seems to occur because SoundFile is an older version.
    • GitHub-librosa: Cannot load when importing librosas #1117
    • librosa seems to install, and should work with pysoundfile, but in my experience, it did not.
      • Remove pysoundfile: conda remove pysoundfile --force-remove
  • SoundFile is not available for a conda install.
  • In my case, I had already done pip uninstall SoundFile, which then lead to an import error that the package couldn't be found.
  • Then I did pip install SoundFile, which installed a newer version and resolved the error.
  • This worked on Windows 10 with Anaconda, python 3.8.11, SoundFile 0.10.3.post1, librosa 0.8.1
https://en.xdnf.cn/q/72082.html

Related Q&A

Most efficient way to multiply a small matrix with a scalar in numpy

I have a program whose main performance bottleneck involves multiplying matrices which have one dimension of size 1 and another large dimension, e.g. 1000: large_dimension = 1000a = np.random.random((1…

MultiValueDictKeyError / request.POST

I think I hav a problem at request.POST[title]MultiValueDictKeyError at /blog/add/post/"title"Request Method: GETRequest URL: http://119.81.247.69:8000/blog/add/post/Django Version: 1.8.…

How can I auto run py.test once a relative command has been change?

Via autonose or nosy, it will automatically run the nosetests once the some tests file or the relative files have been changes. I would like to ask that whether py.test provides the similar function fo…

Publish a post using XML-RPC WordPress API and Python with category

Im doing a migration from a website to another one which use Wordpress. I created new custom types for my needs (with the plugin Custom Post Types), and I created categories for each custom type.I then…

Django registration email not sending

Ive been trying to get the django-registration-redux account activation email to send to newly registered users.Ive gotten all non-email related parts to work, such as loggin in/out and actually regist…

NumPy data type comparison

I was playing with comparing data types of two different arrays to pick one that is suitable for combining the two. I was happy to discover that I could perform comparison operations, but in the proces…

A simple method for rotating images in reportlab

How can we easily rotate an image using reportlab? I have not found an easy method. The only way found comes from http://dods.ipsl.jussieu.fr/orchidee/SANORCHIDEE/TEMP/TEMP_LOCAL/cdat_portable/lib_new…

XML header getting removed after processing with elementtree

i have an xml file and i used Elementtree to add a new tag to the xml file.My xml file before processing is as follows <?xml version="1.0" encoding="utf-8"?><PackageInfo …

How to ntp server time down to millisecond precision using Python ntplib?

I am creating a python module that will output the time from a selection of NTP Pool servers to millisecond precision as an exercise in showing how server timestamps vary. Thus far I have been able to …

Control 2 separate Excel instances by COM independently... can it be done?

Ive got a legacy application which is implemented in a number of Excel workbooks. Its not something that I have the authority to re-implement, however another application that I do maintain does need t…