无法访问Python中的导入函数

3 python aptana import pydev

有人可以帮我这个吗?

我正在继续使用PyDev Aptana开发python代码.这是我在PyDev IDE下的项目结构:

/testProject
        /src
            /testModule  
            __init__.py
            testMod.py
        main.py
Run Code Online (Sandbox Code Playgroud)

testMod.py文件:

def test(n): 
    print "echo"+n 
Run Code Online (Sandbox Code Playgroud)

main.py文件:

import testModule
testModule.test(4) 
Run Code Online (Sandbox Code Playgroud)

当我尝试在PyDev中运行它时,它在main.py,第2行(其中调用test(4))时给了我这个错误:

AttributeError: 'module' object has no attribute 'test'
Run Code Online (Sandbox Code Playgroud)

我将main.py更改为:

import testModule.test
testModule.test(4)  
Run Code Online (Sandbox Code Playgroud)

仍然给我错误 'module' object not callable!

这有什么问题?

Ach*_*him 8

你错过了这个testMod模块.您的方法的全名是testModule.testMod.test.

  • 具体来说,`testMod`单独指的是`__init __.py`(可以用来轻松提供整个包的API的有限但有用的子集). (2认同)