{"id":7453,"date":"2024-03-19T06:48:53","date_gmt":"2024-03-19T06:48:53","guid":{"rendered":"https:\/\/www.skillvertex.com\/blog\/?p=7453"},"modified":"2024-03-19T06:48:53","modified_gmt":"2024-03-19T06:48:53","slug":"python-modules","status":"publish","type":"post","link":"https:\/\/www.skillvertex.com\/blog\/python-modules\/","title":{"rendered":"Python Modules"},"content":{"rendered":"\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\" id=\"rank-math-toc\"><p>Table of Contents<\/p><nav><ul><li ><a href=\"#what-is-a-python-module\">What is a Python Module?<\/a><ul><li ><a href=\"#create-a-module\">Create a Module<\/a><\/li><li ><a href=\"#use-a-module\">Use a Module<\/a><\/li><\/ul><\/li><li ><a href=\"#what-are-the-examples-of-importing-modules-in-python\">What are the examples of Importing Modules in Python?<\/a><\/li><li ><a href=\"#how-to-import-specific-attributes-from-a-python-module\">How to Import Specific Attributes From a Python Module?<\/a><\/li><li ><a href=\"#what-is-import-symbol\">What is * Import symbol?<\/a><\/li><li ><a href=\"#locate-python-module\"> Locate Python Module<\/a><\/li><li ><a href=\"#what-is-the-directories-list-for-modules\">What is the Directories List For Modules?<\/a><\/li><li ><a href=\"#how-to-rename-the-python-module\">How to Rename the Python Module?<\/a><\/li><li ><a href=\"#what-is-python-built-in-modules\">What is Python Built-in Modules?<\/a><\/li><li ><a href=\"#conclusion\">Conclusion<\/a><\/li><li ><a href=\"#python-modules-fa-qs\">Python Modules-FAQs<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<p>Python Module has built-in functions, classes, and variables. There are several Python modules and each has their specific work. This article has listed the Python modules.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-a-python-module\">What is a Python Module?<\/h2>\n\n\n\n<p>The <a href=\"https:\/\/www.skillvertex.com\/blog\/which-of-the-following-is-a-bad-python-variable-name\/\" data-type=\"post\" data-id=\"2650\">Python<\/a> module consists of a file that has Python definitions and statements. A module will then define <a href=\"https:\/\/www.skillvertex.com\/blog\/nested-functions-in-c\/\" data-type=\"post\" data-id=\"2555\">functions<\/a>, <a href=\"https:\/\/www.skillvertex.com\/blog\/which-class-provides-many-methods-for-graphics-programming\/\" data-type=\"post\" data-id=\"2583\">classes<\/a>, and <a href=\"https:\/\/www.skillvertex.com\/blog\/python-variables\/\" data-type=\"post\" data-id=\"6932\">variables.<\/a>  Additionally, the module has a runnable code.<\/p>\n\n\n\n<p>Hence, categorizing the module will make the code easier to analyze. Therefore, the code will be more organized.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"create-a-module\">Create a Module<\/h3>\n\n\n\n<p>a.Create a new file, e.g., <code>my_module.py<\/code>.<\/p>\n\n\n\n<p>b.Define your<a href=\"https:\/\/www.skillvertex.com\/blog\/which-of-these-modules-is-not-included-in-azure-powershell\/\" data-type=\"post\" data-id=\"2764\"> module<\/a> content inside this file.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># File: my_module.py\n\ndef greet(name):\n    \"\"\"A simple function to greet the user.\"\"\"\n    return f\"Hello, {name}! Welcome to my_module.\"\n<\/code><\/pre>\n\n\n\n<p>The syntax is given below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import module\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"use-a-module\">Use a Module<\/h3>\n\n\n\n<p>You will use the <a href=\"https:\/\/www.skillvertex.com\/blog\/mongodb-node-js-module-mongo-is-not-recognised-as-an-internal-or-external-command\/\" data-type=\"post\" data-id=\"3538\">module <\/a>that was created through the import statement.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># File: main_script.py\nimport my_module\n\n# Using the greet function from my_module\nname = \"User\"\nmessage = my_module.greet(name)\nprint(message)\n<\/code><\/pre>\n\n\n\n<p>When you run main_script.py, it will import the my_module and take the greet function to print a welcome message.<\/p>\n\n\n\n<p>These two files (my_module.py and main_script.py) are placed in the same directory where Python can find them.<\/p>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Hello, User! Welcome to my_module.\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-are-the-examples-of-importing-modules-in-python\">What are the examples of Importing Modules in Python?<\/h2>\n\n\n\n<p>The example illustrated below will import the calc for executing the <a href=\"https:\/\/www.skillvertex.com\/blog\/python-arithmetic-operators\/\" data-type=\"post\" data-id=\"6958\">add <\/a>operation.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># File: main_script.py\nimport my_module\n\n# Using the greet function from my_module\nname = \"User\"\ngreeting_message = my_module.greet(name)\nprint(greeting_message)\n\n# Using the add_numbers function from my_module\nresult = my_module.add_numbers(5, 7)\nprint(f\"The result of addition is: {result}\")\n<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Hello, User! Welcome to my_module.\nThe result of addition is: 12\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"how-to-import-specific-attributes-from-a-python-module\">How to Import Specific Attributes From a Python Module?<\/h2>\n\n\n\n<p>Let us look into the example given below :<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># File: main_script.py\nfrom math import sqrt, factorial\n\n# Using the sqrt function from math\nnumber = 25\nsquare_root = sqrt(number)\nprint(f\"The square root of {number} is: {square_root}\")\n\n# Using the factorial function from math\nfact_number = 5\nfactorial_result = factorial(fact_number)\nprint(f\"The factorial of {fact_number} is: {factorial_result}\")\n<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>The square root of 25 is: 5.0\nThe factorial of 5 is: 120\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-import-symbol\">What is * Import symbol?<\/h2>\n\n\n\n<p>The  * symbol is used to import the <a href=\"https:\/\/www.skillvertex.com\/blog\/which-of-the-following-statements-about-the-views-in-sql-server-is-true\/\" data-type=\"post\" data-id=\"2728\">statement<\/a> function to import all the names from the module to the current namespace.<\/p>\n\n\n\n<p>The syntax is given below<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from module_name import *\n<\/code><\/pre>\n\n\n\n<p>Note: <\/p>\n\n\n\n<p>The *  symbol has its benefits and drawbacks. Suppose the purpose is known to you. However, it is not advised to use *.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># File: main_script.py\nfrom math import sqrt, factorial\n\n# Using the sqrt function from math\nnumber = 25\nsquare_root = sqrt(number)\nprint(f\"The square root of {number} is: {square_root}\")\n\n# Using the factorial function from math\nfact_number = 5\nfactorial_result = factorial(fact_number)\nprint(f\"The factorial of {fact_number} is: {factorial_result}\")\n<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>The square root of 25 is: 5.0\nThe factorial of 5 is: 120\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"locate-python-module\"> Locate Python Module<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Current Directory:<\/strong>\n<ul class=\"wp-block-list\">\n<li>First, Python checks if the module is in the same folder where your Python script is located.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>PYTHON PATH:<\/strong>\n<ul class=\"wp-block-list\">\n<li>If it&#8217;s not in the current folder, Python checks other places listed in the PYTHON PATH variable.<\/li>\n\n\n\n<li>PYTHON PATH is like a list of folders where Python looks for modules. You can add more folders to this list if needed.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Installation-Dependent Directories:<\/strong>\n<ul class=\"wp-block-list\">\n<li>If the module is still not found, Python checks places to set up when Python was installed.<\/li>\n\n\n\n<li>These are special folders that come with Python and contain important modules that are needed for many programs.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-the-directories-list-for-modules\">What is the Directories List For Modules?<\/h2>\n\n\n\n<p>The sys. path has a built-in variable within the sys module.  It has a list of directories, in which the interpreter will search in the particular module.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># File: main_script.py\nimport sys\n\n# Now you can use functions and variables from the sys module\nprint(\"Python version:\")\nprint(sys.version)\nprint(\"\")\n\nprint(\"System platform:\")\nprint(sys.platform)\n<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Python version:\n3.8.5 (default, Jan 27 2021, 15:41:15)\n&#91;GCC 9.3.0]\n\nSystem platform:\nlinux\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"how-to-rename-the-python-module\">How to Rename the Python Module?<\/h2>\n\n\n\n<p>The Python module will rename the module by importing it with the keyword.<\/p>\n\n\n\n<p>The syntax is provided below<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Syntax:  Import Module_name as Alias_name\n\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code># File: main_script.py\nimport math\n\n# Using the sqrt function from math\nnumber = 18  # Change the number to 18\nsquare_root = math.sqrt(number)\nprint(f\"The square root of {number} is: {square_root}\")\n\n# Using the factorial function from math\nfact_number = 5\nfactorial_result = math.factorial(fact_number)\nprint(f\"The factorial of {fact_number} is: {factorial_result}\")\n<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>The square root of 18 is: 4.242640687119285\nThe factorial of 5 is: 120\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-python-built-in-modules\">What is Python Built-in Modules?<\/h2>\n\n\n\n<p>Python consists of built-in modules that allow them to import whenever required.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># File: main_script.py\nimport math\n\n# Using the sqrt function from math\nnumber = 25\nsquare_root = math.sqrt(number)\nprint(f\"The square root of {number} is: {square_root}\")\n\n# Using the factorial function from math\nfact_number = 5\nfactorial_result = math.factorial(fact_number)\nprint(f\"The factorial of {fact_number} is: {factorial_result}\")\n\n# Using the pi function from math\npi_value = math.pi\nprint(f\"The value of pi is: {pi_value}\")\n\n# Converting 2 radians to degrees\nradians_to_degrees = math.degrees(2)\nprint(f\"2 radians in degrees is: {radians_to_degrees}\")\n\n# Converting 60 degrees to radians\ndegrees_to_radians = math.radians(60)\nprint(f\"60 degrees in radians is: {degrees_to_radians}\")\n\n# Calculating sine of 2 radians\nsine_value = math.sin(2)\nprint(f\"The sine of 2 radians is: {sine_value}\")\n\n# Calculating cosine of 0.5 radians\ncosine_value = math.cos(0.5)\nprint(f\"The cosine of 0.5 radians is: {cosine_value}\")\n\n# Calculating tangent of 0.23 radians\ntangent_value = math.tan(0.23)\nprint(f\"The tangent of 0.23 radians is: {tangent_value}\")\n<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>The square root of 25 is: 5.0\nThe factorial of 5 is: 120\nThe value of pi is: 3.141592653589793\n2 radians in degrees is: 114.59155902616465\n60 degrees in radians is: 1.0471975511965979\nThe sine of 2 radians is: 0.9092974268256817\nThe cosine of 0.5 radians is: 0.8775825618903728\nThe tangent of 0.23 radians is: 0.23414336235146526\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"conclusion\">Conclusion<\/h2>\n\n\n\n<p>To conclude, this article has offered the information about the Python Modules. It has also included how to create the module, Python import from the module, Renaming the Python Module, and several others.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"python-modules-fa-qs\">Python Modules-FAQs<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1709102542804\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q1.What are the modules in Python?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. Modules are referred to as the files with the \u201c.\u00a0py\u201d extension and have Python code that can be imported inside another Python Program.\u00a0<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1709102553083\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q2.How many modules of Python are there?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans.200<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1709102560798\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q3.What are the five modules in Python?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. Those five modules are &#8211;\u00a0collections, datetime, logging, math, numpy, os, pip, sys, and time<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Python Module has built-in functions, classes, and variables. There are several Python modules and each has their specific work. This article has listed the Python modules. What is a Python Module? The Python module consists of a file that has Python definitions and statements. A module will then define functions, classes, and variables. Additionally, the &#8230; <a title=\"Python Modules\" class=\"read-more\" href=\"https:\/\/www.skillvertex.com\/blog\/python-modules\/\" aria-label=\"More on Python Modules\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":7456,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[864],"tags":[949,945,948,866,950],"class_list":["post-7453","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-tutorial","tag-python-classes","tag-python-function","tag-python-module","tag-python-tutorial","tag-python-variable","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-33"],"_links":{"self":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/7453","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/comments?post=7453"}],"version-history":[{"count":10,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/7453\/revisions"}],"predecessor-version":[{"id":8280,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/7453\/revisions\/8280"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media\/7456"}],"wp:attachment":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media?parent=7453"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/categories?post=7453"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/tags?post=7453"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}