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

3

40

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?

Nielsen answered 5/4 at 10:1 Comment(1)
Upstream issue: github.com/piskvorky/gensim/issues/3525 Upstream PR: github.com/piskvorky/gensim/pull/3524 A release fixing this is targeted for Spring 2024.Kenning
N
61

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
Nielsen answered 5/4 at 10:36 Comment(6)
The release notes tell us to use the numpy functions with the same names. Is gensim really that far out of date?Abohm
The Scipy release that removes these functions is a few days old. The last Gensim release is >7mo old, & its use of Scipy so stable it only required scipy>=1.7, from long before (2021) these functions deprecated (June 2023). You may have unrealistic hopes about how fast an open source project does cleanup around deprecation hints - where changes may not be riskless, & deprecated functions may work indefinitely. You could also ask why Scipy breaks things with less than a year's warning! But, given the ease of workarounds – use an older, tested version – both projects' approaches are fine.Malapropism
pip install "scipy<1.3" will install the most updated version that supports lingalg.triu (i.e. 1.2.3)Sanguinaria
@Sanguinaria Note that you've dropped a "1" there. I think you meant pip install "scipy<1.13" 1.2.3 is much older.Kenning
@NickODell correct, that's what I meant. thanks!Sanguinaria
To patch the problem temporarily without downgrading, I replaced the import inside gensim/matutils.py in my venv with from numpy import triu, thanks to @hpaulj, and Python being an interpreted language.Pimental
P
37

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

pip install scipy==1.12
Predecease answered 9/4 at 19:1 Comment(3)
Correct. Although scipy.linalg.triu was deprecated in 1.11, it was only removed in 1.13. Versions 1.11 and 1.12 will work, but produce a warning. This is a good alternative for anyone that can't use 1.10Kenning
thanks, is work for me, i'm using python 3.12.2Renin
Thanks, it worked for me. I also had to restart the local jupyter kernel for the error to be fixed.Clipfed
F
2

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.

Foote answered 28/5 at 13:8 Comment(1)
this works for pyenv too; scipy<1.13 into requirements.txtEartha

© 2022 - 2024 — McMap. All rights reserved.