嵌套包:AttributeError:module' app'没有属性

时间:2015-11-21 04:40:43

标签: python

我正在学习python,来自C#& amp; Java背景,我对导入系统感到很困惑。只是为了学习目的而尝试运行一个简单的测试,但是得到一个错误AttributeError:module' app'没有属性'示例'

请参阅下面的代码,有人可以解释为什么会抛出错误吗?当包中有一个包时,我似乎只遇到这个问题,如包"示例"包含在包" app"

run.py

import app

应用/ __初始化__。PY

import app.example

应用/示例/ __初始化__。PY

import app.example.a

应用/示例/ a.py

import app.example.b
print("TESTING: " + str(app.example.b)) #error is thrown on this line

应用/示例/ b.py

print("LOADED B")

错误抛出:

"C:\Program Files\Python 3.5\npwc-services\Scripts\python.exe" C:/Users/xxxxxxxx/PycharmProjects/untitled1/run.py
Traceback (most recent call last):
LOADED B
  File "C:/Users/xxxxxxxx/PycharmProjects/untitled1/run.py", line 1, in <module>
    import app
  File "C:\Users\xxxxxxxx\PycharmProjects\untitled1\app\__init__.py", line 1, in <module>
    import app.example
  File "C:\Users\xxxxxxxx\PycharmProjects\untitled1\app\example\__init__.py", line 1, in <module>
    import app.example.a
  File "C:\Users\xxxxxxxx\PycharmProjects\untitled1\app\example\a.py", line 2, in <module>
    print("TESTING: " + str(app.example.b))
AttributeError: module 'app' has no attribute 'example'

Process finished with exit code 1

目录结构:

enter image description here

1 个答案:

答案 0 :(得分:1)

run.py

import app

应用程序/的初始化的.py

from .example.app import a # or b or ...