Calling Function of Custom Python File in Python

Calling Function of custom Python (.py) file in Python programming. Functions are used to divide a large program into sub-programs for easy control. All these sub-programs are called sub-functions of the program.

Program Adding .py File and Calling his Functions

First of all goto left side Project pane and right click on MyProgram and goto New -> and select Python File.
In New Python file dialog in Name: write MyFile and click on OK button.
Write the following code in MyFile.py file.

Program

def fun(): #fun()' is name of Function.
    print("This is Simple Function in MyFile")
def fun2(): #'fun2()' is name of Function.
    print("This is Another Simple Function in MyFile")

Write the following code in main program MyProgram.py file.

Program

import MyFile #importing 'MyFile' File.
MyFile.fun() #calling 'fun()' function of 'MyFile' File.
MyFile.fun2() #calling 'fun2()' function of 'MyFile' File.
Output
This is Simple Function in MyFile
This is Another Simple Function in MyFile

Program Adding .py Files and Calling his Functions

Write the following code in main program MyFile.py file.

Program

def fun(): #fun()' is name of Function.
    print("This is Simple Function in MyFile")
def fun2(): #'fun2()' is name of Function.
    print("This is Another Simple Function in MyFile")

Write the following code in main program MyFile2.py file.

Program

def fun3(): #fun3()' is name of Function.
    print("This is Simple Function in MyFile2")
def fun4(): #'fun4()' is name of Function.
    print("This is Another Simple Function in MyFile2")

Write the following code in main program MyProgram.py file.

Program

import MyFile #importing 'MyFile' File.
import MyFile2 #importing 'MyFile2' File.
MyFile.fun() #calling 'fun()' function of 'MyFile' File.
MyFile.fun2() #calling 'fun2()' function of 'MyFile' File.
MyFile2.fun3() #calling 'fun3()' function of 'MyFile2' File.
MyFile2.fun4() #calling 'fun4()' function of 'MyFile2' File.
Output
This is Simple Function in MyFile
This is Another Simple Function in MyFile
This is Simple Function in MyFile2
This is Another Simple Function in MyFile2

Program Adding .py Files and Calling his Functions Another Method

Write the following code in MyFile.py file.

Program

def fun(): #fun()' is name of Function.
    print("This is Simple Function in MyFile")
def fun2(): #'fun2()' is name of Function.
    print("This is Another Simple Function in MyFile")

Write the following code in MyFile2.py file.

Program

def fun(): #fun()' is name of Function.
    print("This is Simple Function in MyFile2")
def fun2(): #'fun2()' is name of Function.
    print("This is Another Simple Function in MyFile2")

Write the following code in MyProgram.py file.

Program

import MyFile #importing 'MyFile' File.
import MyFile2 #importing 'MyFile2' File.
MyFile.fun() #calling 'fun()' function of 'MyFile' File.
MyFile.fun2() #calling 'fun2()' function of 'MyFile' File.
MyFile2.fun() #calling 'fun()' function of 'MyFile2' File.
MyFile2.fun2() #calling 'fun2()' function of 'MyFile2' File.
Output
This is Simple Function in MyFile
This is Another Simple Function in MyFile
This is Simple Function in MyFile2
This is Another Simple Function in MyFile2

Program Calling .py Files Functions from One Another

Write the following code in MyFile.py file.

Program

import MyFile2 #importing 'MyFile2' File.
def fun(): #fun()' is name of Function.
    print("This is Simple Function in MyFile")
    MyFile2.fun2() #calling 'fun2()' function of 'MyFile2' File.

Write the following code in MyFile2.py file.

Program

def fun2(): #'fun2()' is name of Function.
    print("This is Simple Function in MyFile2")
    print("After Calling Functions")

Write the following code in MyProgram.py file.

Program

import MyFile #importing 'MyFile' File.
print("Before Calling Functions")
MyFile.fun() #calling 'fun()' function of 'MyFile' File.
Output
Before Calling Functions
This is Simple Function in MyFile
This is Simple Function in MyFile2
After Calling Functions