V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
dai269619118
V2EX  ›  程序员

Python 嵌套 import 加载文件, 出现问题求解决

  •  
  •   dai269619118 · 2015-11-19 16:54:14 +08:00 · 2458 次点击
    这是一个创建于 3092 天前的主题,其中的信息可能已经有所发展或是发生改变。

    这是我模拟项目里的一个目录结构
    目录结构

    执行文件:main.py

    from test2 import pig
    print 'hello'
    

    /test2/init.py

    from pig import pig
    

    /test2/pig.py

    from test1 import dog
    
    class pig(object):
        pass
    
    print 'pig'
    

    /test1/init.py

    from dog import dog
    from dog1 import dog1
    

    /test1/dog.py

    class dog(object):
        pass
    
    print 'dog'
    

    /test1/dog1.py

    from test2 import pig
    
    class dog1(object):
        pass
    
    print 'dog1'
    

    执行 main.py 之后就报错了

    Traceback (most recent call last):
    dog
      File "/opt/svn/tor/other/main.py", line 2, in <module>
        from test2 import pig
      File "/opt/svn/tor/other/test2/__init__.py", line 2, in <module>
        from pig import pig
      File "/opt/svn/tor/other/test2/pig.py", line 2, in <module>
        from test1 import dog
      File "/opt/svn/tor/other/test1/__init__.py", line 2, in <module>
        from dog1 import dog1
      File "/opt/svn/tor/other/test1/dog1.py", line 1, in <module>
        from test2 import pig
    ImportError: cannot import name pig
    

    大家帮忙看下 这个问题应该怎么解决

    15 条回复    2015-11-22 09:07:26 +08:00
    NeoAtlantis
        1
    NeoAtlantis  
       2015-11-19 18:23:50 +08:00 via Android   ❤️ 1
    billgreen1
        2
    billgreen1  
       2015-11-19 18:36:59 +08:00   ❤️ 1
    简单一点的话, dog1.py 和 test2 不在同一层次,改成
    from ..test2 import pig
    clino
        3
    clino  
       2015-11-19 18:47:27 +08:00   ❤️ 1
    在函数里 import
    dai269619118
        4
    dai269619118  
    OP
       2015-11-19 21:52:54 +08:00
    @NeoAtlantis 已经看过一次了 再看仔细看下
    @billgreen1
    Traceback (most recent call last):
    File "/opt/svn/tor/other/main.py", line 2, in <module>
    from test2 import pig
    File "/opt/svn/tor/other/test2/__init__.py", line 2, in <module>
    from pig import pig
    File "/opt/svn/tor/other/test2/pig.py", line 2, in <module>
    from test1 import dog
    File "/opt/svn/tor/other/test1/__init__.py", line 2, in <module>
    from dog1 import dog1
    File "/opt/svn/tor/other/test1/dog1.py", line 3, in <module>
    from ..test2 import pig
    ValueError: Attempted relative import beyond toplevel package
    改完还是报错
    NeoAtlantis
        5
    NeoAtlantis  
       2015-11-19 22:19:34 +08:00   ❤️ 1
    from test2.pig import pig 试试
    billgreen1
        6
    billgreen1  
       2015-11-20 00:23:07 +08:00   ❤️ 1
    哈,这个好玩,模块名,类名一样,而且重复导入
    RickyBoy
        7
    RickyBoy  
       2015-11-20 01:01:36 +08:00   ❤️ 1
    Python 2 模块和类名相同的确很烦
    RickyBoy
        8
    RickyBoy  
       2015-11-20 01:36:55 +08:00
    好吧,看了看问题应该出在重复导入
    dai269619118
        9
    dai269619118  
    OP
       2015-11-20 09:21:35 +08:00
    @NeoAtlantis 还是不行 ImportError: cannot import name pig
    @billgreen1
    @RickyBoy
    重复导入 有什么办法解决吗?
    billgreen1
        10
    billgreen1  
       2015-11-20 11:44:15 +08:00   ❤️ 1
    重新组织你的包吧。你的 test1 要用到 test2 里面的, test2 又要用到 test1 里面的,说明你可以考虑把它们整合到一起。

    2.看看 pep8 ,模块名要小写,类名 CamelCase ,导入的时候尽量导入模块,不要导入类,尤其在你写模块的时候。
    dai269619118
        11
    dai269619118  
    OP
       2015-11-20 12:05:12 +08:00
    @billgreen1 恩恩 只能重新组织包了 非常感谢!
    walleL
        12
    walleL  
       2015-11-20 14:29:22 +08:00   ❤️ 1
    这是循环 import 的问题吧。 A 依赖 B.x, B 依赖 A.x

    应该:
    1. 重新组织结构,把 A.x 、 B.x 独立出来成一个模块
    2. 如果使用 1 的方法不好解决,就在调用函数的内部 import
    dai269619118
        13
    dai269619118  
    OP
       2015-11-20 15:00:20 +08:00
    @walleL 谢谢 已经重新组织了
    SmiteChow
        14
    SmiteChow  
       2015-11-21 16:44:14 +08:00
    循环引用,只能在用到时进行 import 才能避免错误
    maijiawei
        15
    maijiawei  
       2015-11-22 09:07:26 +08:00 via Android
    😍
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3997 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 04:17 · PVG 12:17 · LAX 21:17 · JFK 00:17
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.