Python

LAB 6 : Modules in Python

Python

LAB 6 : Modules in Python

Modules refer to a file containing Python statements and definitions. A file containing Python code, for e.g.: example.py, is called a module and its module name would be example. We use modules to break down large programs into small manageable and organized files. Furthermore, modules provide reusability of code.

We can reuse code in our program by defining functions. Similarly, we can use number of function from program to another using modules. There are various built-in modules in python like sys, time, date etc.

Create a Module

To create a module just save the code you want in a file with the file extension .py.

Save the below code in a file named mymodule.py

def greeting():
    print('Hello everyone')

Use a Module

Now we can use the module we just created, by using the import statement:

import  mymodule
mymodule.greeting()
Hello Everyone

Variables in Module

The module can contain functions, as already described, but also variables of all types (lists, dictionaries, objects etc)

import mymodule
x=mymodule.employee['email']
print(x)
harry@abc.com

Naming a Module

You can name the module file whatever you like, but it must have the file extension .py

Re-naming a Module

You can create an alias when you import a module, by using the as keyword

import mymodule as mod
mod.greeting()
x=mod.employee['name']
print(x)
Hello Everyone
Harry

from import statement

Consider our mymodule.py file contains more than 50 function out of which we are only going to use one or two.Then in such situation from import statement can be used.You do not need to call the function using the module name if you are using from import statement .

from mymodule import greeting
greeting()
Hello Everyone

Below syntax should be used to import modules which have space in the name.You cannot import module with sapce in name using the above syntax.

Consider we have a file named new module.py (there is space in between new and module).If you try to import this module in normal way it will give error you will have to import it as shown below.We are import an addition() frunction from new module.py

f = __import__('new module')
f.addition(10,20)

Python Built – In modules

import datetime
current=datetime.datetime.now()
print(current.strftime('%Y-%m-%d %H:%M:%S'))
2020-07-24 00:19:53
import calendar
print(calendar.month(1987,6))
     June 1987
Mo Tu We Th Fr Sa Su
 1  2  3  4  5  6  7
 8  9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
import platform
print(platform.system())
print(platform.release())
Windows
10

Using the dir() Function

There is a built-in function to list all the function names (or variable names) in a module. The dir() function:

Below example List all the defined names belonging to the platform module

import platform
dir(platform)
['DEV_NULL', '_UNIXCONFDIR', '_WIN32_CLIENT_RELEASES', '_WIN32_SERVER_RELEASES', '__builtins__', '__cached__', '__copyright__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '__version__', '_default_architecture', '_dist_try_harder', '_follow_symlinks', '_ironpython26_sys_version_parser', '_ironpython_sys_version_parser', '_java_getprop', '_libc_search', '_linux_distribution', '_lsb_release_version', '_mac_ver_xml', '_node', '_norm_version', '_parse_release_file', '_platform', '_platform_cache', '_pypy_sys_version_parser', '_release_filename', '_release_version', '_supported_dists', '_sys_version', '_sys_version_cache', '_sys_version_parser', '_syscmd_file', '_syscmd_uname', '_syscmd_ver', '_uname_cache', '_ver_output', 'architecture', 'collections', 'dist', 'java_ver', 'libc_ver', 'linux_distribution', 'mac_ver', 'machine', 'node', 'os', 'platform', 'popen', 'processor', 'python_branch', 'python_build', 'python_compiler', 'python_implementation', 'python_revision', 'python_version', 'python_version_tuple', 're', 'release', 'subprocess', 'sys', 'system', 'system_alias', 'uname', 'uname_result', 'version', 'warnings', 'win32_ver']