没有名为“gensim”的模块,但已经安装了它
Posted
技术标签:
【中文标题】没有名为“gensim”的模块,但已经安装了它【英文标题】:No module named 'gensim' but already installed it 【发布时间】:2020-06-04 16:40:14 【问题描述】:我遇到了这个错误问题,我在 jupyter notebook 的基础(root)环境中运行了这个脚本,日志说 gensim 库已经安装并且我已经运行了命令 !pip install gensim 在我导入之前,还是无法导入,报错ModuleNotFoundError: No module named 'gensim'
!pip install gensim
import gensim
from gensim.models import KeyedVectors
model = KeyedVectors.load('model_fasttext2.vec')
model.vector_size
------------------------------------------------------------------------
Requirement already satisfied: gensim in c:\users\ip-03\anaconda3\lib\site-packages (3.8.1)
Requirement already satisfied: scipy>=0.18.1 in c:\users\ip-03\anaconda3\lib\site-packages (from gensim) (1.4.1)
Requirement already satisfied: six>=1.5.0 in c:\users\ip-03\anaconda3\lib\site-packages (from gensim) (1.14.0)
Requirement already satisfied: smart-open>=1.8.1 in c:\users\ip-03\anaconda3\lib\site-packages (from gensim) (1.9.0)
Requirement already satisfied: numpy>=1.11.3 in c:\users\ip-03\anaconda3\lib\site-packages (from gensim) (1.18.1)
Requirement already satisfied: boto>=2.32 in c:\users\ip-03\anaconda3\lib\site-packages (from smart-open>=1.8.1->gensim) (2.49.0)
Requirement already satisfied: boto3 in c:\users\ip-03\anaconda3\lib\site-packages (from smart-open>=1.8.1->gensim) (1.12.3)
Requirement already satisfied: bz2file in c:\users\ip-03\anaconda3\lib\site-packages (from smart-open>=1.8.1->gensim) (0.98)
Requirement already satisfied: requests in c:\users\ip-03\anaconda3\lib\site-packages (from smart-open>=1.8.1->gensim) (2.22.0)
Requirement already satisfied: s3transfer<0.4.0,>=0.3.0 in c:\users\ip-03\anaconda3\lib\site-packages (from boto3->smart-open>=1.8.1->gensim) (0.3.3)
Requirement already satisfied: botocore<1.16.0,>=1.15.3 in c:\users\ip-03\anaconda3\lib\site-packages (from boto3->smart-open>=1.8.1->gensim) (1.15.3)
Requirement already satisfied: jmespath<1.0.0,>=0.7.1 in c:\users\ip-03\anaconda3\lib\site-packages (from boto3->smart-open>=1.8.1->gensim) (0.9.4)
Requirement already satisfied: certifi>=2017.4.17 in c:\users\ip-03\anaconda3\lib\site-packages (from requests->smart-open>=1.8.1->gensim) (2019.11.28)
Requirement already satisfied: chardet<3.1.0,>=3.0.2 in c:\users\ip-03\anaconda3\lib\site-packages (from requests->smart-open>=1.8.1->gensim) (3.0.4)
Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in c:\users\ip-03\anaconda3\lib\site-packages (from requests->smart-open>=1.8.1->gensim) (1.25.8)
Requirement already satisfied: idna<2.9,>=2.5 in c:\users\ip-03\anaconda3\lib\site-packages (from requests->smart-open>=1.8.1->gensim) (2.8)
Requirement already satisfied: python-dateutil<3.0.0,>=2.1 in c:\users\ip-03\anaconda3\lib\site-packages (from botocore<1.16.0,>=1.15.3->boto3->smart-open>=1.8.1->gensim) (2.8.1)
Requirement already satisfied: docutils<0.16,>=0.10 in c:\users\ip-03\anaconda3\lib\site-packages (from botocore<1.16.0,>=1.15.3->boto3->smart-open>=1.8.1->gensim) (0.15.2)
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-10-ee4a48d372cd> in <module>
1 get_ipython().system('pip install gensim')
----> 2 import gensim
3 from gensim.models import KeyedVectors
4 model = KeyedVectors.load('model_fasttext2.vec')
5 model.vector_size
ModuleNotFoundError: No module named 'gensim'
有没有人可以解决这个问题?非常感谢您的帮助,这将有助于我的论文工作,感谢您的关注
【问题讨论】:
【参考方案1】:您的 jupyter 实验室可能运行的是基本内核,而不是虚拟环境的内核。
执行以下检查:
import sys
sys.executable
进入我的笔记本并得到结果
'/anaconda3/bin/python'
如果你得到的是上面的而不是下面的,那就意味着你使用了错误的内核。
'/anaconda3/envs/myenv/bin/python'
您可以通过为您的新环境创建一个新的 iPython 内核来解决这个问题。阅读更多here。
conda install -n myenv ipython
conda activate myenv
python -m ipykernel install --user --name myenv --display-name "Python (myenv)"
```Then, to run Jupyter Lab in the new environment:
【讨论】:
你也可以this 按照@Azy-Crw4282 的建议,在您的笔记本中,您现在可以使用%pip
魔法,请参阅here 安装到当前内核正在使用的环境中。在你的笔记本中,运行%pip install gensim
。
抱歉,在编辑时间到期之前我没有发现拼写错误。那应该是“...在您的笔记本中,您现在可以使用...”。
感谢您的回答,我正在检查环境,它是错误的,不是我要求的基本(root),我通过将其更改为基本内核来修复它以上是关于没有名为“gensim”的模块,但已经安装了它的主要内容,如果未能解决你的问题,请参考以下文章
ImportError:使用easy_install时没有名为extern的模块