无法访问Python中的导入函数
谁能帮我解决这个问题吗?
我将继续使用 PyDev Aptana 来开发 python 代码。这是我在 PyDev IDE 下的项目结构:
/testProject
/src
/testModule
__init__.py
testMod.py
main.py
testMod.py 文件:
def test(n):
print "echo"+n
main.py 文件:
import testModule
testModule.test(4)
当我尝试在 PyDev 中运行它时,它在 main.py 第 2 行(调用 test(4) 的地方)给了我这个错误:
AttributeError: 'module' object has no attribute 'test'
我将 main.py 更改为:
import testModule.test
testModule.test(4)
仍然给出错误 'module' object not callable!
这有什么问题?
Can anyone please help me with this ?
I am moving on using PyDev Aptana for developing python codes. Here is my project structure under PyDev IDE :
/testProject
/src
/testModule
__init__.py
testMod.py
main.py
testMod.py file :
def test(n):
print "echo"+n
main.py file :
import testModule
testModule.test(4)
When I try to run this in PyDev , it gave me this error at main.py , line 2 ( where test(4) is called):
AttributeError: 'module' object has no attribute 'test'
I change main.py to :
import testModule.test
testModule.test(4)
still gives me error 'module' object not callable!
What is wrong with this ??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您错过了
testMod
模块。您的方法的全名是testModule.testMod.test
。You missed the
testMod
module. The full name of your method istestModule.testMod.test
.嗯,这基本上是因为
testModule
中没有方法test()
。事实上,你的testModule
不是模块,而是一个包,而testMod
是testModule
包中的模块。因此,根据您的结构,以下内容将起作用:
请参阅 http://docs.python.org/tutorial /modules.html 了解更多详细信息
Well, this is basically because there is no method
test()
intestModule
. In fact, yourtestModule
is not module, but is a package, whiletestMod
is a module intestModule
package.So, with your structure, the following will work:
See http://docs.python.org/tutorial/modules.html for more details