{"id":197,"date":"2019-02-08T05:31:39","date_gmt":"2019-02-08T05:31:39","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/python\/?p=197"},"modified":"2026-07-04T01:21:16","modified_gmt":"2026-07-04T01:21:16","slug":"17-python-modules","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/python\/17-python-modules\/","title":{"rendered":"Python &#8211; Modules"},"content":{"rendered":"<p>A module in Python is a container for codes. So you can defined functions, classes and variables inside a module. Let&#8217;s say you have a function you have written. This function contains several lines of code and you want to use them in different files. In this case, you can place this function in a module.<\/p>\n<p>We would cover the following:<\/p>\n<ol>\n<li><a href=\"#t1\">How to Create a Module<\/a><\/li>\n<li><a href=\"#t2\">HowThe import Statement<\/a><\/li>\n<li><a href=\"#t3\">HowThe from&#8230;import Statement<\/a><\/li>\n<li><a href=\"#t4\">HowLocating Modules<\/a><\/li>\n<li><a href=\"#t5\">HowThe dir () Function<\/a><\/li>\n<li><a href=\"#t6\">HowThe globals() and locals() Functions<\/a><\/li>\n<li><a href=\"#t7\">HowThe reload() Function<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t1\">1. How to create a module<\/strong><\/h4>\n<p>To create a module, you first create a Python file and save it with the name of the module. So if you want to create a module called mycodes, then you would first create a file called mycodes.py. Inside this file you can then write the functions or other codes you want to reuse.<\/p>\n<p>Now, there are modules that are built-in that you can just use. For example, the math module that contains useful functions for performing arithmetic. The random module for working with random numbers and sequences, and lots more.<\/p>\n<p>You can scroll down to the video and watch how this is done.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t2\">2. The import Statement<\/strong><\/h4>\n<p>To use the codes in the module, you simply use the import statement. So if you have created a module called mycodes, the to use it in any file, you must add import mycodes in the new file.<\/p>\n<p>You can also import more than one module. For example,<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">mycodes<\/span>\r\n<\/pre>\n<p>When an import statement is encountered, then the interpreter searches the current path for the module. If it finds it, then it imports it into the program.<\/p>\n<p>Now, after importing a module, you are free to use the functions available in the module. You simply use them by using this syntax below.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">modulename<span style=\"color: #333333;\">.<\/span>functionname\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong id=\"t3\">3. The from&#8230;import Statement<\/strong><\/p>\n<p>Let&#8217;s say a module contains 100 functions, and you want to use only one of them. You\u00a0 don&#8217;t have to import the whole module. The from&#8230;import statement allows you to import just a block of code or function from a module.<\/p>\n<p>So if you have a module called modulename and the name of the function you want to use is functionname, you can used the syntax below<\/p>\n<p>&nbsp;<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">from<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">modulename<\/span> <span style=\"color: #008800; font-weight: bold;\">import<\/span> functioname\r\n<\/pre>\n<p>So this ensures that you import only the particular function<\/p>\n<p>&nbsp;<\/p>\n<p>You can also use the <em><strong>from module import *<\/strong><\/em> statement. In this case, you are importing all the functions from a module into the current namespace.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t4\">4. Locating Modules<\/strong><\/h4>\n<p>When you use an import statement, the Python interpreter checks for the module in following locations in order.<\/p>\n<ul>\n<li>The current directory<\/li>\n<li>If the module is not there, then Python searches each directory in the <strong><em>PYTHONPATH<\/em> <\/strong>shell variable<\/li>\n<li>If not found, Python then searches the default path.<\/li>\n<\/ul>\n<p>The variable that contains the module search path is the <strong><em>sys.path<\/em><\/strong> variable. The sys.path variable contains the PYTHONPATH, the current directory as well as the installation-dependent default.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>The PYTHONPATH variable<\/strong><\/p>\n<p>The PYTHONPATH is an environment variable used by Python. it contains list of directories that Python searches for modules. You can also change the location of this directory using <em><strong>set PYTHONPATH<\/strong><\/em> command.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t5\">5. The dir( ) Function<\/strong><\/h4>\n<p>This is an important built-in function in Python. The dir() function returns an sorted list of strings containing names that are defined in a module.<\/p>\n<p>This list would contain all functions, modules, variables defined inside the specified module. For example, the code below lists the content of the math module.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\"># import the math built-in module<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">math<\/span>\r\n\r\nlistofitems <span style=\"color: #333333;\">=<\/span> <span style=\"color: #007020;\">dir<\/span>(math)\r\n<span style=\"color: #007020;\">print<\/span>(listofitems)\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>If you execute the code then you will get the list of items below.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">[<span style=\"background-color: #fff0f0;\">'__doc__'<\/span>, <span style=\"background-color: #fff0f0;\">'__loader__'<\/span>, <span style=\"background-color: #fff0f0;\">'__name__'<\/span>, <span style=\"background-color: #fff0f0;\">'__package__'<\/span>, <span style=\"background-color: #fff0f0;\">'__spec__'<\/span>, \r\n<span style=\"background-color: #fff0f0;\">'acos'<\/span>, <span style=\"background-color: #fff0f0;\">'acosh'<\/span>, <span style=\"background-color: #fff0f0;\">'asin'<\/span>, <span style=\"background-color: #fff0f0;\">'asinh'<\/span>, <span style=\"background-color: #fff0f0;\">'atan'<\/span>, <span style=\"background-color: #fff0f0;\">'atan2'<\/span>, <span style=\"background-color: #fff0f0;\">'atanh'<\/span>, <span style=\"background-color: #fff0f0;\">'ceil'<\/span>, \r\n<span style=\"background-color: #fff0f0;\">'copysign'<\/span>, <span style=\"background-color: #fff0f0;\">'cos'<\/span>, <span style=\"background-color: #fff0f0;\">'cosh'<\/span>, <span style=\"background-color: #fff0f0;\">'degrees'<\/span>, <span style=\"background-color: #fff0f0;\">'e'<\/span>, <span style=\"background-color: #fff0f0;\">'erf'<\/span>, <span style=\"background-color: #fff0f0;\">'erfc'<\/span>, <span style=\"background-color: #fff0f0;\">'exp'<\/span>, \r\n<span style=\"background-color: #fff0f0;\">'expm1'<\/span>, <span style=\"background-color: #fff0f0;\">'fabs'<\/span>, <span style=\"background-color: #fff0f0;\">'factorial'<\/span>, <span style=\"background-color: #fff0f0;\">'floor'<\/span>, <span style=\"background-color: #fff0f0;\">'fmod'<\/span>, <span style=\"background-color: #fff0f0;\">'frexp'<\/span>, <span style=\"background-color: #fff0f0;\">'fsum'<\/span>, \r\n<span style=\"background-color: #fff0f0;\">'gamma'<\/span>, <span style=\"background-color: #fff0f0;\">'gcd'<\/span>, <span style=\"background-color: #fff0f0;\">'hypot'<\/span>, <span style=\"background-color: #fff0f0;\">'inf'<\/span>, <span style=\"background-color: #fff0f0;\">'isclose'<\/span>, <span style=\"background-color: #fff0f0;\">'isfinite'<\/span>, <span style=\"background-color: #fff0f0;\">'isinf'<\/span>, \r\n<span style=\"background-color: #fff0f0;\">'isnan'<\/span>, <span style=\"background-color: #fff0f0;\">'ldexp'<\/span>, <span style=\"background-color: #fff0f0;\">'lgamma'<\/span>, <span style=\"background-color: #fff0f0;\">'log'<\/span>, <span style=\"background-color: #fff0f0;\">'log10'<\/span>, <span style=\"background-color: #fff0f0;\">'log1p'<\/span>, <span style=\"background-color: #fff0f0;\">'log2'<\/span>, <span style=\"background-color: #fff0f0;\">'modf'<\/span>, \r\n<span style=\"background-color: #fff0f0;\">'nan'<\/span>, <span style=\"background-color: #fff0f0;\">'pi'<\/span>, <span style=\"background-color: #fff0f0;\">'pow'<\/span>, <span style=\"background-color: #fff0f0;\">'radians'<\/span>, <span style=\"background-color: #fff0f0;\">'sin'<\/span>, <span style=\"background-color: #fff0f0;\">'sinh'<\/span>, <span style=\"background-color: #fff0f0;\">'sqrt'<\/span>, <span style=\"background-color: #fff0f0;\">'tan'<\/span>, \r\n<span style=\"background-color: #fff0f0;\">'tanh'<\/span>, <span style=\"background-color: #fff0f0;\">'tau'<\/span>, <span style=\"background-color: #fff0f0;\">'trunc'<\/span>]\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Note the special variable that begins with double underscore __name__. This is the module&#8217;s name. The other string __file__ is the file name where the module was imported from<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t6\">6. The <em>globals( )<\/em> and the <em>locals( )<\/em> Functions<\/strong><\/h4>\n<p>These two functions are used to return the names in the global and local namespace. Though this depends on the location from where the call is made.<\/p>\n<p>If you call locals() from within a function, then it would return all the names that are accessible locally from that function. However if you call globals() from inside a function, then it would return all that are accessible globally from that function.<\/p>\n<p>The return type of globals() and the locals() functions is a dictionary.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t7\">7. The reload() Function<\/strong><\/h4>\n<p>If you import a module into your program, then the code in the top-level part of the module is executed just once. This means that if changes are made to the module, you would not have the updated version of the module.<\/p>\n<p>To solve this you used the reload() function. This function\u00a0 is used to re-execute the top-level code in a module. So you can have the update module. It runs the import statement again. Then syntax for reload() is:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">reload(module)\r\n<\/pre>\n<p>Here, module is the name of the module you want to reload<\/p>\n<p>&nbsp;<\/p>\n<p><!-- ktg-lesson-nav --><\/p>\n<nav class=\"ktg-lesson-nav\" aria-label=\"Lesson navigation\">\n<p><strong>Previous:<\/strong> <a href=\"https:\/\/kindsonthegenius.com\/python\/16-python-anonymous-functions\/\">Lesson 15: Python &#8211; Anonymous Functions<\/a><\/p>\n<p><strong>Next:<\/strong> <a href=\"https:\/\/kindsonthegenius.com\/python\/18-python-files-io\/\">Lesson 17: Python &#8211; Files &#038; IO<\/a><\/p>\n<\/nav>\n<p><!-- ktg-alkademy-cta --><\/p>\n<div class=\"ktg-alkademy-cta\" style=\"margin:2em 0;padding:1.25em;border-left:4px solid #2563eb;background:#f8fafc;\">\n<p><strong>Want live Python classes?<\/strong> Join <a href=\"https:\/\/www.alkademy.com\/courses\" target=\"_blank\" rel=\"noopener noreferrer\">Alkademy<\/a> for instructor-led Python and data science courses with hands-on projects.<\/p>\n<\/div>\n<p><!-- ktg-author-trust --><\/p>\n<div class=\"ktg-author-trust\" style=\"margin:2em 0;padding:1.25em;border-top:1px solid #e2e8f0;\">\n<p>Kindson Munonye is a software engineer and technical author specializing in Python, data science, and machine learning. He publishes free step-by-step Python tutorials and live classes on Alkademy.<\/p>\n<p><a href=\"https:\/\/github.com\/KindsonTheGenius\" target=\"_blank\" rel=\"noopener noreferrer\">GitHub<\/a> \u00b7 <a href=\"https:\/\/www.linkedin.com\/in\/kindson\" target=\"_blank\" rel=\"noopener noreferrer\">LinkedIn<\/a> \u00b7 <a href=\"https:\/\/www.kindsonthegenius.com\/about\/\">About<\/a> \u00b7 <a href=\"https:\/\/www.alkademy.com\/\" target=\"_blank\" rel=\"noopener noreferrer\">Alkademy<\/a><\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>A module in Python is a container for codes. So you can defined functions, classes and variables inside a module. Let&#8217;s say you have a &hellip; <\/p>\n","protected":false},"author":395,"featured_media":201,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"footnotes":""},"categories":[5],"tags":[17],"class_list":["post-197","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-tutorials","tag-python-modules"],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/python\/wp-json\/wp\/v2\/posts\/197","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/python\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/python\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/python\/wp-json\/wp\/v2\/users\/395"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/python\/wp-json\/wp\/v2\/comments?post=197"}],"version-history":[{"count":4,"href":"https:\/\/kindsonthegenius.com\/python\/wp-json\/wp\/v2\/posts\/197\/revisions"}],"predecessor-version":[{"id":582,"href":"https:\/\/kindsonthegenius.com\/python\/wp-json\/wp\/v2\/posts\/197\/revisions\/582"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/python\/wp-json\/wp\/v2\/media\/201"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/python\/wp-json\/wp\/v2\/media?parent=197"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/python\/wp-json\/wp\/v2\/categories?post=197"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/python\/wp-json\/wp\/v2\/tags?post=197"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}