Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
565 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 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...