- Create a .py file. In this example, we will use name example.py
- Write python code like functions or classes as in example below and save it.
- Now whenever, you need to perform addition of two numbers, simply import this module.
# Python Module example
def add(a, b):
“””This program adds two numbers and return the result”””
result = a + b return result
How to import modules in Python?
We can import the definitions inside a module to another module or the interactive interpreter in Python. We use the import keyword to do this. To import our previously defined module example we type the following in the Python prompt.
import example
Using the module names we can access the function using the dot . operator.
example.add(4, 5.5)
9.5
Standard modules of python can be imported the same way as we import user-defined modules.
Few more exmaples of ways to import and using modules:
- Python import statement
import math
print(“The value of pi is”, math.pi)
- Import with renaming
print(“The value of pi is”, m.pi)
- Python from…import statement
print(“The value of pi is”, pi)
- Import all names
print(“The value of pi is”, pi)
Python Module Search Path:
- While importing a module, Python looks at several places.
- Interpreter first looks for a built-in module then (if not found) into a list of directories defined in sys.path.
- The search is in this order.
- The current directory.
- PYTHONPATH (an environment variable with a list of directory).
- The installation-dependent default directory.
- We can add or modify this list to add our own path.
>>> import sys
>>> sys.path
[’’,
‘C:\\Python33\\Lib\\idlelib’,
‘C:\\Windows\\system32\\python33.zip’,
‘C:\\Python33\\DLLs’,
‘C:\\Python33\\lib’,
‘C:\\Python33’,
‘C:\\Python33\\lib\\site-packages’]
Reloading a module:
- The Python interpreter imports a module only once during a session.
- Now if our module changed during the course of the program, we would have to reload it.
- One way to do this is to restart the interpreter. But this does not help much.
- Python provides a neat way of doing this. We can use the reload() function inside the imp module to reload a module.
>>> import my_module
>>> imp.reload(my_module)
Python Package:
- We usually store all of our files in computer in well organised hierarchy of directories for easier access.
- Similar files are kept in the same directory, for example, we may keep all the songs in one directory.
- Similar to this, Python has packages for directories and modules for files.
- As program grows larger in size with a lot of modules, we place similar modules in one package and different modules in different packages.
- Similar, to that of directory which contains sub-directories and files, a Python package can have sub-packages and modules.
- A directory must contain a file named __init__.py in order for Python to consider it as a package.
- This file can be left empty but we generally place the initialization code for that package in this file.
Here is an example. Suppose we are developing a game, one possible organisation of packages and modules could be as shown in the figure below.