Python Modules

Here, we have learnt about about how to create and import custom python modules and renaming python module. In python, a module is file which....

In this article, we will discuss about Python Modules and also learn about how to create and import custom python modules and renaming python module.

Python Modules

What are Python Modules?

In python, a module is file which contains a set of statements and definitions of python programming language. A Python module is similar to a code library where we can define variables, functions and classes. Simply, we can say that module is a code file in python saved with .py extension such as a file naming test.py is a module and the module name is test. With python modules we can organize the code flexibly and in logical manner. Modules allows us to reuse codes and manage a large program into small organized files.

Create a Python module

In order to create a module in python programming language, we just have to save the code in a file with .py file extension.

Let's create a new Python file named with solutions.py and add the following :

# solutions.py>

# functions

def add(x, y):

    print("Sum of two number is ", x + y)

def subtract(x, y):

    print("Difference between two number is ", x-y)

def multiply(x, y):

    print("Product of two number is ", x * y)

def divide(x, y):

    print("Division by two number is ", x / y)

In above example, we have defined add(), subtract(), multiply() and divide() functions inside a module named with solutions.py

Import Python Module 

In python, to use the functionality of a module, we have to load/import the module in python code. We can import a python module in following ways;

The import statement

With the import statement we can import the functions, variables and classes that are defined in a module into the another module. In a single import statement we can import multiple numbers of modules but we can load a module only once.

Syntax:

import module

When we enter the import statement, the interpreter imports the module into the other module, if that module is in the search path.

# importing  module solutions.py

import solutions

print(solutions.add(5, 3))

Output

8

The from...import statement

In python, we can import specific attributes from a module instead of importing the module as whole into the namespace. It is possible with the from...import statement.

Syntax:

from module_name import name1, name2, name3...

Now let's illustrate from...import statement with following example:

# importing limited functions

# defined in solutions.py

from solutions import add, subtract, multiply

# calling functions

add(2, 10

subtract(5, 4)

multiply(5, 8)

Output

12

1

40

Import all names (from...import* statement)

Python allows us to import all names (definitions) to a current namespace from a module by using the * symbol with the from...import statement.

Syntax:

from module_name import*

Now let's illustrate from...import* statement with following example:

# defined in solutions.py

from solutions import*

# calling functions 

# in solutions.py

add(15, 4)

subtract(5, 1)

multiply(7, 6)

divide(100, 4

Output

The sum of two number is 19

Difference between two number is 4

Product of two number is 42

Division by two number is 25

Renaming Python Module

In python, we can rename a module with a specific name while importing it by using the as keyword. 

Syntax:

import module_name as specific_name

Now let's illustrate renaming python module with following example:

# defined in solutions.py

import solutions as soln;  

x = int(input("Enter value for x:"));  

y = int(input("Enter value for y:"));  

print("Sum of total number is = ",soln.add(x,y))  

Output

Enter value for x:5

Enter value for y:15

Sum of total number is = 20

Locating Python Modules

In python, when we import a module the python interpreter looks in various locations. Firstly, the interpreter checks the built-in module and if it can't find then it checks the directory's list which are defined in the sys.path.  

The python interpreter search in the following order;

• First, it checks the current directory.

• Then it checks other directories in PYTHONPATH.

• And if fails, it checks the installation-dependent directories.

# importing sys module

import sys

# importing sys.path

print(sys.path)

Output

['','C:\\Python33\\Lib\\idlelib','C:\\Windows\\system32\\python34.zip',

'C:\\Python33\\DLLs','C:\\Python33\\lib',

'C:\\Python33','C:\\Python33\\lib\\site-packages']

Reloading Python Modules

As we above learnt that we can load a module only once into the source file of python. 

But if you want to reload a module that python provide us the reload() function. 

Syntax:

reload(module)

The dir() built-in function

To find out the names that are defined by a module, we use the dir() built-in function. It returns a sorted list of variables, classes, functions, sub-modules that are defined in a particular module. 

# Import built-in module  random

import random

print(dir(random))

Output

['BPF', 'LOG4', 'NV_MAGICCONST', 'RECIP_BPF', 'Random', 'SG_MAGICCONST', 'SystemRandom', 'TWOPI', '_Sequence', '_Set', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_accumulate', '_acos', '_bisect', '_ceil', '_cos', '_e', '_exp', '_inst', '_log', '_os', '_pi', '_random', '_repeat', '_sha512', '_sin', '_sqrt', '_test', '_test_generator', '_urandom', '_warn', 'betavariate', 'choice', 'choices', 'expovariate', 'gammavariate', 'gauss', 'getrandbits', 'getstate', 'lognormvariate', 'normalvariate', 'paretovariate', 'randint', 'random', 'randrange', 'sample', 'seed', 'setstate', 'shuffle', 'triangular', 'uniform', 'vonmisesvariate', 'weibullvariate']

Simply, we can say that by using the dir() function we can find out all names that are defined in the present namespace.

Conclusion

From above we have learnt about about how to create and import custom python modules and renaming python module. In python, a module is file which contains a set of statements and definitions of python programming language. To create a module in python programming language, we just have to save the code in a file with .py file extension. In python, to use the functionality of a module, we have to load/import the module in python code.