【发布时间】:2017-06-06 14:44:07
【问题描述】:
我刚接触python 4 天。我只是想了解根 __init__.py 导入功能。谷歌搜索很多以理解相同但无法找到一个有用的链接(可能是我的搜索键不相关)。请分享一些链接。
我收到错误消息“ImportError: cannot import name Person”
下面是结构
Example(directory)
model
__init__.py (empty)
myclass.py
__init__.py
run.py
myclass.py
class Person(object):
def __init__(self):
self.name = "Raja"
def print_name(self):
print self.name
__init__.py
from model.myclass import Person
运行.py
from model import Person
def donext():
person = Person()
person.print_name()
if __name__ == '__main__':
donext()
【问题讨论】:
-
Person不在model中——它在model.myclass中。您在__init__.py中正确地进行了导入。为什么你在run.py中的做法不同? -
为什么要加
flask标签? -
@john-gordon 在 run.py 中,我只是想从 _init_.py 导入并使用相同的
-
@gonczor 抱歉,已删除,因此不适用
标签: python python-2.7