Scrapy import module items error

2020-02-13 05:52发布

My project structure:

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

I am running it on mac and my project folder is created at the location: /user/username/kmss

And within items.py I do have a class named " KmssItem ". If I am going to run the first.py ( my spider), I have to import items.py., which is at a higher level.

I am having problem with the following line:

from kmss.items import KmssItem

Within items.py, the codes are:

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

Code for 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

I am using anaconda spyder and I have added the path /users/username/kmss/kmss to path manager.

Error:

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

Could anyone give a help on this?

Thank you in advance

[Updated]:

When i run the first.py at spyder, it showed the same error However, if I run in anaconda command prompt, the error did not happen.

1条回答
男人必须洒脱
2楼-- · 2020-02-13 06:19

Please try from ..items import KmssItem
P.S. Absolute vs Relative Imports in Python

查看更多
登录 后发表回答