V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
janusle
V2EX  ›  Python

在Stackoverflow上问了个关于import的问题 没什么人回答 转到这里 望解答

  •  
  •   janusle · 2012-02-17 17:25:24 +08:00 · 4203 次点击
    这是一个创建于 4451 天前的主题,其中的信息可能已经有所发展或是发生改变。
    就不翻译啦 很浅的英语

    The following is structure of my program

    ./abc.py

    ./a/__init__.py

    ./b/__init__.py

    ./b/t.py

    Source code of abc.py

    =============
    import a
    =============

    Source code of ./a/__init__.py

    =======================
    from b import t
    =======================

    Source of of ./b/t.py

    =================
    def aa():
    print "bbb"
    =================

    ./b/__init__.py is an empty file.

    There is a statement

    ========
    t.aa()
    ========

    if I put it into ./a/__init__.py, then when I run abc.py, it works fine. But if I put it into abc.py, when I run the script, I get an error like

    Traceback (most recent call last): File "abc.py", line 3, in t.aa() NameError: name 't' is not defined

    What's the reason of this? Thank you in advance.
    10 条回复    1970-01-01 08:00:00 +08:00
    binux
        1
    binux  
       2012-02-17 17:43:02 +08:00
    a.t.aa() ?
    lyxint
        2
    lyxint  
       2012-02-17 17:53:34 +08:00
    如果放在abc.py里, 那么abc.py是这样子:

    import a
    t.bb()

    肯定报NameError
    janusle
        3
    janusle  
    OP
       2012-02-17 18:55:28 +08:00
    @lyxint 我其实不明白 a和b是同级的目录 为什么a里面可以import到b的内容?
    janusle
        4
    janusle  
    OP
       2012-02-17 19:01:43 +08:00
    @lyxint 还有 就是我的理解是这样的 如果把t.aa()放在a目录的__init__.py里面

    abc.py 一旦import a

    它就变成
    from b import t

    t.aa()

    这和我直接把t.aa()放在abc.py里面应该一样啊?
    难道python的import方式不是这样吗?

    我是以c和php的经验来猜测的
    janusle
        5
    janusle  
    OP
       2012-02-17 19:04:16 +08:00
    @binux 有点看懂了 我其实还是不理解 为什么 a能import到b的内容 我理解的是 python搜索应该是 向下搜索啊 这个时候 a的__init__.py在a下面 怎么能够找到上层目录(a)的同级目录b里面的内容呢?
    binux
        6
    binux  
       2012-02-17 19:19:21 +08:00
    @janusle 不是,当你在abc这一级执行的时候,搜索目录是包含./的,在这一层当然是能找到b的

    你可以这么测试,在a的目录中执行python __init__.py,这时候就找不到b了

    然后
    你在a/__init__.py中加上
    print sys.path
    执行abc.py的时候,就知道查找路径了
    janusle
        7
    janusle  
    OP
       2012-02-17 19:35:18 +08:00
    @binux 有点明白了 再问一个问题 如果在./a/__init__.py 里面的放入t.aa() 我在abc这一级执行的时候 python再前面加了包名 则变成a.t.aa() 是这样么?
    binux
        8
    binux  
       2012-02-17 19:42:58 +08:00
    @janusle 不是这个原因

    from b import t
    这一句话已经将t导入当前的命名空间里了,那就可以直接t.aa()了

    如果你在abc.py中这么写
    from a import t
    也是一样可以t.aa()的

    import a
    只是将module a导入到当前命名空间中,而不是a里面的东西。
    janusle
        9
    janusle  
    OP
       2012-02-17 19:51:32 +08:00
    @binux 恩恩 明白啦 谢谢 我再去看一点 命名空间的东西
    clowwindy
        10
    clowwindy  
       2012-02-17 21:40:15 +08:00
    推荐在所有文件里import都写绝对模块名,不要用相对的模块名,增加可读性。
    http://www.python.org/dev/peps/pep-0008/
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5071 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 03:49 · PVG 11:49 · LAX 20:49 · JFK 23:49
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.