在同一目录下就不用讲啦,直接import xxx就OK。
在子目录下导入:
文件目录
新建test.py
from pack.animal import Animal
from pack.animal import Cat
ani = Animal('二哈','公')
print(ani.name)
print(ani.sex)
print("------------")
Acat = Cat('小咪咪','母')
print(Acat.name)
print(Acat.sex)
print(Acat.typ())
新建 pack目录,新建animal.py
class Animal():
def __init__(self,name,sex):
self.name = name
self.sex = sex
class Cat(Animal):
def typ(self):
return '哺乳动物'
运行test.py,结果如图: