Search code examples
pythonscrapyrelative-import

Failing at importing modules on items.py file - Scrapy


I'm new in Python and I'm currently learning webscraping with spiders. Following the tutorial, I stucked at relative importing with Python.

This is the structure of my current folder (provided by scrapy startproject p1):

folder structure

My items.py file:

# Define here the models for your scraped items
#
# See documentation in:
# https://docs.scrapy.org/en/latest/topics/items.html

import scrapy


class Test(scrapy.Item):
    # define the fields for your item here like:
    # name = scrapy.Field()
    title = scrapy.Field()
    price = scrapy.Field()
    pass

In my filetwo.py, it contains:

import scrapy
from p1.items import Test

When I run the code, I get "ModuleNotFoundError: No module named 'p1'"

I also read some people online that faced the same problem, so I tried ..items import Test and still didn't work. It gave me the error: ImportError: attempted relative import with no known parent package

Can someone give me a light?

Thanks in advance!


Solution

  • I am sure my comment should help you. But here is an example

    enter image description here

    enter image description here

    enter image description here