简体   繁体   中英

Error when I try to import a module

This question may be trivial but I don't really get it. I have two python modules.

This is module1 :

import module2

def main():
    print funcion2(2,3)

if __name__ == '__main__':
    main()

This is module2 :

def funcion2(a, b):
    return a + b

I get an error (function2 wasn't found). If write " from module2 import * " it works fine. Why?

If you want to import module2 you will need to call function2 this way: module2.funcion2(2,3) .

You usually want to avoid from <module> import * so either do as above or from module2 import function2 and then you can simply call function2(2, 3) .

Use:

import module2

module2.funcion2(2, 3)

You import a module and should explicitly specify it while calling a method.

You can also import only this function:

from module2 import funcion2

funcion2(2, 3)

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