Python Lab 6 : Modules in Python

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.:&nbsp;example.py, is called a module and its module name would be&nbsp;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&nbsp;<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">&#39;Hello everyone&#39;</span><span class="enlighter-g1">)</span></div></div></div><div class="enlighter-raw">def greeting():
    print(&#39;Hello everyone&#39;)</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(&#39;Hello everyone&#39;)</pre>



<h4 class="wp-block-heading">Use a Module</h4>



<p>Now we can use the module we just created, by using the&nbsp;<code>import</code>&nbsp;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">&#39;email&#39;</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[&#39;email&#39;]
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[&#39;email&#39;]
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&nbsp;<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&nbsp;<code>as</code>&nbsp;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">&#39;name&#39;</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[&#39;name&#39;]
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[&#39;name&#39;]
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">&#39;new module&#39;</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>(&#39;new module&#39;)
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>(&#39;new module&#39;)
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">&#39;%Y-%m-%d %H:%M:%S&#39;</span><span class="enlighter-g1">))</span></div></div></div><div class="enlighter-raw">import datetime
current=datetime.datetime.now()
print(current.strftime(&#39;%Y-%m-%d %H:%M:%S&#39;))</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(&#39;%Y-%m-%d %H:%M:%S&#39;))</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&nbsp;<code>dir()</code>&nbsp;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>[&#39;DEV_NULL&#39;, &#39;_UNIXCONFDIR&#39;, &#39;_WIN32_CLIENT_RELEASES&#39;, &#39;_WIN32_SERVER_RELEASES&#39;, &#39;<strong>builtins</strong>&#39;, &#39;<strong>cached</strong>&#39;, &#39;<strong>copyright</strong>&#39;, &#39;<strong>doc</strong>&#39;, &#39;<strong>file</strong>&#39;, &#39;<strong>loader</strong>&#39;, &#39;<strong>name</strong>&#39;, &#39;<strong>package</strong>&#39;, &#39;<strong>spec</strong>&#39;, &#39;<strong>version</strong>&#39;, &#39;_default_architecture&#39;, &#39;_dist_try_harder&#39;, &#39;_follow_symlinks&#39;, &#39;_ironpython26_sys_version_parser&#39;, &#39;_ironpython_sys_version_parser&#39;, &#39;_java_getprop&#39;, &#39;_libc_search&#39;, &#39;_linux_distribution&#39;, &#39;_lsb_release_version&#39;, &#39;_mac_ver_xml&#39;, &#39;_node&#39;, &#39;_norm_version&#39;, &#39;_parse_release_file&#39;, &#39;_platform&#39;, &#39;_platform_cache&#39;, &#39;_pypy_sys_version_parser&#39;, &#39;_release_filename&#39;, &#39;_release_version&#39;, &#39;_supported_dists&#39;, &#39;_sys_version&#39;, &#39;_sys_version_cache&#39;, &#39;_sys_version_parser&#39;, &#39;_syscmd_file&#39;, &#39;_syscmd_uname&#39;, &#39;_syscmd_ver&#39;, &#39;_uname_cache&#39;, &#39;_ver_output&#39;, &#39;architecture&#39;, &#39;collections&#39;, &#39;dist&#39;, &#39;java_ver&#39;, &#39;libc_ver&#39;, &#39;linux_distribution&#39;, &#39;mac_ver&#39;, &#39;machine&#39;, &#39;node&#39;, &#39;os&#39;, &#39;platform&#39;, &#39;popen&#39;, &#39;processor&#39;, &#39;python_branch&#39;, &#39;python_build&#39;, &#39;python_compiler&#39;, &#39;python_implementation&#39;, &#39;python_revision&#39;, &#39;python_version&#39;, &#39;python_version_tuple&#39;, &#39;re&#39;, &#39;release&#39;, &#39;subprocess&#39;, &#39;sys&#39;, &#39;system&#39;, &#39;system_alias&#39;, &#39;uname&#39;, &#39;uname_result&#39;, &#39;version&#39;, &#39;warnings&#39;, &#39;win32_ver&#39;]</code></pre>
</div></div>

<p></div></p>