We need to import the function defined in another Python file from a Python file.
Import Function From Another Python File
def displayText(): print("wholeblogs.com")
We must call the method displayText() in some other Python file so that the text within this file is displayed whenever displayText() is called. Python modules can be used to do this.
- Make a Python file with the necessary functions.
- Make a new Python file and paste the contents of the old Python file into it.
- Invoke the functions from the imported file.
In the following examples, the above strategy was used:
Example 1: The displayText() function is included in the Python file test.py.
# test.py # function def displayText(): print("wholeblogs.com")
First of all create a file that name should be test.py and then make a function as we show you above so, now the test.py Python file is created, which calls the test.py function displayText(). and then you can use the function where you want in the sublime file.
# importing all the # functions defined in test.py from test import * # calling functions displayText()
All of the functions defined in the test.py file are imported into the above program, and then a function is called.
Expample 2: AddNumbers(), subractNumbers(), multiplyNumbers(), divideNumbers(), and modulusNumbers() are all included in the calc.py Python code ().
def addNumbers(a, b): print("Sum is ", a + b) addNumbers(5, 8) def subtractNumbers(a, b): print("Difference is ", a-b) subtractNumbers(10, 5) def multiplyNumbers(a, b): print("Product is ", a * b) multiplyNumbers(2, 10) def divideNumbers(a, b): print("Division is ", a / b) divideNumbers(15, 3) def modulusNumbers(a, b): print("Remainder is ", a % b) modulusNumbers(10, 20)