【问题标题】:Why python throws an "ImportError: No module named linkextractor" even if the Scrapy module already installed?为什么即使已经安装了 Scrapy 模块,python 也会抛出“ImportError:没有名为 linkextractor 的模块”?
【发布时间】:2014-04-04 17:00:03
【问题描述】:

我想安装portia。它需要scrapyslybot。 我还安装了virtualenv(按照推荐)并执行了virtualenv Portia,只为这个项目创建了一个python虚拟环境。现在它需要用twistd -n slyd 启动twisted 服务器,但是它会抛出一个错误:

  File "/home/john/Downloads/portia-master/slybot/slybot/linkextractor/__init__.py", line 6, in <module>
    from .base import BaseLinkExtractor, ALLOWED_SCHEMES   File "/home/john/Downloads/portia-master/slybot/slybot/linkextractor/base.py", line 6, in <module>
    from scrapy.linkextractor import IGNORED_EXTENSIONS ImportError: No module named linkextractor

我有 Ubuntu 12.04,安装:python 2.7, pip, python-scrapy, python-twisted

这里可能有什么问题?我之前没有使用过pipvirtualenv,我上面描述的部署工作流程(例如环境变量、其他virtualenv/pip 配置)似乎有问题?

【问题讨论】:

    标签: python web-scraping scrapy pip virtualenv


    【解决方案1】:

    在python3中,只需将s添加到linkextractor的末尾即可。

    替换:

    from scrapy.linkextractor import LinkExtractor
    

    与:

    from scrapy.linkextractors import LinkExtractor
                             ^
    

    并享受它。

    有关更多信息,请阅读来自here 的 Scrapy 文档。

    【讨论】:

      【解决方案2】:

      它漏掉了中间部分; contrib,其中linkextractor是一个子模块。

      应该是:

      scrapy.contrib.linkextractor
             ^^^^^^^
      

      【讨论】:

      • 我没有编辑代码,只是想玩一下。在 github 项目中没有与错误导入名称相关的问题。马上试试
      • @static 你确定你的依赖版本和github代码一样吗?
      • 其实不是来自项目,是来自Scrapy项目,他们也有from scrapy.linkextractor import IGNORED_EXTENSIONS
      • @static 没有scrapy.linkextractor 这样的东西。检查scrapy的文档。
      • 当我按照你的建议更改它时,它实际上返回相同:from scrapy.contrib.linkextractor import IGNORED_EXTENSIONS ImportError: No module named linkextractor
      【解决方案3】:

      好吧,我认为你应该在 github scrapinhub repo 中看到这个问题 (https://github.com/scrapinghub/portia/issues/13)。

      我解决了一些在我的系统中而不是在 virtualenv 中安装库的导入错误。

      在我的例子中,库是 jsonschema (pip install jsonschema) 和 slybot (pip install -e slybot),在我运行 requirements.txt 之后。

      最好的问候!

      【讨论】:

        最近更新 更多