2006年06月04日 星期日 17:02
如题,需要引用上一级目录下的py文件
2006年06月04日 星期日 23:31
举一个例子吧:
目录结构为:
demo
|----demo.py
|----A
|----__init__.py
|----KlassA.py
|----B
|----__init__.py
|----KlassB.py
其中没有.py后缀的都是目录。
[KlassB.py]
import A.KlassA
print 'KlassB'
[KlassA.py]
print 'KlassA'
[demo.py]
import A.B.KlassB
在KlassB.py中import了上一级目录的KlassA module.在demo目录下运行python demo.py的结果为:
KlassA
KlassB
这里的原理在于,在KlassB中import A.KlassA时,Python
会首先在目录B中寻找子目录A,由于寻找失败,Python会转向当前工作目录,即目录demo,寻找子目录A,当然会成功。
On 6/4/06, flyaflya <flyaflya at gmail.com> wrote:
> 如题,需要引用上一级目录下的py文件
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
--
Robert
Python源码剖析――http://blog.donews.com/lemur/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060604/68efd56b/attachment.html
Zeuux © 2025
京ICP备05028076号