Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

numpy is a dependency, but this is not declared #406

Open
mcclure opened this issue Sep 24, 2023 · 3 comments
Open

numpy is a dependency, but this is not declared #406

mcclure opened this issue Sep 24, 2023 · 3 comments

Comments

@mcclure
Copy link
Contributor

mcclure commented Sep 24, 2023

python-soundfile requires numpy to run. If numpy is not installed in pip, in my testing python-soundfile will immediately crash on any function invocation with

  File "C:\Users\Andi\work\f\analogue-core-template-amaranth\src\fpga\amaranth_core\my_amaranth_core\toplevel.py", line
451, in bench
    outfile.write(frames)
  File "C:\Users\Andi\work\f\analogue-core-template-amaranth\src\fpga\amaranth_core\.venv\Lib\site-packages\soundfile.py
", line 1017, in write
    import numpy as np
ModuleNotFoundError: No module named 'numpy'

I understand python-soundfile is usually used with numpy, however, the documentation claims it can be used without it.

My expected behavior is that the library should declare all dependencies in setup.py (soundfile does do this for cffi already) so that pip will install them automatically when installing python-soundfile.

Repro

I have a repro but it is not very convenient. You can check out https://github.com/mcclure/analogue-core-template-amaranth and check out revision cd7c72b. You will want to install "pdm" with pip, and then edit src/fpga/amaranth_core/pyproject.toml to include "soundfile" instead of "pysoundfile" and remove the dependency on numpy, then you can run with (cd src/fpga/amaranth_core && python.exe -m pdm install && python.exe -m pdm capture_wav).

The part that uses python-soundfile is here.

Note I only tested this installing numpy with pdm, but my understanding is that uses pip.

@bastibe
Copy link
Owner

bastibe commented Sep 30, 2023

Soundfile can indeed be used without numpy, if you use the buffer_* functions.

These were perhaps a bad idea, as almost all people no doubt use soundfile with numpy. But backwards compatibility is a thing, so that's how it is.

@mcclure
Copy link
Contributor Author

mcclure commented Oct 2, 2023

Let me try again, I think I have a better repro.

Say I create a brand new fresh venv, and then I install python-soundfile. I then run this ultra-simple script:

import soundfile
soundfile.write("test.wav", [[0,0]], 48000)

This fails with "ModuleNotFoundError: No module named 'numpy'". (So does read().)

Looking in setup.py, I find that there is a "numpy" feature in extras_require, and that it does create a numpy dependency. But you only get this if you (?) install with pip install soundfile[numpy], and this is not documented either in the toplevel documentation or the documentation for the functions (such as read and write) which require numpy. (It is mentioned in the changelog that read and write require numpy but buffer_read and buffer_write do not, but this is not a very clear place for it.)

So my "expected behavior" would be one of the following changes:

  • Move numpy from an extras_require to a regular install require (if it helps, I got this working locally and submitted it as PR Require numpy for all installs #409 ). If, as you say, nearly all users of python-soundfile use numpy already, then this change would be harmless.
  • If it is important to you to support the no-numpy buffer_read/buffer_write case, then more specifically document the existence of soundfile[numpy] and specifically note in the function documentation which functions require numpy. (A warning message, or addition to the exception's error string, like "install with soundfile[numpy] to fix this" when the ModuleNotFoundError is raised from read() and write(), would also be helpful, but not required.)

@bastibe
Copy link
Owner

bastibe commented Oct 4, 2023

I agree. Let's require numpy for the next version of soundfile. The entire point of the library is to expose a numpy interface to libsndfile.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants