Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 嵌套包:AttributeError:module';app&x27;没有属性_Python - Fatal编程技术网

Python 嵌套包:AttributeError:module';app&x27;没有属性

Python 嵌套包:AttributeError:module';app&x27;没有属性,python,Python,我刚刚学习python,来自C#和Java背景,我对导入系统感到非常困惑。只是为了学习而尝试运行一个简单的测试,但是获取一个错误属性error:module“app”没有属性“example” 请参阅下面的代码,是否有人可以解释引发错误的原因?我似乎只有在包中有包时才会遇到这个问题,如包“app”中包含的包“example”实例所示 run.py import app import app.example import app.example.a import app.example.b

我刚刚学习python,来自C#和Java背景,我对导入系统感到非常困惑。只是为了学习而尝试运行一个简单的测试,但是获取一个错误属性error:module“app”没有属性“example”

请参阅下面的代码,是否有人可以解释引发错误的原因?我似乎只有在包中有包时才会遇到这个问题,如包“app”中包含的包“example”实例所示

run.py

import app
import app.example
import app.example.a
import app.example.b
print("TESTING: " + str(app.example.b)) #error is thrown on this line
print("LOADED B")
app/\uuuu init\uuuuu.py

import app
import app.example
import app.example.a
import app.example.b
print("TESTING: " + str(app.example.b)) #error is thrown on this line
print("LOADED B")
app/example/\uuuuu init\uuuuuuu.py

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

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

import app
import app.example
import app.example.a
import app.example.b
print("TESTING: " + str(app.example.b)) #error is thrown on this line
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
“C:\Program Files\Python 3.5\npwc services\Scripts\Python.exe”C:/Users/xxxxxxxx/pycharm项目/untitled1/run.py
回溯(最近一次呼叫最后一次):
负载B
文件“C:/Users/xxxxxxxx/PycharmProjects/untitled1/run.py”,第1行,在
导入应用程序
文件“C:\Users\xxxxxxxx\PycharmProjects\untitled1\app\\uuuuu init\uuuuuuuu.py”,第1行,在
导入app.example
文件“C:\Users\xxxxxxxx\PycharmProjects\untitled1\app\example\\uuuuu init\uuuuuu.py”,第1行,在
导入app.example.a
文件“C:\Users\xxxxxxxx\PycharmProjects\untitled1\app\example\a.py”,第2行,在
打印(“测试:+str(应用示例b))
AttributeError:模块“app”没有属性“example”
进程已完成,退出代码为1
目录结构:

"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

run.py

import app
app/init.py

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

app/\uuuuu init\uuuuuu.py
中尝试
import.example
,并在其他模块和软件包中执行此操作。在包中需要相对导入,而不是绝对导入。谢谢,直到你提到它,我才知道相对导入是可能的,但是你提供的确切语法在Python 3中不适用。我不得不这样做:from.example.app导入一个更正,我最终使用from。导入示例,该示例似乎与您在评论中提供的内容直接等效。