如何将模块导入 Jupyter notebook

How to import modules into Jupyter notebook

所以我在 Windows OS.

中使用 anaconda 和 conda

正如您所知,Jupyter 会随 Anaconda 自动安装。 使用 python 命令时,我的 python 代码 运行 正常

python myfile.py

然而,当我尝试通过 jupyter 运行 它时,我收到以下错误

ImportError                               Traceback (most recent call last)
<ipython-input-1-43605f892034> in <module>()
      1 #!/usr/bin/env python
      2 import os
----> 3 from gensim import corpora, models, similarities
      4 from gensim.models import LsiModel
      5 import logging

ImportError: No module named gensim

为了排除故障,我检查了以下内容

  1. 确保 运行 jupyter notebook 启用激活正确的环境
  2. 确保激活的环境安装了 "gensim package"

所以为了解决这个问题我需要通过jupyter本身安装缺少的库

所以我必须在 jupyter 单元格中添加并运行以下命令

pip install --upgrade gensim