simonw / llm

Access large language models from the command-line
https://llm.datasette.io
Apache License 2.0
3.7k stars 203 forks source link

"AttributeError: module 'numpy' has no attribute 'int8'" on fresh brew install #531

Closed jcushman closed 1 month ago

jcushman commented 1 month ago

I just did brew install llm on MacOS Sonoma 14.5 on an Intel Mac. llm --help then fails with a stacktrace:

❯ llm --help
Traceback (most recent call last):
  File "/usr/local/bin/llm", line 5, in <module>
    from llm.cli import cli
  File "/usr/local/Cellar/llm/0.14/libexec/lib/python3.12/site-packages/llm/__init__.py", line 16, in <module>
    from .embeddings import Collection
  File "/usr/local/Cellar/llm/0.14/libexec/lib/python3.12/site-packages/llm/embeddings.py", line 7, in <module>
    from sqlite_utils import Database
  File "/usr/local/Cellar/llm/0.14/libexec/lib/python3.12/site-packages/sqlite_utils/__init__.py", line 4, in <module>
    from .db import Database
  File "/usr/local/Cellar/llm/0.14/libexec/lib/python3.12/site-packages/sqlite_utils/db.py", line 211, in <module>
    np.int8: "INTEGER",
    ^^^^^^^
AttributeError: module 'numpy' has no attribute 'int8'

~ via 🐍 v3.12.4 via S

Not sure if related to this numpy bug about removing type aliases, or something about my Intel platform. Let me know if I can share any more info about my setup to debug.

jcushman commented 1 month ago

Aha, I had a broken numpy install in homebrew's python somehow. This fixed it:

rm -r /usr/local/lib/python3.12/site-packages/numpy*

Feel free to close this, except: I wonder if include-system-site-packages = true in the brew pyvenv.cfg is correct. I don't know anything about brew packaging though.

karolszafranski commented 1 month ago

I experienced the same issue today after executing brew update.

The first thing I discovered were warnings when listing installed Python packages:

$ python3.12 -m pip list
WARNING: Skipping /opt/homebrew/lib/python3.12/site-packages/certifi-2024.7.4.dist-info due to invalid metadata entry 'name'
WARNING: Skipping /opt/homebrew/lib/python3.12/site-packages/numpy-2.0.0.dist-info due to invalid metadata entry 'name'

Reinstallation was not possible:

$ python3.12 -m pip install --force-reinstall numpy
error: externally-managed-environment

Γ— This environment is externally managed
╰─> To install Python packages system-wide, try brew install
    xyz, where xyz is the package you are trying to
    install.

brew refused to install numpy, saying it's already there, just not linked.

$ brew install numpy
Warning: numpy 2.0.0 is already installed, it's just not linked.
To link this version, run:
  brew link numpy

Linking numpy failed:

$ brew link numpy
Linking /opt/homebrew/Cellar/numpy/2.0.0...
Error: Could not symlink bin/f2py
Target /opt/homebrew/bin/f2py
already exists. You may want to remove it:
  rm '/opt/homebrew/bin/f2py'

...but llm started working again, and the pip warnings disappeared when listing installed packages.

I won't say if this solution is better or not. I'm also not sure if this solution is correct regarding the multiple Python versions one can have installed (with Homebrew) as well as other Python version managers, but it works for me.

Nevertheless, as a user, I think that it is a bug with llm or rather its Homebrew configuration that should be resolved before closing this issue. It should not be necessary to perform all those steps.

simonw commented 1 month ago

I've seen this too. It's really frustrating. I think the solution may be to add numpy as an explicit Homebrew dependency to avoid this from happening.

I'm going to fix this in sqlite-utils itself.

simonw commented 1 month ago

OK, I've bumped the sqlite-utils dependency to workaround this. Unfortunately it won't be available through Homebrew until a day or two after the next LLM release (which I hope to push shortly).

simonw commented 1 month ago

The Homebrew update is out! Run this:

brew upgrade llm

And the bug is resolved.