开发者

can't access imported functions in Python

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 :

/testProje开发者_Python百科ct
        /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 ??


You missed the testMod module. The full name of your method is testModule.testMod.test.


Well, this is basically because there is no method test() in testModule. In fact, your testModule is not module, but is a package, while testMod is a module in testModule package.

So, with your structure, the following will work:

from testModule import testMod
testMod.test(4) 

See http://docs.python.org/tutorial/modules.html for more details

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜