Pablo
Pablo

Reputation: 45

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?

Upvotes: 1

Views: 52

Answers (2)

baldr
baldr

Reputation: 2999

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)

Upvotes: 1

DeepSpace
DeepSpace

Reputation: 81664

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).

Upvotes: 3

Related Questions