返回信息流最近在学习scrapy,正好看到如下引用模块的方法:
之前也看到很多这种方式,为了一探究竟这个引入的类Spider,我今天就在scrapy的源文件(即路径/usr/localpython2/lib/python2.7/site-packages/scrapy/下)找到spiders这个包,如下图:
然后进入这个spiders后,发现全是.py模块文件,然后在下图的__init__.py文件中找到了类Spider:
__init.py__文件中的类Spider如下:
我的问题是:为什么用from scrapy.spiders import Spider就可以了,而不用from scrapy.spiders.__init__ import Spider?(就是具体到类Spider所在的__init__.py文件中)。而且当我尝试改成后者的时候,反而就会出错了。希望各位前辈赐教指导一下,感谢!
这是一条镜像帖。来源:北邮人论坛 / python / #7728同步于 2015/7/6
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Python机器人发帖
求教关于from import的问题
zjd10211766
2015/7/6镜像同步4 回复
订阅后,新回复会通过你的通知中心匿名送达。
4 条回复
这明显是import系统的一个特殊规定啊,import的时候自动去加载相应文件夹下的__init__.py文件,所有的包都遵循这个标准,所以就不用显式调用了嘛
另外这种问题显然可以直接去查文档嘛:
“Regular packages are traditional packages as they existed in Python 3.2 and earlier. A regular package is typically implemented as a directory containing an __init__.py file. When a regular package is imported, this __init__.py file is implicitly executed, and the objects it defines are bound to names in the package’s namespace.”
(摘自https://docs.python.org/3.5/reference/import.html#regular-packages)
虽然这是python3的说明,但python2应该也是一样的。
感谢啦,之前查过2.7的文档,没找到。后来仔细查了下找到了。不过很感谢哈~
【 在 qcts 的大作中提到: 】
这明显是import系统的一个特殊规定啊,import...