Home »
Python »
Python Programs
Importing Python Modules (Different Methods & Examples)
Learn how to import Python modules in your program using different methods.
By Shivang Yadav Last updated : January 13, 2024
Programming languages allows programmers to keep their codes in different files and then use them in a program by importing them.
If you are from C / C++ background, you might be familiar with #include statements. These are import statements that import libraries consisting of methods that can be used in a program.
Importing Python Modules
Similarly, Python provides import statement for importing modules in Python. These can be used to import methods in the current program for usage.
Python allows you to import files in different ways,
- Import a module or file
- Import specific methods of a module
- Import all methods of a module (using *)
1. Import a module or file
A python module can be fully imported to a program for usage using import statements. To access the method of this imported file we need to use the dot "."
Syntax to import a module or a file
import module_name
# or
import py_file_name
Python program to import a module (or, user-defined module) or a file
Files to be imported
File: mycheck.py
def iseven(n):
ans=False
if n%2==0:
ans=True
return ans
def isodd(n):
ans=False
if n%2==1:
ans=True
return ans
def isprime(n):
ans=False
c=0
for i in range(1,n+1):
if n%i==0:
c=c+1
if c==2:
ans=True
return ans
def ispalindrome(n):
ans=False
m=n
rev=0
while n>0:
dig=n%10
rev = rev*10+dig
n=n//10
if rev==m:
ans=True
return ans
File: mymath.py
def sum(a,b):
c=a+b
return c
def difference(a,b):
c=a-b
return c
def product(a,b):
c=a*b
return c
def quotient(a,b):
c=a/b
return c
def remainder(a,b):
c=a%b
return c
Main file:
import os
import mymath
import mycheck
def main():
ans=True
while ans:
os.system('cls')
print("MENU")
print("--------------------------------------")
print("1.Add")
print("2.Substract")
print("3.Multiply")
print("4.Divide")
print("5.Even Check")
print("6.Odd Check")
print("7.Prime Check")
print("8.Palindrome Check")
print("9.Exit")
print("-------------------------------------")
ch=int(input("Enter choice(1-9):"))
print("-------------------------------------")
if ch==1:
a = int(input("Enter A: "))
b = int(input("Enter B: "))
c = mymath.sum(a,b)
print("Sum :",c)
elif ch==2:
a = int(input("Enter A: "))
b = int(input("Enter B: "))
c = mymath.difference(a,b)
print("difference :",c)
elif ch==3:
a = int(input("Enter A: "))
b = int(input("Enter B: "))
c = mymath.product(a,b)
print("Product :",c)
elif ch==4:
a = int(input("Enter A: "))
b = int(input("Enter B: "))
c = mymath.quotient(a,b)
print("Quotient :",c)
elif ch==5:
n = int(input("Enter N: "))
if mycheck.iseven(n)==True:
print(n,"is Even")
else:
print(n,"is Not Even")
elif ch==6:
n = int(input("Enter N: "))
if mycheck.isodd(n)==True:
print(n,"is Odd")
else:
print(n,"is Not Odd")
elif ch==7:
n = int(input("Enter N: "))
if mycheck.isprime(n)==True:
print(n,"is Prime")
else:
print(n,"is Not Prime")
elif ch==8:
n = int(input("Enter N: "))
if mycheck.ispalindrome(n)==True:
print(n,"is Palindrome")
else:
print(n,"is Not Palindrome")
elif ch==9:
ans=False
print("-------------------------------------")
input("Press any key.....")
if __name__=="__main__":main()
Output
MENU
--------------------------------------
1.Add
2.Substract
3.Multiply
4.Divide
5.Even Check
6.Odd Check
7.Prime Check
8.Palindrome Check
9.Exit
-------------------------------------
Enter choice(1-9):1
-------------------------------------
Enter A: 43
Enter B: 65
Sum : 108
-------------------------------------
Press any key.....
MENU
--------------------------------------
1.Add
2.Substract
3.Multiply
4.Divide
5.Even Check
6.Odd Check
7.Prime Check
8.Palindrome Check
9.Exit
-------------------------------------
Enter choice(1-9):2
-------------------------------------
Enter A: 899
Enter B: 54
difference : 845
-------------------------------------
Press any key.....
MENU
--------------------------------------
1.Add
2.Substract
3.Multiply
4.Divide
5.Even Check
6.Odd Check
7.Prime Check
8.Palindrome Check
9.Exit
-------------------------------------
Enter choice(1-9):3
-------------------------------------
Enter A: 3
Enter B: 7
Product : 21
-------------------------------------
Press any key.....
MENU
--------------------------------------
1.Add
2.Substract
3.Multiply
4.Divide
5.Even Check
6.Odd Check
7.Prime Check
8.Palindrome Check
9.Exit
-------------------------------------
Enter choice(1-9):4
-------------------------------------
Enter A: 7
Enter B: 2
Quotient : 3.5
-------------------------------------
Press any key.....
MENU
--------------------------------------
1.Add
2.Substract
3.Multiply
4.Divide
5.Even Check
6.Odd Check
7.Prime Check
8.Palindrome Check
9.Exit
-------------------------------------
Enter choice(1-9):5
-------------------------------------
Enter N: 6
6 is Even
-------------------------------------
Press any key.....
MENU
--------------------------------------
1.Add
2.Substract
3.Multiply
4.Divide
5.Even Check
6.Odd Check
7.Prime Check
8.Palindrome Check
9.Exit
-------------------------------------
Enter choice(1-9):6
-------------------------------------
Enter N: 7645
7645 is Odd
-------------------------------------
Press any key.....
MENU
--------------------------------------
1.Add
2.Substract
3.Multiply
4.Divide
5.Even Check
6.Odd Check
7.Prime Check
8.Palindrome Check
9.Exit
-------------------------------------
Enter choice(1-9):7
-------------------------------------
Enter N: 654
654 is Not Prime
-------------------------------------
Press any key.....
MENU
--------------------------------------
1.Add
2.Substract
3.Multiply
4.Divide
5.Even Check
6.Odd Check
7.Prime Check
8.Palindrome Check
9.Exit
-------------------------------------
Enter choice(1-9):8
-------------------------------------
Enter N: 54364
54364 is Not Palindrome
-------------------------------------
Press any key.....
MENU
--------------------------------------
1.Add
2.Substract
3.Multiply
4.Divide
5.Even Check
6.Odd Check
7.Prime Check
8.Palindrome Check
9.Exit
-------------------------------------
Enter choice(1-9):9
-------------------------------------
-------------------------------------
Press any key.....
2. Import specific methods of a module
You can alternately import the function of the module in case you need only some specific function and loading the rest might slow down the application. This is done by using the following statement:
Syntax to import specific methods of a module
from fileName import function(s)
Python program to import files using from-import statement, importing specific functions only
from os import system
from mymath import sum,difference
from mycheck import iseven,isprime,ispalindrome
def main():
ans=True
while ans:
system('cls')
print("MENU")
print("--------------------------------------")
print("1.Add")
print("2.Substract")
print("3.Even Check")
print("4.Prime Check")
print("5.Palindrome Check")
print("6.Exit")
print("-------------------------------------")
ch=int(input("Enter choice(1-6):"))
print("-------------------------------------")
if ch==1:
a = int(input("Enter A: "))
b = int(input("Enter B: "))
c = sum(a,b)
print("Sum :",c)
elif ch==2:
a = int(input("Enter A: "))
b = int(input("Enter B: "))
c = difference(a,b)
print("difference :",c)
elif ch==3:
n = int(input("Enter N: "))
if iseven(n)==True:
print(n,"is Even")
else:
print(n,"is Not Even")
elif ch==4:
n = int(input("Enter N: "))
if isprime(n)==True:
print(n,"is Prime")
else:
print(n,"is Not Prime")
elif ch==5:
n = int(input("Enter N: "))
if ispalindrome(n)==True:
print(n,"is Palindrome")
else:
print(n,"is Not Palindrome")
elif ch==6:
ans=False
print("-------------------------------------")
input("Press any key.....")
if __name__=="__main__":main()
Output
Run as the above program
3. Import all methods of a module (using *)
You can select all functions also using the above method by using ' * ' instead of the function names. Importing like this will eliminate the usage of dot while calling, you can call the function directly.
Syntax to import all methods of a module (using *)
import module_name
# or
from module import *
# or
from py_file_name import *
Python program to import using from - import statement using *
import os
from mymath import *
from mycheck import *
def main():
ans=True
while ans:
os.system('cls')
print("MENU")
print("--------------------------------------")
print("1.Add")
print("2.Substract")
print("3.Multiply")
print("4.Divide")
print("5.Even Check")
print("6.Odd Check")
print("7.Prime Check")
print("8.Palindrome Check")
print("9.Exit")
print("-------------------------------------")
ch=int(input("Enter choice(1-9):"))
print("-------------------------------------")
if ch==1:
a = int(input("Enter A: "))
b = int(input("Enter B: "))
c = sum(a,b)
print("Sum :",c)
elif ch==2:
a = int(input("Enter A: "))
b = int(input("Enter B: "))
c = difference(a,b)
print("difference :",c)
elif ch==3:
a = int(input("Enter A: "))
b = int(input("Enter B: "))
c = product(a,b)
print("Product :",c)
elif ch==4:
a = int(input("Enter A: "))
b = int(input("Enter B: "))
c = quotient(a,b)
print("Quotient :",c)
elif ch==5:
n = int(input("Enter N: "))
if iseven(n)==True:
print(n,"is Even")
else:
print(n,"is Not Even")
elif ch==6:
n = int(input("Enter N: "))
if isodd(n)==True:
print(n,"is Odd")
else:
print(n,"is Not Odd")
elif ch==7:
n = int(input("Enter N: "))
if isprime(n)==True:
print(n,"is Prime")
else:
print(n,"is Not Prime")
elif ch==8:
n = int(input("Enter N: "))
if ispalindrome(n)==True:
print(n,"is Palindrome")
else:
print(n,"is Not Palindrome")
elif ch==9:
ans=False
print("-------------------------------------")
input("Press any key.....")
if __name__=="__main__":main()
Output
RUN as the above program
To understand the above programs, you should have the basic knowledge of the following Python topics:
Python Basic Programs »