Python Modules

Table Of Contents:

  1. What Is A Python Module?
  2. Creating A Python Module.
  3. Importing Python Module.
  4. The dir() Function.
  5. Import Using From Keyword.
  6. Import Module Using *.
  7. Locating Your Module.
  8. Renaming Your Module.

(1) What Is A Python Module?

  • If you want to make package of your code functionality and make it available to others , then you can use the concept of python module.
  • A module can define functions, classes, and variables inside it.
  • It can also include executable code in itself.
  • Grouping related code into a module makes the code easier to understand and use.
  • A Module makes code logically organized.

(2) Creating A Python Module?

  • A python module is simply just a .py file which contains logically related python code.
  • let us create a calculator python file which will do all mathematical operations.
  • Like addition, subtraction, multiplication and division.

Example

Calculator.py

def add(x,y):
    return x+y

def substract(x,y):
    return x-y

def multiply(x,y):
    return x*y

def divide(x,y):
    return x/y

Note:

  • I have created Calculator.py python module and saved it on my local drive.
  • You can call this as Calculator module.

(3) Importing Python Module?

  • As of now we have created a python module called Calculator.py .
  • Now it’s time to use our module.
  • We can use “import” keyword to import a python module.
  • Put your Calculator.py in the same folder where you are running the import statement.

Example

import Calculator

print('Sum:',Calculator.add(10,30))
print('Substract:',Calculator.substract(10,30))
print('Multiply:',Calculator.multiply(10,30))
print('Divide:',Calculator.divide(10,30))
Output:
Sum: 40
Substract: -20
Multiply: 300
Divide: 0.3333333333333333

Note:

  • To call the method inside the Calculator module, we have to use module name dot the method name.
  • Like Calculator.add(10,20) .

(4) The dir() Function?

  • To know what are the methods present inside your module you can use the dir() method.
  • It will list out all the methods and variables inside the module.

Example

import Calculator

dir(Calculator)
Output:
['__builtins__',
 '__cached__',
 '__doc__',
 '__file__',
 '__loader__',
 '__name__',
 '__package__',
 '__spec__',
 'add',
 'divide',
 'multiply',
 'substract']

(5) Import Using From Keyword.

  • If you want to import some specific methods from your module then you can use from statement.
  • from statement will import a specified item from module not all the items.

Example-1

from Calculator import add

print(add(20,40))
Output:
60

Example-2

from Calculator import multiply

print(multiply(20,40))
Output:
800

(6) Import Module Using *

  • If you want to import all the methods present inside the module at a time, then you can use ( * ).
  • It will include all the methods into your file.

Example-1

from Calculator import *

print('Sum:',add(10,30))
print('Substract:',substract(10,30))
print('Multiply:',multiply(10,30))
print('Divide:',divide(10,30))
Output:
Sum: 40
Substract: -20
Multiply: 300
Divide: 0.3333333333333333

Note:

  • Here you can use all the methods present inside Calculator module by there name.
  • I can call the function directly by there name in my code.

(7) Renaming Your Module

  • If you want to give some short name to your module than you can do that.
  • By using “as” keyword you can give some short name to your module which will be handy to use.

Example-1

import Calculator as cl

print('Sum:',cl.add(10,30))
print('Substract:',cl.substract(10,30))
print('Multiply:',cl.multiply(10,30))
print('Divide:',cl.divide(10,30))
Output:
Sum: 40
Substract: -20
Multiply: 300
Divide: 0.3333333333333333

Note:

  • Here I have renamed the Calculator module as cl.
  • I can call the methods inside it using cl.add(10,20).

(8) Locating Your Module

  • When you are importing a module, python interpreter looks for several locations where it may present.
  • First, it searches for the module in the current directory.
  • If the module isn’t found in the current directory, Python then searches each directory in the shell variable PYTHONPATH. The PYTHONPATH is an environment variable, consisting of a list of directories.
  • If that also fails python checks the installation-dependent list of directories configured at the time Python is installed.

Example- Directories List for Modules

# importing sys module
import sys
  
# importing sys.path
print(sys.path)
Output:
['C:\\Users\\SuSahoo\\Blogs', 'C:\\Users\\SuSahoo\\Anaconda3\\python39.zip', 
'C:\\Users\\SuSahoo\\Anaconda3\\DLLs', 'C:\\Users\\SuSahoo\\Anaconda3\\lib',
'C:\\Users\\SuSahoo\\Anaconda3', '', 'C:\\Users\\SuSahoo\\Anaconda3\\lib\\site-packages',
'C:\\Users\\SuSahoo\\Anaconda3\\lib\\site-packages\\win32',
'C:\\Users\\SuSahoo\\Anaconda3\\lib\\site-packages\\win32\\lib',
'C:\\Users\\SuSahoo\\Anaconda3\\lib\\site-packages\\Pythonwin']

Leave a Reply

Your email address will not be published. Required fields are marked *