Unable to import items in scrapy

后端 未结 4 1166
广开言路
广开言路 2021-01-06 05:27

I have a very basic spider, following the instructions in the getting started guide, but for some reason, trying to import my items into my spider returns an error. Spider a

相关标签:
4条回答
  • 2021-01-06 06:03

    I also had this several times while working with scrapy. You could add at the beginning of your Python modules this line:

    from __future__ import absolute_import
    

    More info here:

    • http://www.python.org/dev/peps/pep-0328/#rationale-for-absolute-imports
    • http://pythonquirks.blogspot.ru/2010/07/absolutely-relative-import.html
    0 讨论(0)
  • 2021-01-06 06:09

    you are importing a field ,you must import a class from items.py like from myproject.items import class_name.

    0 讨论(0)
  • 2021-01-06 06:10

    if the structure like this:

    package/
        __init__.py
        subpackage1/
            __init__.py
            moduleX.py
            moduleY.py
        subpackage2/
            __init__.py
            moduleZ.py
        moduleA.py
    

    and if you are in moduleX.py, the way to import other modules can be:

    from .moduleY.py import *
    
    from ..moduleA.py import *
    
    from ..subpackage2.moduleZ.py import *
    

    refer:PEP Imports: Multi-Line and Absolute/Relative

    0 讨论(0)
  • 2021-01-06 06:20

    So, this was a problem that I came across the other day that I was able to fix through some trial and error, but I wasn't able to find any documentation of it so I thought I'd put this up in case anyone happens to run into the same problem I did.

    This isn't so much an issue with scrapy as it is an issue with naming files and how python deals with importing modules. Basically the problem is that if you name your spider file the same thing as the project then your imports are going to break. Python will try to import from the directory closest to your current position which means it's going to try to import from the spider's directory which isn't going to work.

    Basically just change the name of your spider file to something else and it'll all be up and running just fine.

    0 讨论(0)
提交回复
热议问题