{"id":94,"date":"2026-03-02T07:20:30","date_gmt":"2026-03-02T02:20:30","guid":{"rendered":"https:\/\/gigz.pk\/python\/?post_type=lesson&#038;p=94"},"modified":"2026-03-14T09:35:06","modified_gmt":"2026-03-14T04:35:06","slug":"creating-custom-modules","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/python\/lesson\/creating-custom-modules\/","title":{"rendered":"Creating Custom Modules"},"content":{"rendered":"\n<p>A custom module is a Python file (<code>.py<\/code>) that contains functions, variables, or classes which can be reused in other programs.<\/p>\n\n\n\n<p>Creating your own modules helps you:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Organize large programs<\/li>\n\n\n\n<li>Reuse code easily<\/li>\n\n\n\n<li>Improve readability<\/li>\n\n\n\n<li>Build professional projects<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>STEP 1: CREATE A MODULE FILE<\/strong><\/h2>\n\n\n\n<p>Create a new Python file, for example:<\/p>\n\n\n\n<p><strong>my_module.py<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">def greet(name):<br>    return \"Hello \" + namedef add(a, b):<br>    return a + b<\/pre>\n\n\n\n<p>This file is now a custom module.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>STEP 2: IMPORT THE MODULE<\/strong><\/h2>\n\n\n\n<p>In another Python file (for example <code>main.py<\/code>):<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import my_moduleprint(my_module.greet(\"Hira\"))<br>print(my_module.add(5, 3))<\/pre>\n\n\n\n<p>Both files must be in the same folder.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>IMPORT SPECIFIC FUNCTIONS<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">from my_module import greetprint(greet(\"Ali\"))<\/pre>\n\n\n\n<p>Now you don\u2019t need to write <code>my_module.greet()<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>USING ALIAS<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">import my_module as mmprint(mm.add(10, 20))<\/pre>\n\n\n\n<p>Alias makes long names shorter.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>ADDING VARIABLES IN MODULE<\/strong><\/h2>\n\n\n\n<p>You can also define variables inside a module.<\/p>\n\n\n\n<p><strong>my_module.py<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">pi_value = 3.14<\/pre>\n\n\n\n<p>Use it like this:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import my_moduleprint(my_module.pi_value)<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>USING MAIN CHECK (BEST PRACTICE)<\/strong><\/h2>\n\n\n\n<p>To prevent code from running automatically when imported:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">def greet():<br>    print(\"Hello!\")if __name__ == \"__main__\":<br>    greet()<\/pre>\n\n\n\n<p>This ensures the function runs only when the file is executed directly.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>MODULE SEARCH PATH<\/strong><\/h2>\n\n\n\n<p>Python looks for modules:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>In the current folder<\/li>\n\n\n\n<li>In system library folders<\/li>\n\n\n\n<li>In installed packages<\/li>\n<\/ul>\n\n\n\n<p>You can check path using:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import sys<br>print(sys.path)<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>BENEFITS OF CUSTOM MODULES<\/strong><\/h2>\n\n\n\n<p>\u2022 Code reusability<br>\u2022 Better structure<br>\u2022 Easier maintenance<br>\u2022 Professional project organization<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>KEY TAKEAWAYS<\/strong><\/h2>\n\n\n\n<p>\u2022 A module is simply a <code>.py<\/code> file<br>\u2022 Use <code>import module_name<\/code> to use it<br>\u2022 Keep modules in the same directory<br>\u2022 Use <code>if __name__ == \"__main__\"<\/code> for best practice<\/p>\n\n\n\n<p>Creating custom modules is an important step toward building large and well-structured Python applications.<\/p>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/python\/\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">PYTHON INTERMEDIATE (PYI) > Modules and Packages > Creating Custom Modules<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1773463032757\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n","protected":false},"menu_order":42,"template":"","class_list":["post-94","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Creating Custom Modules - One Language. Endless Possibilities<\/title>\n<meta name=\"description\" content=\"Learn how to create and use custom Python modules for reusable functions, variables, and organized, professional projects efficiently\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gigz.pk\/python\/lesson\/creating-custom-modules\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Creating Custom Modules - One Language. Endless Possibilities\" \/>\n<meta property=\"og:description\" content=\"Learn how to create and use custom Python modules for reusable functions, variables, and organized, professional projects efficiently\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/python\/lesson\/creating-custom-modules\/\" \/>\n<meta property=\"og:site_name\" content=\"One Language. Endless Possibilities\" \/>\n<meta property=\"article:modified_time\" content=\"2026-03-14T04:35:06+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/creating-custom-modules\\\/\",\"url\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/creating-custom-modules\\\/\",\"name\":\"Creating Custom Modules - One Language. Endless Possibilities\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/#website\"},\"datePublished\":\"2026-03-02T02:20:30+00:00\",\"dateModified\":\"2026-03-14T04:35:06+00:00\",\"description\":\"Learn how to create and use custom Python modules for reusable functions, variables, and organized, professional projects efficiently\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/creating-custom-modules\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/creating-custom-modules\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/creating-custom-modules\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/python\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PYTHON INTERMEDIATE (PYI) > Modules and Packages > Creating Custom Modules\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/python\\\/\",\"name\":\"One Language. Endless Possibilities\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/python\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Creating Custom Modules - One Language. Endless Possibilities","description":"Learn how to create and use custom Python modules for reusable functions, variables, and organized, professional projects efficiently","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:\/\/gigz.pk\/python\/lesson\/creating-custom-modules\/","og_locale":"en_US","og_type":"article","og_title":"Creating Custom Modules - One Language. Endless Possibilities","og_description":"Learn how to create and use custom Python modules for reusable functions, variables, and organized, professional projects efficiently","og_url":"https:\/\/gigz.pk\/python\/lesson\/creating-custom-modules\/","og_site_name":"One Language. Endless Possibilities","article_modified_time":"2026-03-14T04:35:06+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/python\/lesson\/creating-custom-modules\/","url":"https:\/\/gigz.pk\/python\/lesson\/creating-custom-modules\/","name":"Creating Custom Modules - One Language. Endless Possibilities","isPartOf":{"@id":"https:\/\/gigz.pk\/python\/#website"},"datePublished":"2026-03-02T02:20:30+00:00","dateModified":"2026-03-14T04:35:06+00:00","description":"Learn how to create and use custom Python modules for reusable functions, variables, and organized, professional projects efficiently","breadcrumb":{"@id":"https:\/\/gigz.pk\/python\/lesson\/creating-custom-modules\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/python\/lesson\/creating-custom-modules\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/python\/lesson\/creating-custom-modules\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/python\/"},{"@type":"ListItem","position":2,"name":"PYTHON INTERMEDIATE (PYI) > Modules and Packages > Creating Custom Modules"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/python\/#website","url":"https:\/\/gigz.pk\/python\/","name":"One Language. Endless Possibilities","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/python\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/lesson\/94","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/media?parent=94"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}