ImportError: cannot import name 'remove_stopwords' from partially initialized module 'gensim.parsing.preprocessing'

25 views Asked by At

I have Python 3.12.2 and gensim 4.3.2 but when I tried to use Import gensim in my python code I got the error below:

ImportError                               Traceback (most recent call last)
Cell In[1], line 1
----> 1 import gensim

File ~\AppData\Local\Programs\Python\Python312\Lib\site-packages\gensim\__init__.py:11
      7 __version__ = '4.3.2'
      9 import logging
---> 11 from gensim import parsing, corpora, matutils, interfaces, models, similarities, utils  # noqa:F401
     14 logger = logging.getLogger('gensim')
     15 if not logger.handlers:  # To ensure reload() doesn't add another one

File ~\AppData\Local\Programs\Python\Python312\Lib\site-packages\gensim\parsing\__init__.py:4
      1 """This package contains functions to preprocess raw text"""
      3 from .porter import PorterStemmer  # noqa:F401
----> 4 from .preprocessing import (  # noqa:F401
      5     preprocess_documents,
      6     preprocess_string,
      7     read_file,
      8     read_files,
      9     remove_stopwords,
     10     split_alphanum,
     11     stem_text,
     12     strip_multiple_whitespaces,
     13     strip_non_alphanum,
     14     strip_numeric,
     15     strip_punctuation,
     16     strip_short,
     17     strip_tags,
     18 )

File ~\AppData\Local\Programs\Python\Python312\Lib\site-packages\gensim\parsing\preprocessing.py:28
     26 from gensim import utils
     27 from gensim.parsing.porter import PorterStemmer
---> 28 from gensim.parsing.preprocessing import remove_stopwords
     32 STOPWORDS = frozenset([
     33     'all', 'six', 'just', 'less', 'being', 'indeed', 'over', 'move', 'anyway', 'four', 'not', 'own', 'through',
     34     'using', 'fifty', 'where', 'mill', 'only', 'find', 'before', 'one', 'whose', 'system', 'how', 'somewhere',
   (...)
     60     'make', 'once'
     61 ])
     64 RE_PUNCT = re.compile(r'([%s])+' % re.escape(string.punctuation), re.UNICODE)

ImportError: cannot import name 'remove_stopwords' from partially initialized module 'gensim.parsing.preprocessing' (most likely due to a circular import) (C:\Users\saico\AppData\Local\Programs\Python\Python312\Lib\site-packages\gensim\parsing\preprocessing.py)

I have tried different ways but it gives error from pakages and library of python. Any Solution please

1

There are 1 answers

0
gojomo On

The line which is erroring for you – line 28 of gensim/parsing/preprocessing.py, with the circular import of remove_stopwords – does not appear in the official Gensim source code repository, currently or in any versions going back many years:

https://github.com/piskvorky/gensim/blame/develop/gensim/parsing/preprocessing.py#L28

It thus appears you have a non-standard edited version of Gensim in your local environment, with this buggy line of code.

Thus the first thing to try would be completely uninstalling whatever Gensim exists locally, then re-installing a current official release.