Skip to content

Latest commit

History

History
251 lines (204 loc) · 6.22 KB

File metadata and controls

251 lines (204 loc) · 6.22 KB

Modules

  • A module is a Python file containing python definitions statements.
  • A module can define functions, classes, and variables, Methods also contain runnable code.
  • Grouping similar type of code into a file and whenever it required to use it

some python inbuilt modules

1.os
2.string
3.threading
4.sys
5.collection
6.argparse
7.sqlit3
8.raw
9.re
10.pip
.... and much more

How to Get List of Installed modules list in python?

you can write down following code into an interactive shell or in the python file

help('modules')

you will get the result

Please wait a moment while I gather a list of all available modules...

Output

FileSearchEngine _testcapi ftplib reprlib
GetFiles _testcapi_d functools requests
PIL _testconsole gc rlcompleter
PyQt5 _testconsole_d genericpath runpy
SqliteHandler _testimportmultiple getopt sched
Test _testimportmultiple_d getpass secrets
__future__ _testmultiphase gettext select
_abc _testmultiphase_d glob select_d
_ast _thread gzip selectors
_asyncio _threading_local hashlib selenium
_asyncio_d _tkinter heapq setuptools
_bisect _tkinter_d hmac shelve
_blake2 _tracemalloc html shlex
_bootlocale _warnings html5lib shutil
_bz2 _weakref http signal
........... ................ ........... ....

How To import module in python program?

We can use any Python source file as a module by executing an import statement in some other Python source file.

  • You can import the module using import keywords

Syntax:

import<moduleName>

Example:

#import required moduleimportplatform

How to rename imported modules?

  • as keyword is used to bind a new name to an object.
  • If you import a module directly and then import the same module but with a different name via as both names will point to the same object.

Syntax:

import<moduleName>as<newObjectName>

Example

#Normal way to import a moduleimportplatform#using as Keyword import a moduleimportplatformasplt#if you check the id of object #platform and plt both will point to the same objectprint(id(platform))
print(id(plt))

Output:

139950267344280
139950267344280

How to list all functions in a Python module?

you can use the following two different functions to achieve function names, variables, and documentation

1.help()
2.dir()

1.help()

  • help() function is used to get the all function with docuemantation

Syntax:

help(moduleName)

Example:

#import moduleimportplatform#get documentationprint(help(platform))

Output:

Help on module platform:
NAME
platform
MODULE REFERENCE
https://docs.python.org/3.6/library/platform
DESCRIPTION
This module tries to retrieve as much platform-identifying data as possible.
It makes this information available via function APIs.
CLASSES
builtins.tuple(builtins.object)
uname_result
class uname_result(builtins.tuple)
| uname_result(system, node, release, version, machine, processor)
FUNCTIONS
architecture(executable='/usr/local/bin/python3', bits='', linkage='')
Queries the given executable (defaults to the Python interpreter
binary) for various architecture information.
Returns a tuple (bits, linkage) which contains information about
the bit architecture and the linkage format used for the
executable
DATA
DEV_NULL = '/dev/null'
__copyright__ = '\n Copyright (c) 1999-2000, Marc-Andre Lemburg.
VERSION
1.0.8
FILE
/usr/lib/python3.6/platform.py
None

2.dir()

  • To simply list the names of all the functions and variables defined in the module.

Syntax:

dir(moduleName)

Example:

#import moduleimportplatform#get function names and variable nameprint(dir(platform))

Output

['DEV_NULL', '_UNIXCONFDIR', '_WIN32_CLIENT_RELEASES', '_WIN32_SERVER_RELEASES', '__builtins__', '__cached__',
'__copyright__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '__version__',
'_codename_file_re','_default_architecture', '_dist_try_harder',
'_distributor_id_file_re', '_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_file_re', '_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']

How to use the function, methods of modules?

  • if you want to check the python version from platform module there is one method called python_version()

Syntax:

moduleName.<MethodName>()

Example:

#import moduleimportplatform#Call python_version() from platformversion=platform.python_version()
print(version)

Output:

3.7.3

How to import a specific method or function name from Module?

Syntax:

from<moduleName>importmethodName,methodName2

Example import only python_version()

#import required modulefromplatformimportpython_version#get python versionversion=python_version()
print(version)

Output:

3.7.3