{"id":1222,"date":"2019-12-18T12:46:30","date_gmt":"2019-12-18T12:46:30","guid":{"rendered":"https:\/\/www.askpython.com\/?p=1222"},"modified":"2022-08-06T13:17:07","modified_gmt":"2022-08-06T13:17:07","slug":"python-modules","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/python-modules","title":{"rendered":"Introduction to Python Modules"},"content":{"rendered":"\n<p>Modules are the pre-defined files that contain the python codes which depict the basic functionalities of class, methods, variables, etc. It consists of different functions, classes in a group of files inside a directory. Modules can also be termed as Libraries. These are basically the pre-defined methods that can be used to make the code more efficient and reduce redundancy.<\/p>\n\n\n\n<p>Modules bind the code and reduce the repetitions of functions frequently used in the code. Thus, it makes the code much clear and easy to understand.<\/p>\n\n\n\n<p><strong>Examples:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li> OS<\/li><li>Time<\/li><li>Math<\/li><li>MatPlotlib <\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Mechanism of Python Modules<\/h2>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"971\" height=\"97\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/12\/Python-Module-Mechansim.png\" alt=\"Python Module Mechansim\" class=\"wp-image-1228\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/12\/Python-Module-Mechansim.png 971w, https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/12\/Python-Module-Mechansim-300x30.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/12\/Python-Module-Mechansim-768x77.png 768w\" sizes=\"auto, (max-width: 971px) 100vw, 971px\" \/><figcaption>Python Module Mechanism<\/figcaption><\/figure><\/div>\n\n\n\n<p>The moment a module is imported through a program, Python Interpreter fetches the module from either of the following locations:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Program Directory<\/li><li>The directory in the PYTHONPATH variable<\/li><li>Default directory<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Listing of Modules<\/h2>\n\n\n\n<p>The list of available modules in Python can be found out by executing the following command in the command prompt (interpreter shell).<\/p>\n\n\n\n<p>&gt;&gt;&gt; help(&#8220;module&#8221;)<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/12\/Python-Module-List-1024x576.png\" alt=\"Python Module List\" class=\"wp-image-1230\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/12\/Python-Module-List-1024x576.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/12\/Python-Module-List-300x169.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/12\/Python-Module-List-768x432.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/12\/Python-Module-List-1536x864.png 1536w, https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/12\/Python-Module-List.png 1920w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Python Module List<\/figcaption><\/figure><\/div>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Importing modules from Python Standard path<\/h2>\n\n\n\n<p><strong>Syntax<\/strong>:<\/p>\n\n\n\n<p>import module_name<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<p>import math<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Importing Modules from other Sources<\/h2>\n\n\n\n<p>To fetch and use modules from other and new sources, we need to install Python PIP.<\/p>\n\n\n\n<p>Python pip is a software that installs python modules from index or using a manager like Anaconda.<\/p>\n\n\n\n<p><strong>Run the following command to install modules from new sources using python pip:<\/strong><\/p>\n\n\n\n<p>python -m pip3 install module_name<\/p>\n\n\n\n<p><strong>Run the following command to install modules from new sources using Ananconda:<\/strong><\/p>\n\n\n\n<p>conda install module_name<\/p>\n\n\n\n<p><strong>Example: Steps to install numpy<\/strong><\/p>\n\n\n\n<p>python -m pip3 install numpy<br>conda install numpy<br>sudo apt install python3-numpy<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Example: Built-in Modules<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport math \n\nprint (math.sqrt(121)) \n\nprint (math.pi) \n\nprint (dir(math))\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<p>11.0                                                                                                                                            <br> 3.141592653589793                                                                                                                               <br> [&#8216;doc&#8217;, &#8216;loader&#8217;, &#8216;name&#8217;, &#8216;package&#8217;, &#8216;spec&#8217;, &#8216;acos&#8217;, &#8216;acosh&#8217;, &#8216;asin&#8217;, &#8216;asinh&#8217;, &#8216;atan&#8217;, &#8216;atan2&#8217;, &#8216;atanh&#8217;, &#8216;ceil&#8217;, &#8216;copysign  <br> &#8216;, &#8216;cos&#8217;, &#8216;cosh&#8217;, &#8216;degrees&#8217;, &#8216;e&#8217;, &#8216;erf&#8217;, &#8216;erfc&#8217;, &#8216;exp&#8217;, &#8216;expm1&#8217;, &#8216;fabs&#8217;, &#8216;factorial&#8217;, &#8216;floor&#8217;, &#8216;fmod&#8217;, &#8216;frexp&#8217;, &#8216;fsum&#8217;, &#8216;gamma&#8217;, &#8216;hypot&#8217;, &#8216;isf  <br> inite&#8217;, &#8216;isinf&#8217;, &#8216;isnan&#8217;, &#8216;ldexp&#8217;, &#8216;lgamma&#8217;, &#8216;log&#8217;, &#8216;log10&#8217;, &#8216;log1p&#8217;, &#8216;log2&#8217;, &#8216;modf&#8217;, &#8216;pi&#8217;, &#8216;pow&#8217;, &#8216;radians&#8217;, &#8216;sin&#8217;, &#8216;sinh&#8217;, &#8216;sqrt&#8217;, &#8216;tan&#8217;, &#8216;t  <br> anh&#8217;, &#8216;trunc&#8217;]   <\/p>\n\n\n\n<p>In the above example, dir() method gives the function name, variables, etc in the math module.    <\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Variable in a Module<\/h2>\n\n\n\n<p>Apart from methods and classes, A module can also contain variables.<\/p>\n\n\n\n<p><strong>Example<\/strong>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nFruit = {\n  &quot;name&quot;: &quot;Apple&quot;,\n  &quot;color&quot;: &quot;Green&quot;\n}\n<\/pre><\/div>\n\n\n<p>Save the above snippet of code in the file Module1.py<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport Module1\n\nx = Module1.Fruit&#x5B;&quot;name&quot;]\nprint(x)\n\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<p>Apple<\/p>\n\n\n\n<p>In the above piece of code, Module1 is imported and functionality is performed on it.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Difference between a module and a package in Python<\/h2>\n\n\n\n<p><strong>Python Module<\/strong>: These are set of pre-defined files that contain the python codes which depict the basic functionalities of class, methods, variables, etc. <\/p>\n\n\n\n<p><strong>Python Package<\/strong>: It is a directory that holds and contains modules and sub-packages.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">References<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Python Module<\/li><li><a href=\"https:\/\/docs.python.org\/3\/tutorial\/modules.html\" target=\"_blank\" rel=\"noopener\">Python Module-Docs<\/a><\/li><\/ul>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Modules are the pre-defined files that contain the python codes which depict the basic functionalities of class, methods, variables, etc. It consists of different functions, classes in a group of files inside a directory. Modules can also be termed as Libraries. These are basically the pre-defined methods that can be used to make the code [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-1222","post","type-post","status-publish","format-standard","hentry","category-python-modules"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/1222","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=1222"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/1222\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=1222"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=1222"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=1222"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}