导入错误:无法从部分初始化的模块“gensim.parsing.preprocessing”导入名称“remove_stopwords”

问题描述 投票:0回答:1

我有 Python 3.12.2 和 gensim 4.3.2,但是当我尝试在 python 代码中使用 Import gensim 时,出现以下错误:

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)

我尝试了不同的方法,但它给出了来自 python 包和库的错误。 有什么解决办法请

python-3.x gensim topic-modeling
1个回答
0
投票

对您来说出错的行 –

gensim/parsing/preprocessing.py
的第 28 行,以及
remove_stopwords
的循环导入 – not 出现在官方 Gensim 源代码存储库中,无论是当前还是在许多年前的任何版本中:

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

因此,您的本地环境中似乎有一个非标准编辑版本的 Gensim,其中包含这行有问题的代码。

因此,首先要尝试的是完全卸载本地存在的任何 Gensim,然后重新安装当前的官方版本。

© www.soinside.com 2019 - 2024. All rights reserved.