Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
567 views
in Technique[技术] by (71.8m points)

python - Scrapy import module items error

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...