方式一
/package1/
.../__init__.py
# 空文件
.../class1.py
class Class1: def __init__(self): self.name = "class one" def printInfo(self): print("i am class One!")
.../class2.py
class Class2: def __init__(self): self.name = "class two" def printInfo(self): print("i am class two!")
/demo1.py
from package1.class1 import Class1 from package1.class2 import Class2 if __name__ == "__main__": c1 = Class1() c1.printInfo() c2 = Class2() c2.printInfo()
#####################################
方式二
/package2/
.../__init__.py
from .class1 import Class1 from .class2 import Class2
... /class1.py
class Class1: def __init__(self): self.name = "class one" def printInfo(self): print("i am class One!")
.../class2.py
class Class2: def __init__(self): self.name = "class two" def printInfo(self): print("i am class two!")
/demo2.py
from package2 import Class1, Class2 if __name__ == "__main__": c1 = Class1() c1.printInfo() c2 = Class2() c2.printInfo()