LAB 6 : Modules in Python
<div class="whitepaper-body-page">
<p>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.</p>
<p>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.</p>
<h4 class="wp-block-heading">Create a Module</h4>
<p>To create a module just save the code you want in a file with the file extension <code>.py</code>.</p>
<p>Save the below code in a file named mymodule.py</p>
<div class="enlighter-default enlighter-v-standard enlighter-t-minimal enlighter-hover enlighter-linenumbers "><div class="enlighter-toolbar-top enlighter-toolbar"><div class="enlighter-btn enlighter-btn-raw" title="Plain text"></div><div class="enlighter-btn enlighter-btn-copy" title="Copy to clipboard"></div><div class="enlighter-btn enlighter-btn-window" title="Open code in new window"></div><div class="enlighter-btn enlighter-btn-website" title="EnlighterJS 3 Syntax Highlighter"></div></div><div class="enlighter" style=""><div class=""><div><span class="enlighter-k0">def</span><span class="enlighter-text"> </span><span class="enlighter-m0">greeting</span><span class="enlighter-g1">()</span><span class="enlighter-text">:</span></div></div><div class=""><div><span class="enlighter-text"> </span><span class="enlighter-m0">print</span><span class="enlighter-g1">(</span><span class="enlighter-s0">'Hello everyone'</span><span class="enlighter-g1">)</span></div></div></div><div class="enlighter-raw">def greeting():
print('Hello everyone')</div><div class="enlighter-toolbar-bottom enlighter-toolbar"></div></div><pre class="EnlighterJSRAW enlighter-origin" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">def greeting():
print('Hello everyone')</pre>
<h4 class="wp-block-heading">Use a Module</h4>
<p>Now we can use the module we just created, by using the <code>import</code> statement:</p>
<div class="enlighter-default enlighter-v-standard enlighter-t-minimal enlighter-hover enlighter-linenumbers "><div class="enlighter-toolbar-top enlighter-toolbar"><div class="enlighter-btn enlighter-btn-raw" title="Plain text"></div><div class="enlighter-btn enlighter-btn-copy" title="Copy to clipboard"></div><div class="enlighter-btn enlighter-btn-window" title="Open code in new window"></div><div class="enlighter-btn enlighter-btn-website" title="EnlighterJS 3 Syntax Highlighter"></div></div><div class="enlighter" style=""><div class=""><div><span class="enlighter-k0">import</span><span class="enlighter-text"> mymodule</span></div></div><div class=""><div><span class="enlighter-text">mymodule.</span><span class="enlighter-m1">greeting</span><span class="enlighter-g1">()</span></div></div></div><div class="enlighter-raw">import mymodule
mymodule.greeting()</div><div class="enlighter-toolbar-bottom enlighter-toolbar"></div></div><pre class="EnlighterJSRAW enlighter-origin" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import mymodule
mymodule.greeting()</pre>
<div class="wp-block-pb-accordion-item c-accordion__item js-accordion-item" data-initially-open="false" data-click-to-close="true" data-auto-close="true" data-scroll="false" data-scroll-offset="0"><button id="at-84363" class="c-accordion__title js-accordion-controller c-accordion__title--button" role="button" tabindex="0" aria-controls="ac-84363" aria-expanded="false">Show Output</button><div id="ac-84363" class="c-accordion__content" hidden="hidden">
<pre class="wp-block-code"><code>Hello Everyone</code></pre>
</div></div>
<h4 class="wp-block-heading">Variables in Module</h4>
<p>The module can contain functions, as already described, but also variables of all types (lists, dictionaries, objects etc)</p>
<div class="enlighter-default enlighter-v-standard enlighter-t-minimal enlighter-hover enlighter-linenumbers "><div class="enlighter-toolbar-top enlighter-toolbar"><div class="enlighter-btn enlighter-btn-raw" title="Plain text"></div><div class="enlighter-btn enlighter-btn-copy" title="Copy to clipboard"></div><div class="enlighter-btn enlighter-btn-window" title="Open code in new window"></div><div class="enlighter-btn enlighter-btn-website" title="EnlighterJS 3 Syntax Highlighter"></div></div><div class="enlighter" style=""><div class=""><div><span class="enlighter-k0">import</span><span class="enlighter-text"> mymodule</span></div></div><div class=""><div><span class="enlighter-text">x=mymodule.employee</span><span class="enlighter-g1">[</span><span class="enlighter-s0">'email'</span><span class="enlighter-g1">]</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text"></span><span class="enlighter-m0">print</span><span class="enlighter-g1">(</span><span class="enlighter-text">x</span><span class="enlighter-g1">)</span></div></div></div><div class="enlighter-raw">import mymodule
x=mymodule.employee['email']
print(x)</div><div class="enlighter-toolbar-bottom enlighter-toolbar"></div></div><pre class="EnlighterJSRAW enlighter-origin" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import mymodule
x=mymodule.employee['email']
print(x)</pre>
<div class="wp-block-pb-accordion-item c-accordion__item js-accordion-item" data-initially-open="false" data-click-to-close="true" data-auto-close="true" data-scroll="false" data-scroll-offset="0"><button id="at-67489" class="c-accordion__title js-accordion-controller c-accordion__title--button" role="button" tabindex="0" aria-controls="ac-67489" aria-expanded="false">Show Output</button><div id="ac-67489" class="c-accordion__content" hidden="hidden">
<pre class="wp-block-code"><code>harry@abc.com</code></pre>
</div></div>
<h4 class="wp-block-heading">Naming a Module</h4>
<p>You can name the module file whatever you like, but it must have the file extension <code>.py</code></p>
<h4 class="wp-block-heading">Re-naming a Module</h4>
<p>You can create an alias when you import a module, by using the <code>as</code> keyword</p>
<div class="enlighter-default enlighter-v-standard enlighter-t-minimal enlighter-hover enlighter-linenumbers "><div class="enlighter-toolbar-top enlighter-toolbar"><div class="enlighter-btn enlighter-btn-raw" title="Plain text"></div><div class="enlighter-btn enlighter-btn-copy" title="Copy to clipboard"></div><div class="enlighter-btn enlighter-btn-window" title="Open code in new window"></div><div class="enlighter-btn enlighter-btn-website" title="EnlighterJS 3 Syntax Highlighter"></div></div><div class="enlighter" style=""><div class=""><div><span class="enlighter-k0">import</span><span class="enlighter-text"> mymodule </span><span class="enlighter-k0">as</span><span class="enlighter-text"> mod</span></div></div><div class=""><div><span class="enlighter-text">mod.</span><span class="enlighter-m1">greeting</span><span class="enlighter-g1">()</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text">x=mod.employee</span><span class="enlighter-g1">[</span><span class="enlighter-s0">'name'</span><span class="enlighter-g1">]</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text"></span><span class="enlighter-m0">print</span><span class="enlighter-g1">(</span><span class="enlighter-text">x</span><span class="enlighter-g1">)</span></div></div></div><div class="enlighter-raw">import mymodule as mod
mod.greeting()
x=mod.employee['name']
print(x)</div><div class="enlighter-toolbar-bottom enlighter-toolbar"></div></div><pre class="EnlighterJSRAW enlighter-origin" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import mymodule as mod
mod.greeting()
x=mod.employee['name']
print(x)</pre>
<div class="wp-block-pb-accordion-item c-accordion__item js-accordion-item" data-initially-open="false" data-click-to-close="true" data-auto-close="true" data-scroll="false" data-scroll-offset="0"><button id="at-22909" class="c-accordion__title js-accordion-controller c-accordion__title--button" role="button" tabindex="0" aria-controls="ac-22909" aria-expanded="false">Show Output</button><div id="ac-22909" class="c-accordion__content" hidden="hidden">
<pre class="wp-block-code"><code>Hello Everyone
Harry</code></pre>
</div></div>
<h4 class="wp-block-heading">from import statement</h4>
<p>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 .</p>
<div class="enlighter-default enlighter-v-standard enlighter-t-minimal enlighter-hover enlighter-linenumbers "><div class="enlighter-toolbar-top enlighter-toolbar"><div class="enlighter-btn enlighter-btn-raw" title="Plain text"></div><div class="enlighter-btn enlighter-btn-copy" title="Copy to clipboard"></div><div class="enlighter-btn enlighter-btn-window" title="Open code in new window"></div><div class="enlighter-btn enlighter-btn-website" title="EnlighterJS 3 Syntax Highlighter"></div></div><div class="enlighter" style=""><div class=""><div><span class="enlighter-k0">from</span><span class="enlighter-text"> mymodule </span><span class="enlighter-k0">import</span><span class="enlighter-text"> greeting</span></div></div><div class=""><div><span class="enlighter-text"></span><span class="enlighter-m0">greeting</span><span class="enlighter-g1">()</span></div></div></div><div class="enlighter-raw">from mymodule import greeting
greeting()</div><div class="enlighter-toolbar-bottom enlighter-toolbar"></div></div><pre class="EnlighterJSRAW enlighter-origin" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">from mymodule import greeting
greeting()</pre>
<div class="wp-block-pb-accordion-item c-accordion__item js-accordion-item" data-initially-open="false" data-click-to-close="true" data-auto-close="true" data-scroll="false" data-scroll-offset="0"><button id="at-22034" class="c-accordion__title js-accordion-controller c-accordion__title--button" role="button" tabindex="0" aria-controls="ac-22034" aria-expanded="false">Show Output</button><div id="ac-22034" class="c-accordion__content" hidden="hidden">
<pre class="wp-block-code"><code>Hello Everyone</code></pre>
</div></div>
<p>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.</p>
<p>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</p>
<div class="enlighter-default enlighter-v-standard enlighter-t-minimal enlighter-hover enlighter-linenumbers "><div class="enlighter-toolbar-top enlighter-toolbar"><div class="enlighter-btn enlighter-btn-raw" title="Plain text"></div><div class="enlighter-btn enlighter-btn-copy" title="Copy to clipboard"></div><div class="enlighter-btn enlighter-btn-window" title="Open code in new window"></div><div class="enlighter-btn enlighter-btn-website" title="EnlighterJS 3 Syntax Highlighter"></div></div><div class="enlighter" style=""><div class=""><div><span class="enlighter-text">f = </span><span class="enlighter-e3"><strong>import</strong></span><span class="enlighter-g1">(</span><span class="enlighter-s0">'new module'</span><span class="enlighter-g1">)</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text">f.</span><span class="enlighter-m1">addition</span><span class="enlighter-g1">(</span><span class="enlighter-n1">10</span><span class="enlighter-text">,</span><span class="enlighter-n1">20</span><span class="enlighter-g1">)</span></div></div></div><div class="enlighter-raw">f = <strong>import</strong>('new module')
f.addition(10,20)</div><div class="enlighter-toolbar-bottom enlighter-toolbar"></div></div><pre class="EnlighterJSRAW enlighter-origin" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">f = <strong>import</strong>('new module')
f.addition(10,20)</pre>
<h4 class="wp-block-heading">Python Built – In modules</h4>
<div class="enlighter-default enlighter-v-standard enlighter-t-minimal enlighter-hover enlighter-linenumbers "><div class="enlighter-toolbar-top enlighter-toolbar"><div class="enlighter-btn enlighter-btn-raw" title="Plain text"></div><div class="enlighter-btn enlighter-btn-copy" title="Copy to clipboard"></div><div class="enlighter-btn enlighter-btn-window" title="Open code in new window"></div><div class="enlighter-btn enlighter-btn-website" title="EnlighterJS 3 Syntax Highlighter"></div></div><div class="enlighter" style=""><div class=""><div><span class="enlighter-k0">import</span><span class="enlighter-text"> datetime</span></div></div><div class=""><div><span class="enlighter-text">current=datetime.datetime.</span><span class="enlighter-m1">now</span><span class="enlighter-g1">()</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text"></span><span class="enlighter-m0">print</span><span class="enlighter-g1">(</span><span class="enlighter-text">current.</span><span class="enlighter-m1">strftime</span><span class="enlighter-g1">(</span><span class="enlighter-s0">'%Y-%m-%d %H:%M:%S'</span><span class="enlighter-g1">))</span></div></div></div><div class="enlighter-raw">import datetime
current=datetime.datetime.now()
print(current.strftime('%Y-%m-%d %H:%M:%S'))</div><div class="enlighter-toolbar-bottom enlighter-toolbar"></div></div><pre class="EnlighterJSRAW enlighter-origin" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import datetime
current=datetime.datetime.now()
print(current.strftime('%Y-%m-%d %H:%M:%S'))</pre>
<div class="wp-block-pb-accordion-item c-accordion__item js-accordion-item" data-initially-open="false" data-click-to-close="true" data-auto-close="true" data-scroll="false" data-scroll-offset="0"><button id="at-21475" class="c-accordion__title js-accordion-controller c-accordion__title--button" role="button" tabindex="0" aria-controls="ac-21475" aria-expanded="false">Sow Output</button><div id="ac-21475" class="c-accordion__content" hidden="hidden">
<pre class="wp-block-code"><code>2020-07-24 00:19:53</code></pre>
</div></div>
<div class="enlighter-default enlighter-v-standard enlighter-t-minimal enlighter-hover enlighter-linenumbers "><div class="enlighter-toolbar-top enlighter-toolbar"><div class="enlighter-btn enlighter-btn-raw" title="Plain text"></div><div class="enlighter-btn enlighter-btn-copy" title="Copy to clipboard"></div><div class="enlighter-btn enlighter-btn-window" title="Open code in new window"></div><div class="enlighter-btn enlighter-btn-website" title="EnlighterJS 3 Syntax Highlighter"></div></div><div class="enlighter" style=""><div class=""><div><span class="enlighter-k0">import</span><span class="enlighter-text"> calendar</span></div></div><div class=""><div><span class="enlighter-text"></span><span class="enlighter-m0">print</span><span class="enlighter-g1">(</span><span class="enlighter-text">calendar.</span><span class="enlighter-m1">month</span><span class="enlighter-g1">(</span><span class="enlighter-n1">1987</span><span class="enlighter-text">,</span><span class="enlighter-n1">6</span><span class="enlighter-g1">))</span></div></div></div><div class="enlighter-raw">import calendar
print(calendar.month(1987,6))</div><div class="enlighter-toolbar-bottom enlighter-toolbar"></div></div><pre class="EnlighterJSRAW enlighter-origin" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import calendar
print(calendar.month(1987,6))</pre>
<pre class="wp-block-code"><code> 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</code></pre>
<div class="enlighter-default enlighter-v-standard enlighter-t-minimal enlighter-hover enlighter-linenumbers "><div class="enlighter-toolbar-top enlighter-toolbar"><div class="enlighter-btn enlighter-btn-raw" title="Plain text"></div><div class="enlighter-btn enlighter-btn-copy" title="Copy to clipboard"></div><div class="enlighter-btn enlighter-btn-window" title="Open code in new window"></div><div class="enlighter-btn enlighter-btn-website" title="EnlighterJS 3 Syntax Highlighter"></div></div><div class="enlighter" style=""><div class=""><div><span class="enlighter-k0">import</span><span class="enlighter-text"> platform</span></div></div><div class=""><div><span class="enlighter-text"></span><span class="enlighter-m0">print</span><span class="enlighter-g1">(</span><span class="enlighter-text">platform.</span><span class="enlighter-m1">system</span><span class="enlighter-g1">())</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text"></span><span class="enlighter-m0">print</span><span class="enlighter-g1">(</span><span class="enlighter-text">platform.</span><span class="enlighter-m1">release</span><span class="enlighter-g1">())</span></div></div></div><div class="enlighter-raw">import platform
print(platform.system())
print(platform.release())</div><div class="enlighter-toolbar-bottom enlighter-toolbar"></div></div><pre class="EnlighterJSRAW enlighter-origin" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import platform
print(platform.system())
print(platform.release())</pre>
<div class="wp-block-pb-accordion-item c-accordion__item js-accordion-item" data-initially-open="false" data-click-to-close="true" data-auto-close="true" data-scroll="false" data-scroll-offset="0"><button id="at-65891" class="c-accordion__title js-accordion-controller c-accordion__title--button" role="button" tabindex="0" aria-controls="ac-65891" aria-expanded="false">Show Output</button><div id="ac-65891" class="c-accordion__content" hidden="hidden">
<pre class="wp-block-code"><code>Windows
10</code></pre>
</div></div>
<h4 class="wp-block-heading">Using the dir() Function</h4>
<p>There is a built-in function to list all the function names (or variable names) in a module. The <code>dir()</code> function:</p>
<p>Below example List all the defined names belonging to the platform module</p>
<div class="enlighter-default enlighter-v-standard enlighter-t-minimal enlighter-hover enlighter-linenumbers "><div class="enlighter-toolbar-top enlighter-toolbar"><div class="enlighter-btn enlighter-btn-raw" title="Plain text"></div><div class="enlighter-btn enlighter-btn-copy" title="Copy to clipboard"></div><div class="enlighter-btn enlighter-btn-window" title="Open code in new window"></div><div class="enlighter-btn enlighter-btn-website" title="EnlighterJS 3 Syntax Highlighter"></div></div><div class="enlighter" style=""><div class=""><div><span class="enlighter-k0">import</span><span class="enlighter-text"> platform</span></div></div><div class=""><div><span class="enlighter-text"></span><span class="enlighter-m0">dir</span><span class="enlighter-g1">(</span><span class="enlighter-text">platform</span><span class="enlighter-g1">)</span></div></div></div><div class="enlighter-raw">import platform
dir(platform)</div><div class="enlighter-toolbar-bottom enlighter-toolbar"></div></div><pre class="EnlighterJSRAW enlighter-origin" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import platform
dir(platform)</pre>
<div class="wp-block-pb-accordion-item c-accordion__item js-accordion-item" data-initially-open="false" data-click-to-close="true" data-auto-close="true" data-scroll="false" data-scroll-offset="0"><button id="at-108" class="c-accordion__title js-accordion-controller c-accordion__title--button" role="button" tabindex="0" aria-controls="ac-108" aria-expanded="false">Show Output</button><div id="ac-108" class="c-accordion__content" hidden="hidden">
<pre class="wp-block-code"><code>['DEV_NULL', '_UNIXCONFDIR', '_WIN32_CLIENT_RELEASES', '_WIN32_SERVER_RELEASES', '<strong>builtins</strong>', '<strong>cached</strong>', '<strong>copyright</strong>', '<strong>doc</strong>', '<strong>file</strong>', '<strong>loader</strong>', '<strong>name</strong>', '<strong>package</strong>', '<strong>spec</strong>', '<strong>version</strong>', '_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']</code></pre>
</div></div>
<p></div></p>