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.pywhich contains the definition of the functiondisplayText().
#test.py>def displayText():
print( "Geeks 4 Geeks!")We need to call the function
displayText()in any other Python file such that wherever we calldisplayText()function displays text present in it. This can be done using Python modules.
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.
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.
# test.py> # function def displayText(): print( "Geeks 4 Geeks !") |
Now another Python file is created which calls the displayText() function defined in test.py.
# importing all the # functions defined in test.py from test import * # calling functions 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().
# calc.py> # functions def addNumbers(a, b): print("Sum is ", a + b) def subtractNumbers(a, b): print("Differnece 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.
# importing limited functions # defined in calc.py from calc import addNumbers, multiplyNumbers # calling functions 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.
# test.py> # function defined in test.py def displayText(): print("\nGeeks 4 Geeks !") |
# calc.py> # functions defined in calc.py def addNumbers(a, b): print("Sum is ", a + b) def subtractNumbers(a, b): print("Differnece 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.
# file.py> # importing all the functions # defined in calc.py from calc import * # importing required functions # defined in test.py from test import displayText # calling functions defined # in calc.py addNumbers(25, 6) subtractNumbers(25, 6) multiplyNumbers(25, 6) divideNumbers(25, 6) modulusNumbers(25, 6) # calling function defined # in test.py 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 differnt file which is file.py.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Is Python call by reference or call by value
- Python - Call function from another function
- Python program to reverse the content of a file and store it in another file
- How to Call a C function in Python
- Decorator to print Function call details in Python
- call() decorator in Python
- Python: Call Parent class method
- How to call C / C++ from Python?
- Python - Append content of one text file to another
- reStructuredText | .rst file to HTML file using Python for Documentations
- Create a GUI to convert CSV file into excel file using Python
- Python - Get file id of windows file
- MoviePy – Getting Original File Name of Video File Clip
- PYGLET – Opening file using File Location
- PyCairo - Saving SVG Image file to PNG file
- file parameter of Python's print() Function
- Python - Group Sublists by another List
- Python | Insert list in another list
- Python | Update a list of tuples using another list
- Python | Check if a nested list is a subset of another nested list
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

