"ImportError: cannot import name 'triu' from 'scipy.linalg'" when importing Gensim


I am trying to use Gensim, but running import gensim raises this error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.10/dist-packages/gensim/__init__.py", line 11, in <module>
    from gensim import parsing, corpora, matutils, interfaces, models, similarities, utils  # noqa:F401
  File "/usr/local/lib/python3.10/dist-packages/gensim/corpora/__init__.py", line 6, in <module>
    from .indexedcorpus import IndexedCorpus  # noqa:F401 must appear before the other classes
  File "/usr/local/lib/python3.10/dist-packages/gensim/corpora/indexedcorpus.py", line 14, in <module>
    from gensim import interfaces, utils
  File "/usr/local/lib/python3.10/dist-packages/gensim/interfaces.py", line 19, in <module>
    from gensim import utils, matutils
  File "/usr/local/lib/python3.10/dist-packages/gensim/matutils.py", line 20, in <module>
    from scipy.linalg import get_blas_funcs, triu
ImportError: cannot import name 'triu' from 'scipy.linalg' (/usr/local/lib/python3.10/dist-packages/scipy/linalg/__init__.py)

Why is this happening and how can I fix it?

Asked By: CodeRed



I found the issue.

The scipy.linalg functions tri, triu & tril are deprecated and will be removed in SciPy 1.13.

SciPy 1.11.0 Release Notes § Deprecated features

So, I installed SciPy v1.10.1 instead of the latest version and it was working well.

pip install scipy==1.10.1
Answered By: CodeRed

I wasn’t able to use SciPy v1.10.1, but 1.12 seems to solve the problem too:

pip install scipy==1.12
Answered By: htk

For anaconda users, this fix works:

conda install "scipy<1.13"

I used conda since mixing up conda and pip can break a conda environment and remove some functionality such as reversal to previous version. From their website:

Once pip is used to install software into a conda environment, conda
will be unaware of these changes and may make modifications that would
break the environment.

Answered By: Dudelstein
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.