{"id":21010,"date":"2022-12-13T06:45:00","date_gmt":"2022-12-13T05:45:00","guid":{"rendered":"https:\/\/expert-only.com\/?p=21010"},"modified":"2023-01-11T18:16:58","modified_gmt":"2023-01-11T17:16:58","slug":"python-modules","status":"publish","type":"post","link":"https:\/\/expert-only.com\/en\/python\/python-modules\/","title":{"rendered":"Python Modules"},"content":{"rendered":"\n<h4 class=\"has-text-align-center wp-block-heading\"><strong><em>Tutorial on how to use modules in Python like import, create, document and define classes with scripts examples.<\/em><\/strong><\/h4>\n\n\n\n<p>This part of the Python course is about the Python modules. A module in Python is simply a file that contains definitions, functions, and statements. Here&#8217;s a detailed explanation of how modules work in Python, with code examples to illustrate each concept:<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 ez-toc-wrap-center counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/expert-only.com\/en\/python\/python-modules\/#1-import-and-use-built-in-modules-in-python\" >1. Import and use built-in modules in Python<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/expert-only.com\/en\/python\/python-modules\/#2-import-specific-functions-from-a-python-module\" >2. Import specific functions from a Python module<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/expert-only.com\/en\/python\/python-modules\/#3-assign-aliases-to-modules-or-functions-in-python\" >3. Assign aliases to modules or functions in Python<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/expert-only.com\/en\/python\/python-modules\/#4-access-functions-and-variables-in-python-modules\" >4. Access functions and variables in Python modules<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/expert-only.com\/en\/python\/python-modules\/#5-create-and-use-custom-python-modules\" >5. Create and use custom Python modules<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/expert-only.com\/en\/python\/python-modules\/#6-how-to-create-and-use-packages-in-python\" >6. How to create and use packages in Python?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/expert-only.com\/en\/python\/python-modules\/#7-document-python-modules-with-docstrings\" >7. Document Python modules with docstrings<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/expert-only.com\/en\/python\/python-modules\/#8-execute-statements-in-python-modules\" >8. Execute statements in Python modules<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/expert-only.com\/en\/python\/python-modules\/#9-define-classes-in-python-modules\" >9. Define classes in Python modules<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/expert-only.com\/en\/python\/python-modules\/#10-use-the-main-block-in-modules\" >10. Use the main block in modules<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/expert-only.com\/en\/python\/python-modules\/#conclusion-on-working-with-python-modules\" >Conclusion on working with Python modules<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\" id=\"h-1-import-and-use-built-in-modules-in-python\"><span class=\"ez-toc-section\" id=\"1-import-and-use-built-in-modules-in-python\"><\/span>1. Import and use built-in modules in Python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Python comes with a variety of built-in modules that provide a wide range of functionality. These modules are imported using the &#8220;import&#8221; statement. For example, to import the built-in &#8220;math&#8221; module, you would write &#8220;import math&#8221; at the top of your script.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import math\n<\/pre>\n\n\n\n<p>Once the module is imported, you can use its functions by calling them with the module&#8217;s name as a prefix. For example, to use the &#8220;sqrt&#8221; function from the math module, you would call &#8220;math.sqrt(4)&#8221;.<\/p>\n\n\n\n<p>This allows you to perform mathematical operations such as square root, trigonometric functions and many more. By importing a module, you can use its functions and constants, and make use of the functionalities it provides without having to rewrite them.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># Import the built-in math module\nimport math\n\n# Use the sqrt function from the math module\nprint(math.sqrt(9))\n# output : 3.0\n\n# Importing sin function from math module\nfrom math import sin\nprint(sin(90))\n#output : 0.8939...\n\n# Using alias while importing a module\nimport math as m\nprint(m.sqrt(16))\n# output : 4.0\n<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"700\" height=\"660\" src=\"https:\/\/expert-only.com\/wp-content\/uploads\/2023\/01\/python-modules-import-and-call.jpg\" alt=\"Python Modules to import the math library using aliases\" class=\"wp-image-21094\" srcset=\"https:\/\/expert-only.com\/wp-content\/uploads\/2023\/01\/python-modules-import-and-call.jpg 700w, https:\/\/expert-only.com\/wp-content\/uploads\/2023\/01\/python-modules-import-and-call-300x283.jpg 300w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/><figcaption class=\"wp-element-caption\">Python Modules to import the math library using aliases<\/figcaption><\/figure>\n\n\n\n<p>In the first example, we use the <em>import<\/em> statement to import the built-in &#8220;math&#8221; module. Then we can use the <em>sqrt<\/em> function from the math module by calling <em>math.sqrt(9)<\/em>. The output will be 3.0..<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"2-import-specific-functions-from-a-python-module\"><\/span>2. Import specific functions from a Python module<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>You can also import specific functions or variables from a module using the <code>from<\/code> keyword. For example, to import only the <code>sqrt<\/code> function from the math module, you would write <code>from math import sqrt<\/code>. This allows you to call the function directly without having to use the module&#8217;s name as a prefix. This means you can simply write <code>sqrt(4)<\/code> instead of <code>math.sqrt(4)<\/code>. By doing this way, you can import and use only the functions and variable you need, and avoid unnecessary imports.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># Import only the sqrt function from the math module\nfrom math import sqrt\n\n# Use the sqrt function\nprint(sqrt(4))\n# output: 2.0\n\n# Importing multiple functions from the math module\nfrom math import sin, cos\n\nprint(sin(60))\n# output : 0.8660254037844386\n\nprint(cos(45))\n# output : 0.5253219888177297\n\n# Importing variables \nfrom math import pi\nprint(pi)\n# output : 3.141592653589793\n<\/pre>\n\n\n\n<p>The goal when import only specific functions or Python variables, is to save memory and make code more easy to read.<\/p>\n\n\n\n<p>In the first example, we use the <code>from<\/code> keyword to import only the <code>sqrt<\/code> function from the math module. This allows us to call the function directly without having to use the module&#8217;s name as a prefix. so we can simply write <code>sqrt(4)<\/code> instead of <code>math.sqrt(4)<\/code><\/p>\n\n\n\n<p>In the second example, we import multiple functions from the math module using a single line. This allows you to call the functions directly without having to use the module&#8217;s name as a prefix. so we can simply write <code>sin(60)<\/code> and <code>cos(45)<\/code> instead of <code>math.sin(60)<\/code> and <code>math.cos(45)<\/code>.<\/p>\n\n\n\n<p>In the third example, we imported the variable <code>pi<\/code> from math module, which can be used directly without any prefix.<\/p>\n\n\n\n<p>This other tutorial from the Python full course is about the <a href=\"https:\/\/expert-only.com\/en\/python\/user-defined-functions-in-python\/\">user-defined functions in Python<\/a>. <\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed is-provider-sql-and-it-tutorials wp-block-embed-sql-and-it-tutorials\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"4qyaB1AFUr\"><a href=\"https:\/\/expert-only.com\/en\/python\/user-defined-functions-in-python\/\">User-defined functions in Python<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;User-defined functions in Python&#8221; &#8212; SQL and IT Tutorials\" src=\"https:\/\/expert-only.com\/en\/python\/user-defined-functions-in-python\/embed\/#?secret=pt9hIWVFXy#?secret=4qyaB1AFUr\" data-secret=\"4qyaB1AFUr\" width=\"500\" height=\"282\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"3-assign-aliases-to-modules-or-functions-in-python\"><\/span>3. Assign aliases to modules or functions in Python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Python allows you to assign an alias to a module or function when you import it. This can be useful when you have multiple modules or functions with similar or duplicate names, or when you want to shorten the name of a module or function for easier reference.<\/p>\n\n\n\n<p>This is done using the <code>as<\/code> keyword. For example, you could import the <code>math<\/code> module and assign it the alias <code>m<\/code> like this: <code>import math as m<\/code>. Then you can use the module&#8217;s functions by calling them with the alias as a prefix, like this <code>m.sqrt(4)<\/code> instead of <code>math.sqrt(4)<\/code>.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># Import the math module and give it an alias\nimport math as m\n\n# Use the sqrt function from the math module with the alias\nprint(m.sqrt(4)) # output: 2.0\n\n# Use alias while importing a function \nfrom math import sin as sine\nprint(sine(90)) # output : 1.0\n\n<\/pre>\n\n\n\n<p>In the first example, we import the <code>math<\/code> module and give it the alias <code>m<\/code>, so we can use the <code>math<\/code> module functions by calling them with the alias as a prefix, like this <code>m.sqrt(4)<\/code> instead of <code>math.sqrt(4)<\/code>. In the second example, we use the <code>as<\/code> keyword to import only the <code>sin<\/code> function from the math module and giving it an alias <code>sine<\/code>.<\/p>\n\n\n\n<p>This allows you to call the function directly with the alias as a prefix, like this <code>sine(90)<\/code> instead of <code>math.sin(90)<\/code> This way you can use the more meaningful and descriptive names for the functions and variables in your code, while keeping the original names of the imported modules and functions.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"4-access-functions-and-variables-in-python-modules\"><\/span>4. Access functions and variables in Python modules<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In Python, modules can contain variables and functions that can be accessed by other modules. These are called global variables and functions. To access these variables and functions, you can use the module name as a prefix, followed by a dot and the name of the variable or function.<\/p>\n\n\n\n<p>For example, consider a module named <code>mymodule<\/code> that contains the following variable:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">x = 3<\/pre>\n\n\n\n<p>You could access the value of <code>x<\/code> from another module as follows:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import mymodule\nprint(mymodule.x)\n# Output: 3\n<\/pre>\n\n\n\n<p>Similarly, consider a module named <code>mymodule<\/code> that contains the following function:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">def print_hello():\n    print(\"Hello, World!\")<\/pre>\n\n\n\n<p>You could call the <code>print_hello<\/code> function from another module as follows:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import mymodule\nmymodule.print_hello() # Output: \"Hello, World!\"<\/pre>\n\n\n\n<p>It&#8217;s important to note that global variables and functions are not visible to the modules that import them by default, it has to be explicitly imported using the import statement.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"5-create-and-use-custom-python-modules\"><\/span>5. Create and use custom Python modules<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In addition to the built-in modules that come with Python, you can also create your own modules by writing your own Python scripts and saving them with the <code>.py<\/code> file extension. To use your own module in another script, you&#8217;ll need to put the module file in the same directory as the script that imports it, or in a directory listed in the <code>sys.path<\/code> list.<\/p>\n\n\n\n<p>Here is an example of a custom module <code>mymodule.py<\/code>:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">def print_hello():\n    print(\"Hello, World!\")\n\ndef print_goodbye():\n    print(\"Goodbye, World!\")\n<\/pre>\n\n\n\n<p>And an example of how to use it in another Python script,  this one prints two message in the output screen: <\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Hello, World!<\/li>\n\n\n\n<li>Goodbye, World!<\/li>\n<\/ul>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import mymodule\n\nmymodule.print_hello()\n# Output: \"Hello, World!\"\n\nmymodule.print_goodbye()\n# Output: \"Goodbye, World!\"\n<\/pre>\n\n\n\n<p>In case your custom module is in different directory from the script, you will have to use <code>sys.path<\/code> to add the directory to the path list.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import sys\nsys.path.append(\"\/path\/to\/your\/module\")\n\nimport mymodule\n\nmymodule.print_hello() # Output: \"Hello, World!\"\nmymodule.print_goodbye() # Output: \"Goodbye, World!\"\n<\/pre>\n\n\n\n<p>It&#8217;s important to note that naming convention in python is to use all lowercase letters, and to use _ as word delimiter, it&#8217;s a good practice to follow it to prevent any naming conflicts when using multiple modules.<\/p>\n\n\n\n<p>This other tutorial presents <a href=\"https:\/\/expert-only.com\/en\/python\/python-data-types\/\">the multiple data types available with Python<\/a> programming language.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed is-provider-sql-and-it-tutorials wp-block-embed-sql-and-it-tutorials\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"tmw3gqtPNV\"><a href=\"https:\/\/expert-only.com\/en\/python\/python-data-types\/\">Python data types with code examples<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;Python data types with code examples&#8221; &#8212; SQL and IT Tutorials\" src=\"https:\/\/expert-only.com\/en\/python\/python-data-types\/embed\/#?secret=I6PhIihmN8#?secret=tmw3gqtPNV\" data-secret=\"tmw3gqtPNV\" width=\"500\" height=\"282\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"6-how-to-create-and-use-packages-in-python\"><\/span>6. How to create and use packages in Python?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>A Python package is a way to organize related modules together. To create a package, follow these steps: <\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>First create a <strong>directory<\/strong>.<\/li>\n\n\n\n<li>Include an <strong>__init__.py<\/strong> file within the directory.<\/li>\n<\/ol>\n\n\n\n<p>The presence of the <code>__init__.py<\/code> file indicates that the directory should be treated as a package by Python. Here is an example package directory structure that contains 2 Python modules.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">myfirstpackage\/\n    __init__.py\n    module1.py\n    module2.py\n<\/pre>\n\n\n\n<p>The <code>__init__.py<\/code> file can be used to initialize the package or to define some variables or functions which will be shared across all modules of the package.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># __init__.py\nshared_variable = \"This is a shared variable\"\n\ndef print_hello():\n    print(\"Hello from mypackage\")\n<\/pre>\n\n\n\n<p>When package is imported, <code>__init__.py<\/code> is executed, in this way you can share variables and functions across all modules of package.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import mypackage\n\nprint(mypackage.shared_variable)\n# Output: \"This is a shared variable\"\n\nmypackage.print_hello()\n# Output: \"Hello from mypackage\"\n<\/pre>\n\n\n\n<p>It is also possible to import a specific module and use variables and functions defined in it, like in the code below.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">from mypackage import module1\n\nmodule1.print_hello()\n# Output: \"Hello from module1\"\n<\/pre>\n\n\n\n<p>It&#8217;s important to note that the <strong>init.py<\/strong> file can be empty as well, it serves only as a marker for python to recognize the directory as a package.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"7-document-python-modules-with-docstrings\"><\/span>7. Document Python modules with docstrings<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>A module in Python can have a <strong>docstring<\/strong>, which is a string that describes what the module does. Docstrings can be a useful tool for documentation and can be accessed by other developers to understand the purpose and functionality of a module.<\/p>\n\n\n\n<p>The docstring is placed as the first statement in a module and is accessible using the <code><strong>__doc__<\/strong><\/code> attribute. Here&#8217;s an example of a module with a docstring:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\"\"\"\nThis module contains functions for performing mathematical operations.\n\"\"\"\n\ndef add(a, b):\n    \"\"\"\n    Add two numbers\n    \"\"\"\n    return a + b\n\ndef multiply(a, b):\n    \"\"\"\n    Multiply two numbers\n    \"\"\"\n    return a * b\n<\/pre>\n\n\n\n<p>You can access the docstring using the <code>__doc__<\/code> attribute, for example, in the script below, we display the documentation of the math_operations module.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import math_operations\nprint(math_operations.__doc__)\n# Output: \n# This module contains functions for performing mathematical operations.\n<\/pre>\n\n\n\n<p>You can also access the docstring of a given function, to do so, add the function name inside the print function, like this.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">print(math_operations.add.__doc__)\n# Output:\n#    Add two numbers\n<\/pre>\n\n\n\n<p>It&#8217;s good practice in <a href=\"https:\/\/expert-only.com\/en\/python\/introduction-to-python-programming\/\">Python programming<\/a> to use docstring and provide clear and comprehensive documentation in your modules. This will make it easier for other developers to understand and use your code.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed is-provider-sql-and-it-tutorials wp-block-embed-sql-and-it-tutorials\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"m6xWbl6SiV\"><a href=\"https:\/\/expert-only.com\/en\/python\/introduction-to-python-programming\/\">Introduction to Python programming language<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;Introduction to Python programming language&#8221; &#8212; SQL and IT Tutorials\" src=\"https:\/\/expert-only.com\/en\/python\/introduction-to-python-programming\/embed\/#?secret=FEqGw3iI3d#?secret=m6xWbl6SiV\" data-secret=\"m6xWbl6SiV\" width=\"500\" height=\"282\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"8-execute-statements-in-python-modules\"><\/span>8. Execute statements in Python modules<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Python modules can also include executable statements, which are executed when the module is first imported. These statements are typically used to define global variables and functions, or to perform other one-time setup tasks. The following script example use a module with executable statements. <\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">x = 3\ndef print_hello():\n    print(\"Hello, World!\")\n\nprint(\"Module is being imported\")\n<\/pre>\n\n\n\n<p>When this module is imported for the first time, the statements <code>x = 3<\/code>, <code>def print_hello():<\/code> and <code>print(\"Module is being imported\")<\/code> will be executed. The variable <code>x<\/code> and the function <code>print_hello<\/code> will be available for use, and the string &#8220;Module is being imported&#8221; will be printed.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import mymodule\n\nprint(mymodule.x)\n# Output: 3\n\nmymodule.print_hello()\n# Output: \"Hello, World!\"\n<\/pre>\n\n\n\n<p>It&#8217;s important to note that these statements are executed only once, when the module is imported for the first time, not every time when it&#8217;s imported later. It&#8217;s also good practice to keep these statements separate from the function and class definitions to make the code more readable and maintainable.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"9-define-classes-in-python-modules\"><\/span>9. Define classes in Python modules<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Python modules can also include classes, which can be used to create objects of a particular type. For example, you might create a class in a module that represents a particular type of data, such as a Customer record or a Person record. This module uses a script that defines a class called <code>Customer<\/code>. The Customer class below can manage these 7 fields: <\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>First name<\/li>\n\n\n\n<li>Last name<\/li>\n\n\n\n<li>Address<\/li>\n\n\n\n<li>Phone<\/li>\n\n\n\n<li>Email<\/li>\n\n\n\n<li>Profession<\/li>\n\n\n\n<li>Purchases (in the form of a Python list)<\/li>\n<\/ol>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">class Customer:\n    def __init__(self, first_name, last_name, address, phone, email, profession):\n        self.first_name = first_name\n        self.last_name = last_name\n        self.address = address\n        self.phone = phone\n        self.email = email\n        self.profession = profession\n        self.purchases = []\n        \n    def display_customer_info(self):\n        \"\"\"\n        Display Customer Information\n        \"\"\"\n        print(f\"Name: {self.first_name} {self.last_name}\")\n        print(f\"Profession: {self.profession}\")\n        print(f\"Address: {self.address}\")\n        print(f\"Phone: {self.phone}\")\n        print(f\"Email: {self.email}\")\n        if self.purchases:\n            print(\"Purchases:\")\n            for purchase in self.purchases:\n                print(purchase)\n        else:\n            print(\"No purchases made yet\")\n    \n    def update_address(self, new_address):\n        \"\"\"\n        update the customer address\n        \"\"\"\n        self.address = new_address\n        \n    def make_purchase(self, item):\n        \"\"\"\n        make purchase and add to customer's purchase history\n        \"\"\"\n        self.purchases.append(item)\n<\/pre>\n\n\n\n<p>Once a class has been defined in a module, you can create objects of that class using the class&#8217;s constructor and use the methods associated with the <strong><em>Customer<\/em><\/strong> class.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import mymodule\ncust = mymodule.Customer(\"John\", \"Smith\", \"1st Ave, NY\", \"555-555-5555\", \"johnsmith@example.com\")\ncust.display_customer_info()\n# Output:\n# Name: John Smith\n# Address: 1st Ave, NY\n# Phone: 555-555-5555\n# Email: johnsmith@example.com\n\ncust.update_address(\"2nd Ave, NY\")\ncust.display_customer_info()\n# Output:\n# Name: John Smith\n# Address: 2nd Ave, NY\n# Phone: 555-555-5555\n# Email: johnsmith@example.com\n<\/pre>\n\n\n\n<p>It&#8217;s a good practice in Python and in <a href=\"https:\/\/www.partech.nl\/en\/publications\/2020\/10\/basic-principles-of-object-oriented-programming\" target=\"_blank\" rel=\"noreferrer noopener\">OO Programming<\/a> to keep the classes and modules related to each other. And also use the class name as the module name, that way it&#8217;s easy to understand what classes a module contains and also easy to locate the module while importing.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"10-use-the-main-block-in-modules\"><\/span>10. Use the main block in modules<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Python modules can include a special block of code, called the <code>if __name__ == \"__main__\"<\/code> block. This block of code will only be executed if the module is run as the main script. This can be useful for testing purposes, or for providing a way to run the module as a standalone script.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">def my_function():\n    print(\"This is my function\")\n\nif __name__ == \"__main__\":\n    my_function()\n<\/pre>\n\n\n\n<p>The <code>if __name__ == \"__main__\"<\/code> block is an incredibly useful tool for any Python developer. It allows you to easily check whether your module is being run as a standalone script or being imported as a module in another script. If the module is being run as a standalone script, the function inside the block will be called and executed.<\/p>\n\n\n\n<p>But if the same module is imported in another script, the block will not be executed, allowing you to separate your code for different purposes. This means you can use the <code>if __name__ == \"__main__\"<\/code> block to test your modules separately, without having to run your whole project.<\/p>\n\n\n\n<p>It&#8217;s also great for creating standalone scripts that can run independently of other scripts in your project. It&#8217;s an elegant solution that ensures that your code runs exactly as it should, depending on the context it&#8217;s being used in. It&#8217;s a simple but powerful technique that can really improve the organization and maintainability of your code.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"conclusion-on-working-with-python-modules\"><\/span>Conclusion on working with Python modules<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>That is for this interesting tutorial on how to work with Python modules. We have seen some of the powerful possibilities with examples of scripts to create and use modules.<\/p>\n\n\n\n<p>In conclusion, Python modules are a powerful tool for organizing and structuring your code. They allow you to separate different functionality into different files, making your code more manageable and easier to understand. We&#8217;ve covered the <strong>basics of importing and using modules<\/strong>, as well as some of the advanced features such as <strong>importing specific Python functions<\/strong>, giving modules aliases, <strong>accessing global variables and functions<\/strong>, and using condition on <strong>the block named main<\/strong>.<\/p>\n\n\n\n<p>The ability to import and use pre-built modules can save a lot of time and effort, as it allows you to take advantage of the work that others have already done. It also makes it easy to share and reuse your own code, by packaging it into a module that others can import and use in their own projects.<\/p>\n\n\n\n<p>Furthermore, understanding the <strong>concept of classes and objects<\/strong>, and how to create and use them in Python modules, can help you create more powerful and organized programs.<\/p>\n\n\n\n<p>All in all, Python modules are a fundamental part of the language, and mastering their use is an important step in becoming a proficient Python developer. With the knowledge you have gained from this tutorial, you can now start building more complex and <strong>powerful Python programs<\/strong> with confidence and enthusiasm. Good luck!<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Popular MS tutorials from the blog (Power BI, T-SQL and MS-DOS)<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/expert-only.com\/en\/power-bi\/last-year-value-dax-power-bi\/\">Calculate the last year value in Power BI with DAX<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/expert-only.com\/en\/t-sql\/calculate-difference-between-two-dates\/\">Calculate the difference between two dates with SQL Server<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/expert-only.com\/en\/t-sql\/insert-or-remove-sql-server-line-breaks\/\">Insert or remove SQL Server line breaks from text<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/expert-only.com\/en\/ms-dos\/rename-multiple-windows-files\/\">Rename multiple Windows files at the same time<\/a><\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed is-provider-sql-and-it-tutorials wp-block-embed-sql-and-it-tutorials\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"nG7jtuCKTF\"><a href=\"https:\/\/expert-only.com\/en\/ms-dos\/rename-multiple-windows-files\/\">How to batch rename multiple files with Windows cmd?<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;How to batch rename multiple files with Windows cmd?&#8221; &#8212; SQL and IT Tutorials\" src=\"https:\/\/expert-only.com\/en\/ms-dos\/rename-multiple-windows-files\/embed\/#?secret=yy0ySkmNg1#?secret=nG7jtuCKTF\" data-secret=\"nG7jtuCKTF\" width=\"500\" height=\"282\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>Tutorial on how to use modules in Python like import, create, document and define classes with scripts examples. This part of the Python course is about the Python modules. A module in Python is simply a file that contains <a class=\"mh-excerpt-more\" href=\"https:\/\/expert-only.com\/en\/python\/python-modules\/\" title=\"Python Modules\">&#8230;<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":6400,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[657],"tags":[],"class_list":{"0":"post-21010","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-python"},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v21.7 (Yoast SEO v26.2) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Python Modules - Code examples - Python tutorials<\/title>\n<meta name=\"description\" content=\"Step by step tutorial on how to use modules in Python like import, create, document and define classes with scripts examples.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/expert-only.com\/en\/python\/python-modules\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Modules\" \/>\n<meta property=\"og:description\" content=\"Step by step tutorial on how to use modules in Python like import, create, document and define classes with scripts examples.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/expert-only.com\/en\/python\/python-modules\/\" \/>\n<meta property=\"og:site_name\" content=\"SQL and IT Tutorials\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/ExpertOnlyCom\/\" \/>\n<meta property=\"article:published_time\" content=\"2022-12-13T05:45:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-01-11T17:16:58+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/expert-only.com\/wp-content\/uploads\/2020\/07\/fresh-glass-coffee-cup-2D256C64A99_1920x1080.jpeg\" \/>\n\t<meta property=\"og:image:width\" content=\"1920\" \/>\n\t<meta property=\"og:image:height\" content=\"1080\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Expert-Only\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@expert_only\" \/>\n<meta name=\"twitter:site\" content=\"@expert_only\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Expert-Only\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"12 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/expert-only.com\/en\/python\/python-modules\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/expert-only.com\/en\/python\/python-modules\/\"},\"author\":{\"name\":\"Expert-Only\",\"@id\":\"https:\/\/expert-only.com\/en\/#\/schema\/person\/406a9576b52944f018739a42046873ef\"},\"headline\":\"Python Modules\",\"datePublished\":\"2022-12-13T05:45:00+00:00\",\"dateModified\":\"2023-01-11T17:16:58+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/expert-only.com\/en\/python\/python-modules\/\"},\"wordCount\":2116,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/expert-only.com\/en\/#organization\"},\"image\":{\"@id\":\"https:\/\/expert-only.com\/en\/python\/python-modules\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2020\/07\/fresh-glass-coffee-cup-2D256C64A99_1920x1080.jpeg\",\"articleSection\":[\"Python tutorials\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/expert-only.com\/en\/python\/python-modules\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/expert-only.com\/en\/python\/python-modules\/\",\"url\":\"https:\/\/expert-only.com\/en\/python\/python-modules\/\",\"name\":\"Python Modules - Code examples - Python tutorials\",\"isPartOf\":{\"@id\":\"https:\/\/expert-only.com\/en\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/expert-only.com\/en\/python\/python-modules\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/expert-only.com\/en\/python\/python-modules\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2020\/07\/fresh-glass-coffee-cup-2D256C64A99_1920x1080.jpeg\",\"datePublished\":\"2022-12-13T05:45:00+00:00\",\"dateModified\":\"2023-01-11T17:16:58+00:00\",\"description\":\"Step by step tutorial on how to use modules in Python like import, create, document and define classes with scripts examples.\",\"breadcrumb\":{\"@id\":\"https:\/\/expert-only.com\/en\/python\/python-modules\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/expert-only.com\/en\/python\/python-modules\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/expert-only.com\/en\/python\/python-modules\/#primaryimage\",\"url\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2020\/07\/fresh-glass-coffee-cup-2D256C64A99_1920x1080.jpeg\",\"contentUrl\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2020\/07\/fresh-glass-coffee-cup-2D256C64A99_1920x1080.jpeg\",\"width\":1920,\"height\":1080},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/expert-only.com\/en\/python\/python-modules\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"en\",\"item\":\"https:\/\/expert-only.com\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python Modules\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/expert-only.com\/en\/#website\",\"url\":\"https:\/\/expert-only.com\/en\/\",\"name\":\"SQL and IT Tutorials\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/expert-only.com\/en\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/expert-only.com\/en\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/expert-only.com\/en\/#organization\",\"name\":\"Expert-Only\",\"url\":\"https:\/\/expert-only.com\/en\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/expert-only.com\/en\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2017\/09\/cropped-logo_Expert-Only.jpg\",\"contentUrl\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2017\/09\/cropped-logo_Expert-Only.jpg\",\"width\":381,\"height\":174,\"caption\":\"Expert-Only\"},\"image\":{\"@id\":\"https:\/\/expert-only.com\/en\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/ExpertOnlyCom\/\",\"https:\/\/x.com\/expert_only\",\"https:\/\/www.youtube.com\/channel\/UCMS5sR_FwAetB0FmciNvUaA\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/expert-only.com\/en\/#\/schema\/person\/406a9576b52944f018739a42046873ef\",\"name\":\"Expert-Only\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/expert-only.com\/en\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/084b15660763ff5b13bb60b2f52f97bb?s=96&d=identicon&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/084b15660763ff5b13bb60b2f52f97bb?s=96&d=identicon&r=g\",\"caption\":\"Expert-Only\"}}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Python Modules - Code examples - Python tutorials","description":"Step by step tutorial on how to use modules in Python like import, create, document and define classes with scripts examples.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/expert-only.com\/en\/python\/python-modules\/","og_locale":"en_US","og_type":"article","og_title":"Python Modules","og_description":"Step by step tutorial on how to use modules in Python like import, create, document and define classes with scripts examples.","og_url":"https:\/\/expert-only.com\/en\/python\/python-modules\/","og_site_name":"SQL and IT Tutorials","article_publisher":"https:\/\/www.facebook.com\/ExpertOnlyCom\/","article_published_time":"2022-12-13T05:45:00+00:00","article_modified_time":"2023-01-11T17:16:58+00:00","og_image":[{"width":1920,"height":1080,"url":"https:\/\/expert-only.com\/wp-content\/uploads\/2020\/07\/fresh-glass-coffee-cup-2D256C64A99_1920x1080.jpeg","type":"image\/jpeg"}],"author":"Expert-Only","twitter_card":"summary_large_image","twitter_creator":"@expert_only","twitter_site":"@expert_only","twitter_misc":{"Written by":"Expert-Only","Est. reading time":"12 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/expert-only.com\/en\/python\/python-modules\/#article","isPartOf":{"@id":"https:\/\/expert-only.com\/en\/python\/python-modules\/"},"author":{"name":"Expert-Only","@id":"https:\/\/expert-only.com\/en\/#\/schema\/person\/406a9576b52944f018739a42046873ef"},"headline":"Python Modules","datePublished":"2022-12-13T05:45:00+00:00","dateModified":"2023-01-11T17:16:58+00:00","mainEntityOfPage":{"@id":"https:\/\/expert-only.com\/en\/python\/python-modules\/"},"wordCount":2116,"commentCount":0,"publisher":{"@id":"https:\/\/expert-only.com\/en\/#organization"},"image":{"@id":"https:\/\/expert-only.com\/en\/python\/python-modules\/#primaryimage"},"thumbnailUrl":"https:\/\/expert-only.com\/wp-content\/uploads\/2020\/07\/fresh-glass-coffee-cup-2D256C64A99_1920x1080.jpeg","articleSection":["Python tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/expert-only.com\/en\/python\/python-modules\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/expert-only.com\/en\/python\/python-modules\/","url":"https:\/\/expert-only.com\/en\/python\/python-modules\/","name":"Python Modules - Code examples - Python tutorials","isPartOf":{"@id":"https:\/\/expert-only.com\/en\/#website"},"primaryImageOfPage":{"@id":"https:\/\/expert-only.com\/en\/python\/python-modules\/#primaryimage"},"image":{"@id":"https:\/\/expert-only.com\/en\/python\/python-modules\/#primaryimage"},"thumbnailUrl":"https:\/\/expert-only.com\/wp-content\/uploads\/2020\/07\/fresh-glass-coffee-cup-2D256C64A99_1920x1080.jpeg","datePublished":"2022-12-13T05:45:00+00:00","dateModified":"2023-01-11T17:16:58+00:00","description":"Step by step tutorial on how to use modules in Python like import, create, document and define classes with scripts examples.","breadcrumb":{"@id":"https:\/\/expert-only.com\/en\/python\/python-modules\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/expert-only.com\/en\/python\/python-modules\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/expert-only.com\/en\/python\/python-modules\/#primaryimage","url":"https:\/\/expert-only.com\/wp-content\/uploads\/2020\/07\/fresh-glass-coffee-cup-2D256C64A99_1920x1080.jpeg","contentUrl":"https:\/\/expert-only.com\/wp-content\/uploads\/2020\/07\/fresh-glass-coffee-cup-2D256C64A99_1920x1080.jpeg","width":1920,"height":1080},{"@type":"BreadcrumbList","@id":"https:\/\/expert-only.com\/en\/python\/python-modules\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"en","item":"https:\/\/expert-only.com\/en\/"},{"@type":"ListItem","position":2,"name":"Python Modules"}]},{"@type":"WebSite","@id":"https:\/\/expert-only.com\/en\/#website","url":"https:\/\/expert-only.com\/en\/","name":"SQL and IT Tutorials","description":"","publisher":{"@id":"https:\/\/expert-only.com\/en\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/expert-only.com\/en\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/expert-only.com\/en\/#organization","name":"Expert-Only","url":"https:\/\/expert-only.com\/en\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/expert-only.com\/en\/#\/schema\/logo\/image\/","url":"https:\/\/expert-only.com\/wp-content\/uploads\/2017\/09\/cropped-logo_Expert-Only.jpg","contentUrl":"https:\/\/expert-only.com\/wp-content\/uploads\/2017\/09\/cropped-logo_Expert-Only.jpg","width":381,"height":174,"caption":"Expert-Only"},"image":{"@id":"https:\/\/expert-only.com\/en\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/ExpertOnlyCom\/","https:\/\/x.com\/expert_only","https:\/\/www.youtube.com\/channel\/UCMS5sR_FwAetB0FmciNvUaA"]},{"@type":"Person","@id":"https:\/\/expert-only.com\/en\/#\/schema\/person\/406a9576b52944f018739a42046873ef","name":"Expert-Only","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/expert-only.com\/en\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/084b15660763ff5b13bb60b2f52f97bb?s=96&d=identicon&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/084b15660763ff5b13bb60b2f52f97bb?s=96&d=identicon&r=g","caption":"Expert-Only"}}]}},"_links":{"self":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts\/21010","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/comments?post=21010"}],"version-history":[{"count":0,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts\/21010\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/media\/6400"}],"wp:attachment":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/media?parent=21010"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/categories?post=21010"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/tags?post=21010"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}