我试图在我的代码中加载gensim。通常它工作正常。今天,我收到以下异常:
Traceback (most recent call last):
File "/project/6008168/tamouze/just.py", line 2, in <module>
import gensim
File "/project/6008168/tamouze/Python_directory/ENV2.7_new/lib/python2.7/site-packages/gensim/__init__.py", line 5, in <module>
from gensim import parsing, corpora, matutils, interfaces, models, similarities, summarization, utils # noqa:F401
File "/project/6008168/tamouze/Python_directory/ENV2.7_new/lib/python2.7/site-packages/gensim/parsing/__init__.py", line 4, in <module>
from .preprocessing import (remove_stopwords, strip_punctuation, strip_punctuation2, # noqa:F401
File "/project/6008168/tamouze/Python_directory/ENV2.7_new/lib/python2.7/site-packages/gensim/parsing/preprocessing.py", line 40, in <module>
from gensim import utils
File "/project/6008168/tamouze/Python_directory/ENV2.7_new/lib/python2.7/site-packages/gensim/utils.py", line 44, in <module>
from smart_open import smart_open
File "/project/6008168/tamouze/Python_directory/ENV2.7_new/lib/python2.7/site-packages/smart_open/__init__.py", line 1, in <module>
from .smart_open_lib import *
File "/project/6008168/tamouze/Python_directory/ENV2.7_new/lib/python2.7/site-packages/smart_open/smart_open_lib.py", line 29, in <module>
import requests
File "/project/6008168/tamouze/Python_directory/ENV2.7_new/lib/python2.7/site-packages/requests/__init__.py", line 97, in <module>
from . import utils
File "/project/6008168/tamouze/Python_directory/ENV2.7_new/lib/python2.7/site-packages/requests/utils.py", line 26, in <module>
from ._internal_utils import to_native_string
ImportError: cannot import name to_native_string
我使用python 2.7.14和gensim 3.4.0。 我该如何解决这个问题?
答案 0 :(得分:1)
即使您是通过这种方式发现的,该错误实际上并未在gensim
中发生。如果您查看堆栈,则仅由于gensim
使用smart_open
而又使用requests
才触发堆栈。错误发生在requests
中。
如果此方法有效,但现在已停止,则您的环境或启动此代码的方式可能已更改,这与Python和requests
包之间的关系有关。
对于此类错误,您应该尝试在Google中搜索错误堆栈的最后两行-与问题最相关的那两行。这些是:
from ._internal_utils import to_native_string
ImportError: cannot import name to_native_string
(这些省略了特定于您的文件路径,但是有许多唯一的令牌也可能被其他任何人报告过。)
许多其他项目也都采用了这种方法,但是总是通过requests
触发的。有些人报告说重新安装requests
(也许是为了确保它是2.0.0或更高版本)会有所帮助。
如果简单的重新安装没有帮助,您也可以尝试以下一项或全部:
requests
本身),然后进行安装–这可以确保您在不同的地方没有多余的冗余安装,这两者都使问题感到困惑< / li>