Python scrapy框架时调用上级items出现attempted relative import with no known parent package

在这里插入图片描述

items.py

import scrapy

# 用于保存所抓取的数据的容器
# 定义字段内容

# 每日天气
class WeatherItem(scrapy.Item):
  # define the fields for your item here like:
  # 今日
  name = scrapy.Field()
  # 天气状态
  status = scrapy.Field()
  # 日期
  date = scrapy.Field()
  # 最高气温
  max = scrapy.Field()
  # 最低气温
  min = scrapy.Field()
  pass


# 每小时天气
class WeatherHourItem(scrapy.Item):
  # 日期
  date = scrapy.Field()
  pass

同级调用items
pipelines.py

from .items import WeatherItem,WeatherHourItem

编译成功

子级调用items
比如sh.py

from ..items import WeatherItem,WeatherHourItem

编译失败,报错attempted relative import with no known parent package

解决方案
引入绝对路径

# 多item
import sys,os
sys.path.append(os.path.abspath(os.path.dirname(__file__) + '/' + '..'))
from weather.items import WeatherItem,WeatherHourItem

猜你喜欢

转载自blog.csdn.net/qq_26003101/article/details/113641536