{"id":6996,"date":"2019-01-09T23:11:42","date_gmt":"2019-01-09T17:41:42","guid":{"rendered":"https:\/\/techbeamers.com\/?p=6996"},"modified":"2025-11-30T10:52:44","modified_gmt":"2025-11-30T15:52:44","slug":"python-module","status":"publish","type":"post","link":"https:\/\/techbeamers.com\/python-module\/","title":{"rendered":"Modules in Python with Examples"},"content":{"rendered":"\n<p>In this Python programming class, we&#8217;ll explain: What is a module in Python with examples. We&#8217;ll provide examples to demonstrate how to import modules in your code. Please note that modules are the pillars of modular programming in Python.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-what-is-module-in-python-with-example\"><a id=\"post-7790-_mgimcvxddv0q\"><\/a>What is Module in Python with Example<\/h2>\n\n\n\n<p>Greetings readers, in this tutorial, you will learn about modules in Python and their usage. We are also going to teach you how to implement them in Python.<\/p>\n\n\n\n<p><strong>Note:<\/strong> We are going to teach according to Python 3 syntax. With slight modification, you can use the code snippets on Python 2.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-1-introduction-to-python-module\"><a id=\"intro\"><\/a>1. Introduction to Python Module<\/h3>\n\n\n\n<p>Modules are primarily the (.py) files that contain Python code defining functions, classes, variables, etc. with a suffix .py appended in its file name.<\/p>\n\n\n\n<p>They can have different functions, variables, and classes in one file. We can also call them libraries.<\/p>\n\n\n\n<p>A Python module brings certain benefits such as we can reduce redundancy in the code. It can let us maintain uniformity in the&nbsp;coding style.<\/p>\n\n\n\n<p><strong>Example: Take a file called math_function_def.py<\/strong><\/p>\n\n\n\n<p>It could contain functions for calculating the factorial value of a number, cube, square root, constant values such as the value of pi, <a href=\"https:\/\/techbeamers.com\/python-program-generate-fibonacci-sequence\/\" target=\"_blank\" rel=\"noopener\">Fibonacci sequence generation<\/a> code, etc.<\/p>\n\n\n\n<p>Generally, it is a good practice to create modules that have a fixed purpose. It increases readability and increases productivity and bug reporting.<\/p>\n\n\n\n<p>A few examples of modules are:<\/p>\n\n\n\n<p><strong>From Python Standard Library:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>OS, Time, Math, MatPlotlib,&nbsp;etc.<\/li>\n<\/ul>\n\n\n\n<p><strong>From Online Sources:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Keras(for deep learning), Numpy(for number manipulation), Pandas(for array manipulation), etc.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-2-python-module-mechanism\"><a id=\"mechanism\"><\/a>2. Python Module: Mechanism<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-2-1-mechanism\"><a id=\"mechanism-detail\"><\/a>2.1. Mechanism<\/h4>\n\n\n\n<p>For systems where Python is pre-installed or when installed using the&nbsp;system package manager such as <code>apt-get<\/code>, <code>dnf<\/code>, <code>zypper<\/code>, or using package environment managers like Anaconda the following applies.<\/p>\n\n\n\n<p>When we import modules, the Python interpreter locates them in three locations:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>The directory from the program is executed<\/li>\n\n\n\n<li>The directory specified in the PYTHONPATH variable (A shell variable or an environment variable)<\/li>\n\n\n\n<li>The default directory (It depends on the OS distribution.)<\/li>\n<\/ol>\n\n\n\n<p>The Flowchart for the above mechanism is as follows:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"911\" height=\"361\" src=\"https:\/\/techbeamers.com\/wp-content\/uploads\/2019\/01\/Python-Module-Flowchart.png\" alt=\"Python Module - Flowchart\" class=\"wp-image-6998\"\/><\/figure>\n<\/div>\n\n\n<h4 class=\"wp-block-heading\" id=\"h-2-2-modules-listing\"><a id=\"listing\"><\/a>2.2. Modules Listing<\/h4>\n\n\n\n<p>To find out the list of modules present in Python, we can issue the command: help(&#8220;modules&#8221;) in the Python interpreter shell.<\/p>\n\n\n\n<p>Executing the above command will return the list of available modules, as shown below:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"482\" src=\"https:\/\/techbeamers.com\/wp-content\/uploads\/2019\/01\/Python-Module-List-of-Avaiable-Modules.jpeg\" alt=\"Python Module - List of Available Modules\" class=\"wp-image-6999\"\/><\/figure>\n<\/div>\n\n\n<p>Besides, you can also issue the <strong>pip list or conda list<\/strong> command in the console to list all available modules.<\/p>\n\n\n\n<p>The following diagram shows the output for the&nbsp;second method in Windows 10 cmd shell.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"528\" height=\"694\" src=\"https:\/\/techbeamers.com\/wp-content\/uploads\/2019\/01\/Python-Module-Conda-List-Command.jpeg\" alt=\"List all available Python modules\" class=\"wp-image-7000\"\/><\/figure>\n<\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"h-3-modules-implementation\">3. Modules: Implementation<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-3-1-importing-of-modules-from-standard-python-path\"><a id=\"standard\"><\/a>3.1. Importing of Modules from Standard Python Path<\/h4>\n\n\n\n<p><strong>Syntax &#8211; Using Full Name<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import module_name1, module_name2\u2026<\/pre>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import os<\/pre>\n\n\n\n<p>If the module name is too long to type, we can assign an alias&nbsp;as short as a single letter. Here, we have further explained in detail <a href=\"https:\/\/techbeamers.com\/how-to-import-another-python-file\/\" target=\"_blank\" rel=\"noreferrer noopener\">how to import a module in Python<\/a>.<\/p>\n\n\n\n<p><strong>Syntax&nbsp;&#8211; Using a Short Name<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import module_name as shortened_module_name<\/pre>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import math as m<\/pre>\n\n\n\n<p>It is a time-saver for those who have module names that are too long to remember to type.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-3-2-importing-modules-from-new-sources\"><a id=\"new-source\"><\/a>3.2. Importing Modules from New Sources<\/h4>\n\n\n\n<p>To load new modules from new Sources, we must install using Python pip a software utility that installs Python modules from the Python index online or using a package environment manager like Anaconda.<\/p>\n\n\n\n<p><strong>Python PIP to Install New Modules<\/strong><\/p>\n\n\n\n<p>Run the following command to install a Python module.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">python -m pip3 install module_package_name<\/pre>\n\n\n\n<p><strong>Anaconda to Install new Modules<\/strong><\/p>\n\n\n\n<p>Run the following command to install a Python module<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">conda install module_package_name<\/pre>\n\n\n\n<p><strong>System Package Manager to Install New Modules<\/strong><\/p>\n\n\n\n<p>Run the following command to install a Python module on Ubuntu.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo apt install module_package_name<\/pre>\n\n\n\n<p>For example, If we want to install Numpy.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">python -m pip3 install numpy\nconda install numpy\nsudo apt install python3-numpy<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-4-module-program-examples\"><a id=\"examples\"><\/a>4. Module Program Examples<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-4-1-built-in-modules\"><a id=\"built-In\"><\/a>4.1. Built-In Modules<\/h4>\n\n\n\n<p>There are several built-in modules such as dir(), math(), random(), time(), datetime() etc.<\/p>\n\n\n\n<p>Example Program:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import math, random #we can write multiple modules in one import statement.\n\nprint (math.sqrt(625)) #prints square root of number 625\n\nprint (math.factorial(10)) #prints factorial value of a number 10\n\nprint (math.pi) #prints value of pi according to the built-in module\n\nprint (random.randint(1,20)) #prints a <a href=\"https:\/\/techbeamers.com\/python-generate-random-numbers-list\/\" target=\"_blank\" rel=\"noopener\">random value from integers<\/a> 1-20\n\nprint (dir(math)) #prints function name, variables,etc in math module<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-4-2-user-defined-python-module\"><a id=\"user-defined\"><\/a>4.2. User-Defined Python Module<\/h4>\n\n\n\n<p>Take a Python file, for example, <strong>factorial_definition.py<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">def get_factorial(param):\n    out = 1\n    if param &lt; 0:\n        print(\"Factorial not allowed for -ve numbers\")\n        out = None\n    elif param == 0:\n        print(\"Factorial(0) = 1\")\n        out = None\n    else:\n        for i in range(1, param + 1):\n            out = out*i\n    return out\n\n# For testing purpose:\n# in_value = -1\n# in_value = 0\n# in_value = 5\n# out = get_factorial(in_value)\n\n# if out != None:\n#    print(f\"Factorial of {in_value} is {out}\")<\/pre>\n\n\n\n<p>Save this file either in PYTHONPATH or in the path where another program resides which will import the module.<\/p>\n\n\n\n<p>To import this file, we use the following code in the program, which will load the module.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import factorial_definition\n\nfactorial_definition.factorial()<\/pre>\n\n\n\n<p>We can call the variable Pi using <strong>factorial_definition.Pi<\/strong><\/p>\n\n\n\n<p>Since the module name is long, we can rename it in the manner <strong>import factorial_definition as fact <\/strong>and use this to call the variables and variables.<\/p>\n\n\n\n<p>If we want we can import only the Pi variable, to do so, we use <strong>from factorial_definition import Pi.<\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-5-usages-of-modules\"><a id=\"usages\"><\/a>5. Usages of Modules<\/h3>\n\n\n\n<p>Modules are used to reduce the redundant statements in a program. It saves time and increases readability as well as productivity. They are also used to extend the functionality of Python and allow different developers around the world to work in a coordinated manner.<\/p>\n\n\n\n<p>For example, Google developed Tensorflow, which contains functions for deep learning and is open for contributions from the past many years. It is an open-source module so that various people from different parts of the world can participate and improve the scope of deep learning applications.<\/p>\n\n\n\n<p>The <strong>TensorFlow library<\/strong> uses the icon shown below.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"225\" height=\"225\" src=\"https:\/\/techbeamers.com\/wp-content\/uploads\/2019\/01\/Python-Module-TensorFlow.png\" alt=\"TensorFlow library\" class=\"wp-image-7001\"\/><\/figure>\n<\/div>\n\n\n<p>Other examples of open-source modules are Keras, OpenCV, etc.<\/p>\n\n\n\n<p><strong>Keras Module<\/strong><\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"227\" height=\"66\" src=\"https:\/\/techbeamers.com\/wp-content\/uploads\/2019\/01\/Python-Module-Keras.png\" alt=\"Keras Module\" class=\"wp-image-7002\"\/><\/figure>\n<\/div>\n\n\n<p><strong>OpenCV Module<\/strong><\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"54\" height=\"50\" src=\"https:\/\/techbeamers.com\/wp-content\/uploads\/2019\/01\/Python-Module-OpenCV.png\" alt=\"OpenCV\" class=\"wp-image-7003\"\/><\/figure>\n<\/div>\n\n\n<p>To Learn more topics, read the latest <a href=\"https:\/\/techbeamers.com\/python-tutorial-step-by-step\/\" target=\"_blank\" rel=\"noopener noreferrer\">Python tutorial<\/a>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-before-you-leave\">Before You Leave<\/h3>\n\n\n\n<p>This tutorial clearly explained: what is a module in Python with examples, and how to create and import them in programs. Python modules are a powerful way to organize and reuse code. By grouping related functions and classes into modules, you can make your code more modular and easier to maintain.<\/p>\n\n\n\n<p>Before we leave, let&#8217;s share some tips for using Python modules effectively:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use descriptive names for your modules and functions. This will make it easier to understand and use your code.<\/li>\n\n\n\n<li>Document your modules and functions. This will help other people understand how to use your code.<\/li>\n\n\n\n<li>Import only the modules and functions that you need. This will help to keep your code efficient.<\/li>\n\n\n\n<li>Use namespaces to avoid name collisions. This can happen when you import multiple modules that have functions with the same name.<\/li>\n<\/ul>\n\n\n\n<p>Lastly, our site needs your support to remain free. Share this post on social media (<a href=\"https:\/\/www.linkedin.com\/company\/techbeamers\/\" target=\"_blank\" rel=\"noreferrer noopener\">Linkedin<\/a>\/<a href=\"https:\/\/www.facebook.com\/TechBeamersWorld\/\" target=\"_blank\" rel=\"noopener\" title=\"\">Facebook<\/a>) if you gained some knowledge from this tutorial.<\/p>\n\n\n\n<p><strong>Enjoy coding,<br>TechBeamers.<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this Python programming class, we&#8217;ll explain: What is a module in Python with examples. We&#8217;ll provide examples to demonstrate how to import modules in your code. Please note that modules are the pillars of modular programming in Python. What is Module in Python with Example Greetings readers, in this tutorial, you will learn about [&hellip;]<\/p>\n","protected":false},"author":321,"featured_media":6997,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","jetpack_post_was_ever_published":false},"categories":[92],"tags":[],"class_list":{"0":"post-6996","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-python-programming-tutorials"},"jetpack_featured_media_url":"https:\/\/techbeamers.com\/wp-content\/uploads\/2019\/01\/Python-Module-A-Step-by-Step-Tutorial-for-Beginners.png","_links":{"self":[{"href":"https:\/\/techbeamers.com\/wp-json\/wp\/v2\/posts\/6996","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techbeamers.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techbeamers.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techbeamers.com\/wp-json\/wp\/v2\/users\/321"}],"replies":[{"embeddable":true,"href":"https:\/\/techbeamers.com\/wp-json\/wp\/v2\/comments?post=6996"}],"version-history":[{"count":1,"href":"https:\/\/techbeamers.com\/wp-json\/wp\/v2\/posts\/6996\/revisions"}],"predecessor-version":[{"id":23561,"href":"https:\/\/techbeamers.com\/wp-json\/wp\/v2\/posts\/6996\/revisions\/23561"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techbeamers.com\/wp-json\/wp\/v2\/media\/6997"}],"wp:attachment":[{"href":"https:\/\/techbeamers.com\/wp-json\/wp\/v2\/media?parent=6996"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techbeamers.com\/wp-json\/wp\/v2\/categories?post=6996"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techbeamers.com\/wp-json\/wp\/v2\/tags?post=6996"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}