【发布时间】:2015-08-12 20:03:34
【问题描述】:
我的文件夹结构是这样的:
pythonstuff/
program.py
moduleA/
__init__.py
foo.py
bar.py
这是我的bar.py 代码:
def hello():
print("Hello, world!")
这是我的program.py 代码:
#!/usr/bin/env python3
from moduleA import bar
bar.hello()
我跑$ python3 program.py
不知怎的,我得到了一个错误:
File "program.py", line 3, in <module>
bar.hello()
AttributeError: 'module' object has no attribute 'hello'
编辑:我的__init__.py 文件为空。
Edit2:在尝试了一切之后,我终于意识到我在根目录中有一个bar.py,它实际上包含hello() 方法。 moduleA/ 目录中的bar.py 为空。
【问题讨论】:
-
您的 init 文件中有什么内容?
-
初始化文件为空
-
它在 Linux 上的 Python 3.4.0(默认,2014 年 4 月 11 日,13:05:11)[GCC 4.8.2] 下按照我的预期运行。检查你的 PYTHONPATH???
-
你有旧的 bar.pyc 文件吗?
-
哎呀,我在根目录中有一个 bar.py,其中包含最初发布在我的问题中的代码。我的 moduleA 目录中的 bar.py 是一个空白文件。将文件从根目录移动到 moduleA 目录解决了这个问题。我应该删除这个问题还是...?