简体   繁体   中英

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 :

/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 ??

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM