Subscribe to our Youtube Channel To Learn Free Python Course and More
Modules and Packages
Modules
What is Module?
Modules are simple files with .py extensions. When you do programming, most of the time your code base is so large that it’s not easy to maintain that in the one file. So you divide your code base into smaller chunks and you can keep those smaller chunks of code into the different file. These files are called modules.
Modules also provide reusability of code. If you find that functions which are more frequently used you can write that into one module and use that module where ever you need.
To create a module, just create a file with .py extension. Here we will create a module name ‘greet.py’. See the below code.
# greet.py module
def hello(name):
print(f'Hello {name}')
def say_morning(name):
print(f'Good Morning {name}')
Here we create the one module name ‘greet’ and there are two functions in it. Let’s see how you can get these functions into another module.
Import Module in Python
Import statement
The import keyword is used to get the function definitions into another module. To use the functions we use dot(.) operator. We will import the module ‘greet’ that we just created.
# Import module
import greet
greet.hello('Ramesh')
# Output: Hello Ramesh
greet.say_morning('Kumar')
# Output: Good Morning Kumar
from … import … Statement
If you want to import some specific object you can use from … import statement. See the belo code.
from greet import hello
hello('Kumar')
# Output: Hello Kumar
from … import *
If you want to import all the objects of the module then you can use below syntax.
form greet import *
hello('Kumar')
# Output: Hello Kumar
say_morning('Kumar')
# Output: Good Morning Kumar
Module Search Path
import greet
When Python interpreter encounters import statement, it searches for that particular module in built-in modules that are already in Python, if it’s not found then looks in the current directory in which the module is present. In the last, it searches in the installation-dependent directories configured at the time Python is installed.
Note: To make sure that the module is found in the script that you are writing, keep module and script in the same directory.
Packages
Packages are the directories containing multiple modules and sub-packages. In computer, to organize your files you put them in folders, so you can easily access them. You make an organized hierarchy of directories to maintain the files.
In the same way, packages are helpful in organizing your modules and scripts. When you are working on a large application, It contains lots of modules. So it is easy to put similar modules in one package and different module in a different package.
Note: To initialize a package, it should contain a file called init.py in order for Python to understand it as a package. This file can be left empty but it generally it contains initialization code. But in Python version 3.3, it is not necessary for a package to have init.py file. it can still be present if package initialization is needed. But it is no longer required.
You can use the dot(.) operator to access function inside the sub-packages and modules inside them.
# Packages
import pkg
from pkg import sub_pkg_a.mod_a1
from pkg.sub_pkg_b import mod_b1, mod_b2
Top comments (0)