{"id":125,"date":"2024-01-25T11:27:07","date_gmt":"2024-01-25T11:27:07","guid":{"rendered":"https:\/\/learnpython.elegantwallp.com\/?p=125"},"modified":"2024-01-25T11:27:08","modified_gmt":"2024-01-25T11:27:08","slug":"python-modules","status":"publish","type":"post","link":"https:\/\/learnpython.elegantwallp.com\/2024\/01\/25\/python-modules\/","title":{"rendered":"Python Modules"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you\u2019ll learn about Python modules, how to import objects from a module, and how to develop your modules.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to Python modules<\/h2>\n\n\n\n<p>A module is a piece of software that has a specific functionality. A Python module is a file that contains Python code.<\/p>\n\n\n\n<p>For example, when building a shopping cart application, you can have one module for calculating prices and another module for managing items in the cart. Each module is a separate Python source code file.<\/p>\n\n\n\n<p>A module has a name specified by the filename without the&nbsp;<code>.py<\/code>&nbsp;extension. For example, if you have a file called&nbsp;<code>pricing.py<\/code>, the module name is&nbsp;<code>pricing<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Writing Python modules<\/h2>\n\n\n\n<p>First, create a new file called\u00a0<code>pricing.py<\/code>\u00a0and add the following code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code><em># pricing.py<\/em> def get_net_price(price, tax_rate, discount=0): return price * (1 + tax_rate) * (1-discount) def get_tax(price, tax_rate=0): return price * tax_rate<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The pricing module has two functions that calculate the net price and tax from the selling price, tax rate, and discount.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Importing module objects<\/h2>\n\n\n\n<p>To use objects defined in a module from another file, you can use the&nbsp;<code>import<\/code>&nbsp;statement.<\/p>\n\n\n\n<p>The&nbsp;<code>import<\/code>&nbsp;statement has several forms that we will discuss in the next sections.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1) import &lt;module_name&gt;<\/h3>\n\n\n\n<p>To use objects defined in a module, you need to import the module using the following\u00a0<code>import<\/code>\u00a0statement:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>import module_name<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>For example, to use the&nbsp;<code>pricing<\/code>&nbsp;module in the&nbsp;<code>main.py<\/code>&nbsp;file, you use the following statement:<code>import pricing<\/code><small>Code language: Python (python)<\/small><\/p>\n\n\n\n<p>When you import a module, Python executes all the code from the corresponding file. In this example, Python executes the code from the&nbsp;<code>pricing.py<\/code>&nbsp;file. Also, Python adds the module name to the current module.<\/p>\n\n\n\n<p>This module name allows you to access functions, variables, etc. from the imported module in the current module. For example, you can call a\u00a0function\u00a0defined in the imported module using the following syntax:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>module_name.function_name()<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The following shows how to use the\u00a0<code>get_net_price()<\/code>\u00a0function defined in the\u00a0<code>pricing<\/code>\u00a0module in the\u00a0<code>main.py<\/code>\u00a0file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code><em># main.py<\/em> import pricing net_price = pricing.get_net_price( price=100, tax_rate=0.01 ) print(net_price)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>101.0<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2) import &lt;module_name&gt; as new_name<\/h3>\n\n\n\n<p>If you don\u2019t want to use the\u00a0<code>pricing<\/code>\u00a0as the identifier in the\u00a0<code>main.py<\/code>, you can rename the module name to another as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>import pricing as selling_price<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>And then you can use the\u00a0<code>selling_price<\/code>\u00a0as the identifier in the\u00a0<code>main.py<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>net_price = selling_price.get_net_price( price=100, tax_rate=0.01 )<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3) from &lt;module_name&gt; import &lt;name&gt;<\/h3>\n\n\n\n<p>If you want to reference objects in a module without prefixing the module name, you can explicitly import them using the following syntax:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>from module_name import fn1, fn2<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Now, you can use the imported functions without specifying the module name like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>fn1() fn2()<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>This example imports the\u00a0<code>get_net_price()<\/code>\u00a0function from the\u00a0<code>pricing<\/code>\u00a0module:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code><em># main.py<\/em> from pricing import get_net_price<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>and use the\u00a0<code>get_net_price()<\/code>\u00a0function from the\u00a0<code>pricing<\/code>\u00a0module:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code><em># main.py<\/em> from pricing import get_net_price net_price = get_net_price(price=100, tax_rate=0.01) print(net_price)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">4) from &lt;module_name&gt; import &lt;name&gt; as &lt;new_name&gt;: rename the imported objects<\/h3>\n\n\n\n<p>It\u2019s possible to rename an imported name to another by using the following\u00a0<code>import<\/code>\u00a0statement:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>from &lt;module_name> import &lt;name> as &lt;new_name><\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The following example renames the\u00a0<code>get_net_price()<\/code>\u00a0function from the\u00a0<code>pricing<\/code>\u00a0module to\u00a0<code>calculate_net_price()<\/code>\u00a0function:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>from pricing import get_net_price as calculate_net_price net_price = calculate_net_price( price=100, tax_rate=0.1, discount=0.05 )<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">5) from &lt;module_name&gt; import * : import all objects from a module<\/h3>\n\n\n\n<p>To import every object from a module, you can use the following syntax:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>from module_name import *<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>This\u00a0<code>import<\/code>\u00a0statement will import all public identifiers including\u00a0variables,\u00a0constants,\u00a0functions,\u00a0classes, etc., to the program.<\/p>\n\n\n\n<p>However, it\u2019s not a good practice because if the imported modules have the same object, the object from the second module will override the first one. The program may not work as you would expect.<\/p>\n\n\n\n<p>Let\u2019s see the following example.<\/p>\n\n\n\n<p>First, create a new file called\u00a0<code>product.py<\/code>\u00a0and define the\u00a0<code>get_tax()<\/code>\u00a0function:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code><em># product.py<\/em> def get_tax(price): return price * 0.1<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Both&nbsp;<code>product<\/code>&nbsp;and&nbsp;<code>pricing<\/code>&nbsp;modules have the&nbsp;<code>get_tax()<\/code>&nbsp;function. However, the&nbsp;<code>get_tax()<\/code>&nbsp;function from the&nbsp;<code>product<\/code>&nbsp;module has only one parameter while the&nbsp;<code>get_tax()<\/code>&nbsp;function from the&nbsp;<code>pricing<\/code>&nbsp;module has two parameters.<\/p>\n\n\n\n<p>Second, import all objects from both\u00a0<code>pricing<\/code>\u00a0and\u00a0<code>product<\/code>\u00a0modules and use the\u00a0<code>get_tax()<\/code>\u00a0function:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>from pricing import * from product import * tax = get_tax(100) print(tax)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Since the&nbsp;<code>get_tax()<\/code>&nbsp;function from the&nbsp;<code>product<\/code>&nbsp;module overrides the&nbsp;<code>get_tax<\/code>() function from the&nbsp;<code>pricing<\/code>&nbsp;module, you get the&nbsp;<code>tax<\/code>&nbsp;with a value of 10.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you\u2019ll learn about Python modules, how to import objects from a module, and how to develop your modules. Introduction to Python modules A module is a piece of software that has a specific functionality. A Python module is a file that contains Python code. For example, when building a shopping cart [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23],"tags":[],"class_list":["post-125","post","type-post","status-publish","format-standard","hentry","category-9-modules-packages"],"_links":{"self":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/125","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/comments?post=125"}],"version-history":[{"count":1,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/125\/revisions"}],"predecessor-version":[{"id":126,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/125\/revisions\/126"}],"wp:attachment":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/media?parent=125"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/categories?post=125"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/tags?post=125"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}