【问题标题】:Scrapy import module items errorScrapy导入模块项目错误
【发布时间】:2015-10-08 05:07:15
【问题描述】:

我的项目结构:

kmss/
├── kmss
│   ├── __init__.py
│   ├── items.py
│   ├── pipelines.py
│   ├── settings.py
│   └── spiders
│       ├── __init__.py
│       └── first.py
├── README.rst
├── scrapy.cfg
└── setup.py

我在 mac 上运行它,我的项目文件夹创建在以下位置:/user/username/kmss

items.py 中,我确实有一个名为" KmssItem " 的类。 如果我要运行first.py(我的蜘蛛),我必须导入items.py.,这是更高级别的。

我对以下行有疑问:

from kmss.items import KmssItem

items.py内,代码为:

from scrapy import Item, Field

class KmssItem(Item):
    # define the fields for your item here like:
    ##image_urls= Field()
    ##Images = Field()
    title = Field()
    ##url= Field()
    pass

first.py 的代码

from scrapy.contrib.spiders import CrawlSpider , Rule
from scrapy.contrib.linkextractors import LinkExtractor
from kmss.items import KmssItem


class FirstSpider(CrawlSpider):
    name = "first"
    ## you do not find it to go to facebook links 
    allowed_domains = ["www.reddit.com"]
    start_urls = [
        'http://www.reddit.com/r/pics/',
    ## some other codes

我正在使用 anaconda spyder,并且已将路径 /users/username/kmss/kmss 添加到路径管理器中。

错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/username/anaconda/lib/python2.7/site-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 685, in runfile
    execfile(filename, namespace)
  File "/Users/username/anaconda/lib/python2.7/site-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 78, in execfile
    builtins.execfile(filename, *where)
  File "/Users/username/kmss/kmss/spiders/first.py", line 4, in <module>
    from kmss.items import KmssItem
ImportError: No module named kmss.items

有人可以帮忙吗?

提前谢谢你

[更新]:

当我在 spyder 上运行 first.py 时,它显示了同样的错误 但是,如果我在 anaconda 命令提示符下运行,则不会发生错误。

【问题讨论】:

  • 导入模块项目错误。我刚刚在上面添加了它
  • 你是如何运行它的?
  • 我尝试在 spyder python 控制台和 mac 终端 'scrapy crawl first'
  • @ZéRicardo 请你帮忙:) 即使我重新创建项目,我也遇到同样的问题
  • 文件夹结构和代码看起来不错。 scrapy crawl first 应该在项目的根目录下工作。为了确定,我会停用 anaconda 并尝试在新的 virtualenv 中运行它。

标签: python python-2.7 web-crawler scrapy


【解决方案1】:

请尝试from ..items import KmssItem
P.S. Absolute vs Relative Imports in Python

【讨论】:

    猜你喜欢
    • 2020-04-23
    • 1970-01-01
    • 1970-01-01
    • 2016-06-12
    • 2018-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多