python包引用问题
python模块引用梳理 文件组织结构: 复制代码t├── __init__.py├── main.py├── t1│ ├── A.py│ └── __init__.py└── t2 ├── B.py └── __init__.py 复制代码A.py def test(): print 't.t1.A.test()' B.py def test(): print 't.t2.B.test()' 执行: python t/main.py 问题1: 在main.py中引用t2/B的test方法,如何写? 方式1: from xxx import xxx from t2 import BB.test()这个写法很糟糕, 但能解决目前问题。糟糕的地方在于隐晦的引入t2。更好的方式是相对引用。 from .t2 import BB.test()但如果用 python t/main.py执行会报错,此处原因请参考这。原因是相对引用默认作为包的方式才能运行。 正确执行方法(linux shell下): python -m t.main 这个写法也不够好!B在具体的代码行,看不出其出处。更好的方式是 ...





