gensim: cannot import name 'triu' from 'scipy.linalg' (tested)
ImportError: cannot import name 'triu' from 'scipy.linalg' at import gensim means gensim 4.3.2 or older met SciPy 1.13 or newer, which removed triu. gensim 4.4.0 fixes it, and also fixes numpy.dtype size changed under NumPy 2. Tested below.
1. Reproduce: gensim 4.3.2 with a current SciPy
A fresh Python 3.11 venv and pip install gensim==4.3.2. Its metadata says only scipy>=1.7.0 and numpy>=1.18.5, so the resolver picks the newest of both. b1.py:
import numpy, scipy
print("numpy", numpy.__version__, "scipy", scipy.__version__)
import gensim
from gensim.models import Word2Vec
print("gensim", gensim.__version__)
sents = [["cat", "sat", "mat"], ["dog", "sat", "log"]] * 50
m = Word2Vec(sents, vector_size=8, min_count=1, seed=1, workers=1)
print(len(m.wv), "words in vocabulary")
Output, exit code 1 (paths shortened to /venv):
$ pip install gensim==4.3.2
$ python b1.py
numpy 2.4.6 scipy 1.17.1
Traceback (most recent call last):
File "b1.py", line 3, in <module>
import gensim
File "/venv/lib/python3.11/site-packages/gensim/__init__.py", line 11, in <module>
from gensim import parsing, corpora, matutils, interfaces, models, similarities, utils # noqa:F401
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/venv/lib/python3.11/site-packages/gensim/corpora/__init__.py", line 6, in <module>
from .indexedcorpus import IndexedCorpus # noqa:F401 must appear before the other classes
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/venv/lib/python3.11/site-packages/gensim/corpora/indexedcorpus.py", line 14, in <module>
from gensim import interfaces, utils
File "/venv/lib/python3.11/site-packages/gensim/interfaces.py", line 19, in <module>
from gensim import utils, matutils
File "/venv/lib/python3.11/site-packages/gensim/matutils.py", line 20, in <module>
from scipy.linalg import get_blas_funcs, triu
ImportError: cannot import name 'triu' from 'scipy.linalg' (/venv/lib/python3.11/site-packages/scipy/linalg/__init__.py)
scipy.linalg.triu (and tril) were removed in SciPy 1.13.0. With numpy 1.26.4, gensim 4.3.2 imported fine on SciPy 1.12.0 and failed with this exact error on SciPy 1.13.0. gensim 4.3.1 has the same uncapped metadata.
2. Fix: upgrade gensim, or pin scipy<1.13
$ pip install -U gensim
$ python b1.py
numpy 2.4.6 scipy 1.17.1
gensim 4.4.0
5 words in vocabulary
gensim 4.4.0 works with NumPy 2.4.6 and SciPy 1.17.1. It has wheels for Python 3.9 to 3.13. If you must keep 4.3.2:
$ pip install "scipy<1.13"
$ python b1.py
numpy 1.26.4 scipy 1.12.0
gensim 4.3.2
5 words in vocabulary
Pinning SciPy below 1.13 also pulled NumPy back to 1.26.4, because SciPy 1.12 does not support NumPy 2.
3. gensim 4.3.3 and NumPy 2: numpy.dtype size changed
gensim 4.3.3 fixed the triu import but pins numpy<2.0 and scipy<1.14.0. A clean install resolves to numpy 1.26.4 + scipy 1.13.1 and works. Asking for NumPy 2 alongside it is refused:
error: No solution found when resolving dependencies
cause: Because gensim==4.3.3 depends on numpy>=1.18.5,<2.0 and you require gensim==4.3.3, we can conclude that you require numpy>=1.18.5,<2.0.
And because you require numpy==2.2.6, we can conclude that your requirements are unsatisfiable.
Forcing numpy==2.2.6 in a second command (or any later install that upgrades NumPy) gives, exit code 1:
numpy 2.2.6 scipy 1.13.1
Traceback (most recent call last):
File "b1.py", line 3, in <module>
import gensim
File "/venv/lib/python3.11/site-packages/gensim/__init__.py", line 11, in <module>
from gensim import parsing, corpora, matutils, interfaces, models, similarities, utils # noqa:F401
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/venv/lib/python3.11/site-packages/gensim/corpora/__init__.py", line 6, in <module>
from .indexedcorpus import IndexedCorpus # noqa:F401 must appear before the other classes
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/venv/lib/python3.11/site-packages/gensim/corpora/indexedcorpus.py", line 14, in <module>
from gensim import interfaces, utils
File "/venv/lib/python3.11/site-packages/gensim/interfaces.py", line 19, in <module>
from gensim import utils, matutils
File "/venv/lib/python3.11/site-packages/gensim/matutils.py", line 1034, in <module>
from gensim._matutils import logsumexp, mean_absolute_difference, dirichlet_expectation
File "gensim/_matutils.pyx", line 1, in init gensim._matutils
ValueError: numpy.dtype size changed, may indicate binary incompatibility. Expected 96 from C header, got 88 from PyObject
The compiled gensim._matutils was built against NumPy 1.x. Fix: pip install -U gensim (4.4.0, section 2) or go back to NumPy 1:
$ pip install "numpy<2"
$ python b1.py
numpy 1.26.4 scipy 1.13.1
gensim 4.3.3
5 words in vocabulary
Restart any Jupyter kernel after changing versions. Same NumPy 2 problem elsewhere: SciPy, statsmodels, scikit-learn, PyArrow. All changes: NumPy 2 breaking changes.
Frequently asked questions
How do I fix cannot import name 'triu' from 'scipy.linalg' in gensim?
pip install -U gensim (4.4.0 worked with SciPy 1.17.1 and NumPy 2.4.6), or pin scipy<1.13 for gensim 4.3.2.
Which SciPy version removed triu?
SciPy 1.13.0. gensim 4.3.2 imported on SciPy 1.12.0 and failed on 1.13.0 in this test.
Does gensim support NumPy 2?
gensim 4.4.0 does. 4.3.3 pins numpy<2 and fails with numpy.dtype size changed if NumPy 2 is forced.
Written with AI assistance. Measured on 2026-10-24 on Linux with Python 3.11.17: gensim 4.3.2 with scipy 1.12.0/1.13.0/1.17.1, gensim 4.3.3 with numpy 1.26.4 and 2.2.6, and gensim 4.4.0 with numpy 2.4.6 + scipy 1.17.1. The script ran verbatim and output was pasted unedited (paths shortened). Version pins read from PyPI metadata the same day. PlainTables is our extension; gensim, SciPy and NumPy are free.