How do you call a function from a module in python?

How do you call a function from an imported file? for example:

Test:

import test2
def aFunction[]:
    print "hi there"

Test2:

import test
aFunction[]

This give me a name error, saying my function isn't defined. I've also tried:

from test import aFunction

And:

from test import *

I've also tried not importing test2 in test. I'm coming to Python from C++, so I fear I'm missing something blatantly obvious to veteran Python progammers...

asked Sep 27, 2013 at 16:58

3

You are creating a circular import. test.py imports test2.py which tries to import test.py.

Don't do this. By the time test2 imports test, that module has not completed executing all the code; the function is not yet defined:

  • test is compiled and executed, and an empty module object is added to sys.modules.

  • The line import test2 is run.

    • test2 is compiled and executed, and an empty module object is added to sys.modules.

    • The line import test is run.

      • test is already present as a module in sys.modules, this object is returned and bound to the name test.
    • A next line tries to run test.aFunction[]. No such name exists in test. An exception is raised.

  • The lines defining def aFunction[] are never executed, because an exception was raised.

Remove the import test2 line, and run test2.py directly, and importing the function will work fine:

import test

test.aFunction[]

answered Sep 27, 2013 at 17:01

Martijn PietersMartijn Pieters

986k274 gold badges3881 silver badges3239 bronze badges

12

Given a Python file, we need to call a function in it defined in any other Python file. 
Example: 
 

Suppose there is a file test.py which contains the definition of the function displayText[]. 
#test.py>
def displayText[]: 
    print[ “Geeks 4 Geeks!”]
We need to call the function displayText[] in any other Python file such that wherever we call displayText[] function displays text present in it. This can be done using Python modules. 
 

Approach:
 

  1. Create a Python file containing the required functions.
  2. Create another Python file and import the previous Python file into it.
  3. Call the functions defined in the imported file.

The above approach has been used in the below examples:
Example 1: A Python file test.py is created and it contains the displayText[] function.
 

Python3

def displayText[]:

    print[ "Geeks 4 Geeks !"]

Now another Python file is created which calls the displayText[] function defined in test.py.
 

Python3

from test import *

displayText[]

Output:
 

Geeks 4 Geeks!

In the above program, all the functions defined in test.py file is imported then a functions is called.
Example 2: A Python file calc.py is created containing addNumbers[], subractNumbers[], multiplyNumbers[], divideNumbers[] and modulusNumbers[].
 

Python3

def addNumbers[a, b]:

    print["Sum is ", a + b]

def subtractNumbers[a, b]:

    print["Difference is ", a-b]

def multiplyNumbers[a, b]:

    print["Product is ", a * b]

def divideNumbers[a, b]:

    print["Division is ", a / b]

def modulusNumbers[a, b]:

    print["Remainder is ", a % b]

The functions defined in calc.py is called in another Python file.
 

Python3

from calc import addNumbers, multiplyNumbers

addNumbers[2, 5]

multiplyNumbers[5, 4]

Output:
 

7
20

In the above program, all the functions defined in calc.py are not imported. 
To import all the functions defined in a Python file:
Syntax: 
 

from file import *

To import only required functions defined in a Python file:
Syntax:
 

from file import func1, func2, func3

Example 3: 
The below Python files test.py and calc.py are created having various function definitions.
 

Python3

def displayText[]:

    print["\nGeeks 4 Geeks !"]

Python3

def addNumbers[a, b]:

    print["Sum is ", a + b]

def subtractNumbers[a, b]:

    print["Difference is ", a-b]

def multiplyNumbers[a, b]:

    print["Product is ", a * b]

def divideNumbers[a, b]:

    print["Division is ", a / b]

def modulusNumbers[a, b]:

    print["Remainder is ", a % b]

Both files are imported into an another Python file named file.py.
 

Python3

from calc import *

from test import displayText

addNumbers[25, 6]

subtractNumbers[25, 6]

multiplyNumbers[25, 6]

divideNumbers[25, 6]

modulusNumbers[25, 6]

displayText[]

Output:
 

Sum is  31
Difference is  19
Product is  150
Division is  4.166666666666667
Remainder is  1

Geeks 4 Geeks!

In the above program, functions defined in test.py and calc.py are called in a different file which is file.py.
 


How do I use a function from a module in Python?

To make use of the functions in a module, you'll need to import the module with an import statement. An import statement is made up of the import keyword along with the name of the module. In a Python file, this will be declared at the top of the code, under any shebang lines or general comments.

How do you call a function from a module?

[OTHERS = r o]]. You enter the name of the function module as a literal. In the EXPORTING , IMPORTING , CHANGING , and TABLES options, you pass parameters by explicitly assigning the actual parameters to the formal parameters.

How do you call a function from another package in Python?

Approach:.
Create a Python file containing the required functions..
Create another Python file and import the previous Python file into it..
Call the functions defined in the imported file..

How do you call a function in Python program?

Syntax:.
def function_name[]:.
Statement1..
function_name[] # directly call the function..
# calling function using built-in function..
def function_name[]:.
str = function_name['john'] # assign the function to call the function..
print[str] # print the statement..

Chủ Đề