【问题标题】:Google colab: No module named pytextrank can be found (worked previously with the same notebook)Google colab:找不到名为 pytextrank 的模块(以前使用同一个笔记本)
【发布时间】:2019-12-03 12:30:27
【问题描述】:

从最近开始,我每次运行笔记本时都会收到此错误:

ModuleNotFoundError: No module named 'pytextrank'

这是我笔记本的链接: https://colab.research.google.com/github/neomatrix369/awesome-ai-ml-dl/blob/master/examples/better-nlp/notebooks/jupyter/better_nlp_summarisers.ipynb#scrollTo=-dJrJ54a3w8S

虽然检查显示库已安装,但 python 导入失败 - 我曾在不同的场景中遇到过此问题,并使用以下方法修复它:

python -m pip install pytextrank

但这没有任何影响,错误仍然存​​在。

过去这不是问题,同一个笔记本运行良好 - 我认为这可能是一种回归。

有什么想法吗?任何有用的反馈都将受到高度赞赏。

这是我调用的代码:

import pytextrank
import sys
import networkx as nx
import pylab as plt

我在 colab 单元中得到了这个:

[nltk_data] Downloading package stopwords to /root/nltk_data...
[nltk_data]   Unzipping corpora/stopwords.zip.
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-3-7f30423e40f2> in <module>()
      3 sys.path.insert(0, './awesome-ai-ml-dl/examples/better-nlp/library')
      4 
----> 5 from org.neomatrix369.better_nlp import BetterNLP

1 frames
/content/awesome-ai-ml-dl/examples/better-nlp/library/org/neomatrix369/summariser_pytextrank.py in <module>()
----> 1 import pytextrank
      2 import sys
      3 import networkx as nx
      4 import pylab as plt
      5 

ModuleNotFoundError: No module named 'pytextrank'

---------------------------------------------------------------------------
NOTE: If your import is failing due to a missing package, you can
manually install dependencies using either !pip or !apt.

To view examples of installing some common dependencies, click the
"Open Examples" button below.

【问题讨论】:

  • 我设法解决了这个问题,我不得不更改我的 shell 脚本并重建笔记本,但奇怪的是我不必在 Jupyter Notebooks 或 Kaggle 上这样做,原来的内核/笔记本刚刚工作开箱即用,没有任何更改,但我的新更改现在似乎适用于所有三个平台:Jupyter、Colab 和 Kaggle - 我上次检查时。
  • 让我觉得 Colab 上有些东西导致它不工作,我必须使用固定的安装包方式才能让它工作,看看变化我制作了github.com/neomatrix369/awesome-ai-ml-dl/commit/…,最新版本看起来像这样github.com/neomatrix369/awesome-ai-ml-dl/blob/master/examples/…。 我会鼓励工程团队使用我的 git 历史记录重现该问题。

标签: python jupyter-notebook nlp google-colaboratory pytextrank


【解决方案1】:

您可以运行一个单元以使用pip 在 Colab 中显式安装pytextrank:

!pip install pytextrank

之后,假设已经创建或上传了一个数据文件mih.json,在Colab中运行以下代码:

import pytextrank

path_stage0 = "mih.json"
path_stage1 = "o1.json"

with open(path_stage1, 'w') as f:
    for graf in pytextrank.parse_doc(pytextrank.json_iter(path_stage0)):
        f.write("%s\n" % pytextrank.pretty_print(graf._asdict()))

graph, ranks = pytextrank.text_rank(path_stage1)

for rl in pytextrank.normalize_key_phrases(path_stage1, ranks):
    print(pytextrank.pretty_print(rl))

生成此输出:

["systems", 0.11805817287949238, [2], "np", 1]
["mixed types", 0.09727027009440953, [31, 24], "np", 1]
["minimal set", 0.0656181165649375, [19, 5], "np", 1]
["considered", 0.05314007683878527, [15], "vbn", 2]
["strict inequations", 0.05291409382374228, [11, 12], "np", 1]
["natural numbers", 0.0502966356772243, [6, 7], "np", 1]
["types", 0.048635135047204764, [24], "nns", 3]
["be", 0.04857443543935028, [14], "vb", 3]
["set", 0.03280905828246875, [5], "nn", 4]
["minimal generating sets", 0.03280905828246875, [19, 23, 5], "np", 1]
["solving", 0.03256884170337274, [30], "vbg", 1]
["solutions", 0.030576007873278972, [20], "nns", 3]
["linear constraints", 0.0271278909527188, [3, 4], "np", 1]
["linear diophantine equations", 0.0271278909527188, [3, 9, 10], "np", 1]
["inequations", 0.02645704691187114, [12], "nns", 2]
["nonstrict inequations", 0.02645704691187114, [13, 12], "np", 1]
["numbers", 0.02514831783861215, [7], "nns", 1]
["used", 0.021117092168160108, [29], "vbn", 1]
["given", 0.020891260341666464, [25], "vbn", 1]
["supporting", 0.014650048747874869, [28], "vbg", 1]
["constraints", 0.0135639454763594, [4], "nns", 1]
["diophantine", 0.0135639454763594, [9], "nnp", 1]
["generating", 0.013543878573169788, [23], "nn", 1]
["algorithms", 0.013397910676526051, [21], "nns", 2]
["equations", 0.013056662983606804, [10], "nns", 1]
["constructing", 0.012594570053681782, [27], "vbg", 1]
["upper bounds", 0.012248038294705636, [16, 17], "np", 1]
["nonstrict", 0.011793629610984586, [13], "nn", 1]
["components", 0.0113294598001366, [18], "nns", 1]
["construction", 0.009991849727289892, [22], "nn", 1]
["compatibility", 0.006124019147352818, [1], "nn", 2]
["bounds", 0.006124019147352818, [17], "nns", 1]
["corresponding", 0.006124019147352818, [26], "vbg", 1]
["criteria", 0.004297554552892375, [8], "nns", 2]

【讨论】:

  • 您分享的内容在所有情况下都有效,感谢您,但如果您使用旧版本的 Google Colab 笔记本,您将面临与我相同的问题。该问题特定于 Google CoLab 上的实例,我在 Kaggle Kernels 或我的本地机器上没有此问题!
猜你喜欢
  • 2018-09-14
  • 1970-01-01
  • 1970-01-01
  • 2020-11-16
  • 2015-01-11
  • 1970-01-01
  • 2020-02-13
  • 2020-11-01
  • 1970-01-01
相关资源
最近更新 更多