{"id":1137,"date":"2024-02-24T10:54:36","date_gmt":"2024-02-24T10:54:36","guid":{"rendered":"https:\/\/learnpython.elegantwallp.com\/?p=1137"},"modified":"2024-02-24T10:54:37","modified_gmt":"2024-02-24T10:54:37","slug":"python-modules-2","status":"publish","type":"post","link":"https:\/\/learnpython.elegantwallp.com\/2024\/02\/24\/python-modules-2\/","title":{"rendered":"Python\u00a0Modules"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">What is a Module?<\/h2>\n\n\n\n<p>Consider a module to be the same as a code library.<\/p>\n\n\n\n<p>A file containing a set of functions you want to include in your application.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Create a Module<\/h2>\n\n\n\n<p>To create a module just save the code you want in a file with the file extension&nbsp;<code>.py<\/code>:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">ExampleGet your own Python Server<\/h3>\n\n\n\n<p>Save this code in a file named&nbsp;<code>mymodule.py<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def&nbsp;greeting(name):<br>&nbsp;&nbsp;print(\"Hello, \"&nbsp;+ name)<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Use a Module<\/h2>\n\n\n\n<p>Now we can use the module we just created, by using the&nbsp;<code>import<\/code>&nbsp;statement:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<p>Import the module named mymodule, and call the greeting function:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import\u00a0mymodule<br><br>mymodule.greeting(\"Jonathan\")<\/code><\/pre>\n\n\n\n<p><strong>Note:<\/strong>&nbsp;When using a function from a module, use the syntax:&nbsp;<em>module_name.function_name<\/em>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Variables in Module<\/h2>\n\n\n\n<p>The module can contain functions, as already described, but also variables of all types (arrays, dictionaries, objects etc):<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<p>Save this code in the file&nbsp;<code>mymodule.py<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>person1 = {<br>&nbsp;&nbsp;\"name\":&nbsp;\"John\",<br>&nbsp;&nbsp;\"age\":&nbsp;36,<br>&nbsp;&nbsp;\"country\":&nbsp;\"Norway\"<br>}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<p>Import the module named mymodule, and access the person1 dictionary:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import\u00a0mymodule<br><br>a = mymodule.person1&#91;\"age\"]<br>print(a)<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Naming a Module<\/h2>\n\n\n\n<p>You can name the module file whatever you like, but it must have the file extension&nbsp;<code>.py<\/code><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Re-naming a Module<\/h2>\n\n\n\n<p>You can create an alias when you import a module, by using the&nbsp;<code>as<\/code>&nbsp;keyword:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<p>Create an alias for&nbsp;<code>mymodule<\/code>&nbsp;called&nbsp;<code>mx<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import&nbsp;mymodule&nbsp;as&nbsp;mx<br><br>a = mx.person1&#91;\"age\"]<br>print(a)<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Built-in Modules<\/h2>\n\n\n\n<p>There are several built-in modules in Python, which you can import whenever you like.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<p>Import and use the&nbsp;<code>platform<\/code>&nbsp;module:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import\u00a0platform\n\nx = platform.system()\nprint(x)\n\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Using the dir() Function<\/h2>\n\n\n\n<p>There is a built-in function to list all the function names (or variable names) in a module. The&nbsp;<code>dir()<\/code>&nbsp;function:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<p>List all the defined names belonging to the platform module:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import\u00a0platform<br><br>x =\u00a0dir(platform)<br>print(x)<\/code><\/pre>\n\n\n\n<p><strong>Note:<\/strong>&nbsp;The dir() function can be used on&nbsp;<em>all<\/em>&nbsp;modules, also the ones you create yourself.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Import From Module<\/h2>\n\n\n\n<p>You can choose to import only parts from a module, by using the&nbsp;<code>from<\/code>&nbsp;keyword.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<p>The module named&nbsp;<code>mymodule<\/code>&nbsp;has one function and one dictionary:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def&nbsp;greeting(name):<br>&nbsp;&nbsp;print(\"Hello, \"&nbsp;+ name)<br><br>person1&nbsp;= {<br>&nbsp;&nbsp;\"name\":&nbsp;\"John\",<br>&nbsp;&nbsp;\"age\":&nbsp;36,<br>&nbsp;&nbsp;\"country\":&nbsp;\"Norway\"<br>}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<p>Import only the person1 dictionary from the module:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from\u00a0mymodule\u00a0import\u00a0person1<br><br>print\u00a0(person1&#91;\"age\"])<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>What is a Module? Consider a module to be the same as a code library. A file containing a set of functions you want to include in your application. Create a Module To create a module just save the code you want in a file with the file extension&nbsp;.py: ExampleGet your own Python Server Save [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[107],"tags":[],"class_list":["post-1137","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/1137","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=1137"}],"version-history":[{"count":1,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/1137\/revisions"}],"predecessor-version":[{"id":1138,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/1137\/revisions\/1138"}],"wp:attachment":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/media?parent=1137"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/categories?post=1137"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/tags?post=1137"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}