我厌倦了阅读相对导入的一次性用例,所以我想我想作为一个问题来获得一个示例,说明如何从上面和下面的目录中进行相对导入,用于导入模块函数和类对象。
目录结构:
.
├── lib
│ ├── __init__.py
│ └── bar.py
└── src
├── main.py
└── srclib
├── __init__.py
└── foo.py
酒吧.py
def BarFunc():
print("src Bar function")
class BarClass():
def __inti__(self):
print("src Bar Class")
def test(self):
print("BarClass working")
foo.py
def FooFunction():
print("srclib Foo function")
class FooClass():
def __init__(self):
print("srclib Foo Class")
def test(self):
print("FooClass working")
问题:python 3 中导入这些用例的语法是什么?
主文件
# What is the syntax to import in python 3?
# I want to be able to call FooFunc()
foo.FooFunc()
# I want to be able to create a FooClass() object
foo_class = foo.FooClass()
foo_class.test()
# I want to be able to call FooFunc()
bar.BarFunc()
# I want to be able to create a BarClass() object
bar_class = bar.BarClass()
bar_class.test()