问题描述
我的项目结构:
kmss/
├── kmss
│ ├── __init__.py
│ ├── items.py
│ ├── pipelines.py
│ ├── settings.py
│ └── spiders
│ ├── __init__.py
│ └── first.py
├── README.rst
├── scrapy.cfg
└── setup.py
我在 mac 上运行它,我的项目文件夹是在以下位置创建的:/user/username/kmss
I am running it on mac and my project folder is created at the location: /user/username/kmss
在 items.py
中,我确实有一个名为 " KmssItem "
的类.如果我要运行first.py
(我的蜘蛛),我必须导入items.py.
,这是一个更高的层次.
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.
我遇到以下问题:
from kmss.items import KmssItem
在items.py
中,代码是:
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
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
我正在使用 anaconda spyder,并且已将路径 /users/username/kmss/kmss
添加到路径管理器.
I am using anaconda spyder and I have added the path /users/username/kmss/kmss
to path manager.
错误:
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
有人可以帮忙吗?
提前致谢
[更新]:
当我在 spyder 上运行 first.py 时,它显示了同样的错误但是,如果我在 anaconda 命令提示符下运行,则不会发生错误.
推荐答案
请尝试 from ..items import KmssItem
P.S.Python 中的绝对导入与相对导入
这篇关于Scrapy 导入模块项目错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!