

{"id":11100,"date":"2018-03-20T04:15:37","date_gmt":"2018-03-19T22:45:37","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=11100"},"modified":"2026-04-24T14:34:33","modified_gmt":"2026-04-24T09:04:33","slug":"python-modules","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/python-modules\/","title":{"rendered":"Advance Python Modules &#8211; How to Create &amp; Import with dir Function"},"content":{"rendered":"<p>In this Python Module tutorial, we will discuss what a module is in the Python programming language. Moreover, we will talk about how to create Python modules and import modules in Python.<\/p>\n<p>Along with this, we will learn how to execute modules as a script, standard Python modules, and Python dir functions.<\/p>\n<p>So, let&#8217;s start with the Modules in Python Tutorial.<\/p>\n<div id=\"attachment_11550\" style=\"width: 1210px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/03\/Python-Modules-01.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-11550\" class=\"wp-image-11550 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/03\/Python-Modules-01.jpg\" alt=\"Python Modules\" width=\"1200\" height=\"628\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/03\/Python-Modules-01.jpg 1200w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/03\/Python-Modules-01-150x79.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/03\/Python-Modules-01-300x157.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/03\/Python-Modules-01-768x402.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/03\/Python-Modules-01-1024x536.jpg 1024w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><\/a><p id=\"caption-attachment-11550\" class=\"wp-caption-text\">Python Modules<\/p><\/div>\n<h3>What is a Python Module?<\/h3>\n<p><em>Python module is but a piece of code.<\/em><\/p>\n<p>Exiting the interpreter destroys all functions and variables we created. But when we want a longer program, we create a script.<\/p>\n<p>With Python, we can put such definitions in a file and use them in a script, or in an interactive instance of the interpreter. Such a file is a module.<\/p>\n<p><strong>Types of modules in Python:<\/strong><\/p>\n<ul>\n<li><strong>Built-in module:<\/strong> These modules are preinstalled with the Python standard library, and they can be used immediately.<\/li>\n<li><strong>User- defined module:<\/strong> These are the custom files that are created by the developers to create their own code.<\/li>\n<li><strong>External module:<\/strong> This module is being maintained by the community and is installed by the package managers like pip.<\/li>\n<\/ul>\n<p>In essence, a module is a file that contains Python statements and definitions. A Python module looks like this:<\/p>\n<p><strong>calc.py<\/strong><\/p>\n<h3>How to Create Python Modules?<\/h3>\n<p>Let\u2019s create a Python modules \u2018calc\u2019.<\/p>\n<p>Microsoft Windows [Version 10.0.16299.309]<\/p>\n<p>(c) 2017 Microsoft Corporation. All rights reserved.<\/p>\n<p>C:\\Users\\lifei&gt;cd Desktop<\/p>\n<p>C:\\Users\\lifei\\Desktop&gt;mkdir calc<\/p>\n<p>C:\\Users\\lifei\\Desktop&gt;cd calc<\/p>\n<p>C:\\Users\\lifei\\Desktop\\calc&gt;echo &gt;__init__.py<\/p>\n<p>C:\\Users\\lifei\\Desktop\\calc&gt;echo &gt;calc.py<\/p>\n<p>C:\\Users\\lifei\\Desktop\\calc&gt;<\/p>\n<p>And this is what we put inside the module calc.py:<\/p>\n<pre class=\"EnlighterJSRAW\">def add(a,b):\r\n     return a+b\r\ndef sub(a,b): \r\n     return a-b\r\ndef mul(a,b):\r\n     return a*b\r\ndef div(a,b):\r\n     return a\/b\r\ndef exp(a,b):\r\n     return a**b\r\ndef floordiv(a,b):\r\n     return a\/\/b<\/pre>\n<p>Also, calc is a package we create, and we place __init__.py inside it (Refer to Python Packages).<\/p>\n<h3>How can we Import Modules in Python?<\/h3>\n<p>Now, to import Python modules, we first get to the Desktop in Python.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; import os\r\n&gt;&gt;&gt; os.chdir('C:\\\\Users\\\\lifei\\\\Desktop\\\\calc')\r\n&gt;&gt;&gt; import calc\r\n&gt;&gt;&gt;<\/pre>\n<p>To find the name of this module, we can use the __name__ attribute.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; calc.__name__<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&#8216;calc&#8217;<\/div>\n<p>We can now use functions from this module:<\/p>\n<p>We can also assign one of these functions a name:<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; fd=calc.floordiv\r\n&gt;&gt;&gt; fd(5.5,4)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">1.0<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; fd(9,4)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">2<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; type(fd(9,4))<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&lt;class &#8216;int&#8217;&gt;<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; type(fd(5.5,4))<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&lt;class &#8216;float&#8217;&gt;<\/div>\n<h3>More on Python Modules and Importing<\/h3>\n<p>Python modules may contain anything from executable statements to function definitions. Such statements initialize the module. Hence, they execute only once, when we import the module.<\/p>\n<p>However, they also run if we execute the file as a script. Each module uses its own private symbol table globally for all of its functions.<\/p>\n<p>So, the author can use global variables in the module without worrying that they will accidentally clash with a\u00a0 user\u2019s global variables.<\/p>\n<p>A module can always import other modules in Python. In fact, we can place import statements at the beginning of a module\/script, but we don\u2019t need to.<\/p>\n<p>This places the imported module\u2019s name in the importing module\u2019s symbol table.<\/p>\n<p>We can also selectively import functions from a module:<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; from calc import div as d,floordiv as fd\r\n&gt;&gt;&gt; d(9,4)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">2.25<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; fd(9,4)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">2<\/div>\n<p>We can also import all from a module:<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; from calc import *\r\n&gt;&gt;&gt; floordiv(9,4)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">2<\/div>\n<p>This will import all names other than those beginning with an underscore(_). However, we disparage this use, as it makes for poorly readable code.<\/p>\n<p>We can also import a module under an alias.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; import calc as cal\r\n&gt;&gt;&gt; cal.sub<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>&lt;function sub at 0x0655CC90&gt;<\/p>\n<\/div>\n<h3>How to Execute Python Modules as Scripts?<\/h3>\n<p>Look at the following code:<\/p>\n<pre class=\"EnlighterJSRAW\">def add(a,b):\r\n      print(a+b)\r\ndef sub(a,b): \r\n      print(a-b)\r\ndef mul(a,b): \r\n      print(a*b)\r\ndef div(a,b):\r\n      print(a\/b)\r\ndef exp(a,b):\r\n      print(a**b)\r\ndef floordiv(a,b):\r\n      print(a\/\/b)\r\nif __name__ == \"__main__\":\r\n      import sys\r\n      if int(sys.argv[1])==1:\r\n            add(int(sys.argv[2]),int(sys.argv[3]))\r\n      elif int(sys.argv[1])==2:\r\n            sub(int(sys.argv[2]),int(sys.argv[3]))<\/pre>\n<p>These last few lines let us use the sys module to deal with command line arguments. To execute subtraction, this is what we type in the command prompt:<\/p>\n<p>C:\\Users\\lifei\\Desktop\\calc&gt;python calc.py 2 3 4<\/p>\n<p>-1<\/p>\n<p>This way, you can complete the code for other operations as well. Hence, we\u2019ve created a script. But we can also import this normally, like a module:<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; import calc\r\n&gt;&gt;&gt;<\/pre>\n<p>We may want to run a module as a script for testing purposes.<\/p>\n<p>Any doubts yet in Python Modules? Please Comment.<\/p>\n<h3>Python Module Search Path<\/h3>\n<p>Whenever we import Python modules, say eggs, the interpreter searches a built-in version. If not found, it searches for a file named eggs.py under a list of directories given by the variable sys.path.<\/p>\n<p><strong>This variable is initialized from the following locations:<\/strong><\/p>\n<ul>\n<li>The directory holding the input script (or the current directory, in case no file is specified).<\/li>\n<li>PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH).<\/li>\n<li>The installation-dependent default.<\/li>\n<\/ul>\n<p>Once initialized, a Python program may modify sys.path.<\/p>\n<h3>Compiled Python Files<\/h3>\n<p>In an attempt to speed up loading a module, Python will cache each module\u2019s compiled version in the __pycache__ directory. It does so under the name module.version.pyc.<\/p>\n<p>Here, the version encodes the compiled file\u2019s format. For instance, under CPython 3.3, we\u2019ll have eggs.py as __pycache__\/eggs.cpython-33.pyc.<\/p>\n<p>This allows compiled modules from different Python versions and releases to coexist.<\/p>\n<p>These compiled modules are platform-independent. If the compiled version is out of date, Python recompiles it automatically.<\/p>\n<h3>Python Standard Modules<\/h3>\n<p>And like we\u2019ve always said, Python ships with a library of standard Python modules. While some of them are built into the interpreter, we can create our own.<\/p>\n<p>The standard ones lend us extra functionality, in turn reducing the need to code too much. Other times, they provide efficiency to a programmer, in cases like providing access to operating system primitives, such as system calls.<\/p>\n<p>The module sys is built into every Python interpreter. However, some modules are only available to certain operating platforms.<\/p>\n<p>For instance, the winreg module is only available to Windows programmers.<\/p>\n<p>The sys module will also tell you which version of Python you are using.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; import sys\r\n&gt;&gt;&gt; sys.version<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&#8216;3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 07:18:10) [MSC v.1900 32 bit (Intel)]&#8217;<\/div>\n<h3>Python dir() Function<\/h3>\n<p>The dir() is a built-in function that returns a sorted list of all the names that a Python modules defines.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; dir(sys)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">[&#8216;__displayhook__&#8217;, &#8216;__doc__&#8217;, &#8216;__excepthook__&#8217;, &#8216;__interactivehook__&#8217;, &#8216;__loader__&#8217;, &#8216;__name__&#8217;, &#8216;__package__&#8217;, &#8216;__spec__&#8217;, &#8216;__stderr__&#8217;, &#8216;__stdin__&#8217;, &#8216;__stdout__&#8217;, &#8216;_clear_type_cache&#8217;, &#8216;_current_frames&#8217;, &#8216;_debugmallocstats&#8217;, &#8216;_enablelegacywindowsfsencoding&#8217;, &#8216;_getframe&#8217;, &#8216;_home&#8217;, &#8216;_mercurial&#8217;, &#8216;_xoptions&#8217;, &#8216;api_version&#8217;, &#8216;argv&#8217;, &#8216;base_exec_prefix&#8217;, &#8216;base_prefix&#8217;, &#8216;builtin_module_names&#8217;, &#8216;byteorder&#8217;, &#8216;call_tracing&#8217;, &#8216;callstats&#8217;, &#8216;copyright&#8217;, &#8216;displayhook&#8217;, &#8216;dllhandle&#8217;, &#8216;dont_write_bytecode&#8217;, &#8216;exc_info&#8217;, &#8216;excepthook&#8217;, &#8216;exec_prefix&#8217;, &#8216;executable&#8217;, &#8216;exit&#8217;, &#8216;flags&#8217;, &#8216;float_info&#8217;, &#8216;float_repr_style&#8217;, &#8216;get_asyncgen_hooks&#8217;, &#8216;get_coroutine_wrapper&#8217;, &#8216;getallocatedblocks&#8217;, &#8216;getcheckinterval&#8217;, &#8216;getdefaultencoding&#8217;, &#8216;getfilesystemencodeerrors&#8217;, &#8216;getfilesystemencoding&#8217;, &#8216;getprofile&#8217;, &#8216;getrecursionlimit&#8217;, &#8216;getrefcount&#8217;, &#8216;getsizeof&#8217;, &#8216;getswitchinterval&#8217;, &#8216;gettrace&#8217;, &#8216;getwindowsversion&#8217;, &#8216;hash_info&#8217;, &#8216;hexversion&#8217;, &#8216;implementation&#8217;, &#8216;int_info&#8217;, &#8216;intern&#8217;, &#8216;is_finalizing&#8217;, &#8216;last_traceback&#8217;, &#8216;last_type&#8217;, &#8216;last_value&#8217;, &#8216;maxsize&#8217;, &#8216;maxunicode&#8217;, &#8216;meta_path&#8217;, &#8216;modules&#8217;, &#8216;path&#8217;, &#8216;path_hooks&#8217;, &#8216;path_importer_cache&#8217;, &#8216;platform&#8217;, &#8216;prefix&#8217;, &#8216;set_asyncgen_hooks&#8217;, &#8216;set_coroutine_wrapper&#8217;, &#8216;setcheckinterval&#8217;, &#8216;setprofile&#8217;, &#8216;setrecursionlimit&#8217;, &#8216;setswitchinterval&#8217;, &#8216;settrace&#8217;, &#8216;stderr&#8217;, &#8216;stdin&#8217;, &#8216;stdout&#8217;, &#8216;thread_info&#8217;, &#8216;version&#8217;, &#8216;version_info&#8217;, &#8216;warnoptions&#8217;, &#8216;winver&#8217;]<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; for i in dir(calc): print(i)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">__builtins__<br \/>\n__cached__<br \/>\n__doc__<br \/>\n__file__<br \/>\n__loader__<br \/>\n__name__<br \/>\n__package__<br \/>\n__spec__<br \/>\nadd<br \/>\ndiv<br \/>\nexp<br \/>\nfloordiv<br \/>\nmul<br \/>\nsub<\/div>\n<p>And without any arguments, dir() returns a list of the names that we have defined currently.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; dir()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">[&#8216;__annotations__&#8217;, &#8216;__builtins__&#8217;, &#8216;__doc__&#8217;, &#8216;__loader__&#8217;, &#8216;__name__&#8217;, &#8216;__package__&#8217;, &#8216;__spec__&#8217;, &#8216;add&#8217;, &#8216;cal&#8217;, &#8216;calc&#8217;, &#8216;div&#8217;, &#8216;exp&#8217;, &#8216;floordiv&#8217;, &#8216;i&#8217;, &#8216;mul&#8217;, &#8216;os&#8217;, &#8216;sub&#8217;, &#8216;sys&#8217;]<\/div>\n<p>To get a list of built-in functions and variables, we do the following, instead:<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; import builtins\r\n&gt;&gt;&gt; dir(builtins)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">[&#8216;ArithmeticError&#8217;, &#8216;AssertionError&#8217;, &#8216;AttributeError&#8217;, &#8216;BaseException&#8217;, &#8216;BlockingIOError&#8217;, &#8216;BrokenPipeError&#8217;, &#8216;BufferError&#8217;, &#8216;BytesWarning&#8217;, &#8216;ChildProcessError&#8217;, &#8216;ConnectionAbortedError&#8217;, &#8216;ConnectionError&#8217;, &#8216;ConnectionRefusedError&#8217;, &#8216;ConnectionResetError&#8217;, &#8216;DeprecationWarning&#8217;, &#8216;EOFError&#8217;, &#8216;Ellipsis&#8217;, &#8216;EnvironmentError&#8217;, &#8216;Exception&#8217;, &#8216;False&#8217;, &#8216;FileExistsError&#8217;, &#8216;FileNotFoundError&#8217;, &#8216;FloatingPointError&#8217;, &#8216;FutureWarning&#8217;, &#8216;GeneratorExit&#8217;, &#8216;IOError&#8217;, &#8216;ImportError&#8217;, &#8216;ImportWarning&#8217;, &#8216;IndentationError&#8217;, &#8216;IndexError&#8217;, &#8216;InterruptedError&#8217;, &#8216;IsADirectoryError&#8217;, &#8216;KeyError&#8217;, &#8216;KeyboardInterrupt&#8217;, &#8216;LookupError&#8217;, &#8216;MemoryError&#8217;, &#8216;ModuleNotFoundError&#8217;, &#8216;NameError&#8217;, &#8216;None&#8217;, &#8216;NotADirectoryError&#8217;, &#8216;NotImplemented&#8217;, &#8216;NotImplementedError&#8217;, &#8216;OSError&#8217;, &#8216;OverflowError&#8217;, &#8216;PendingDeprecationWarning&#8217;, &#8216;PermissionError&#8217;, &#8216;ProcessLookupError&#8217;, &#8216;RecursionError&#8217;, &#8216;ReferenceError&#8217;, &#8216;ResourceWarning&#8217;, &#8216;RuntimeError&#8217;, &#8216;RuntimeWarning&#8217;, &#8216;StopAsyncIteration&#8217;, &#8216;StopIteration&#8217;, &#8216;SyntaxError&#8217;, &#8216;SyntaxWarning&#8217;, &#8216;SystemError&#8217;, &#8216;SystemExit&#8217;, &#8216;TabError&#8217;, &#8216;TimeoutError&#8217;, &#8216;True&#8217;, &#8216;TypeError&#8217;, &#8216;UnboundLocalError&#8217;, &#8216;UnicodeDecodeError&#8217;, &#8216;UnicodeEncodeError&#8217;, &#8216;UnicodeError&#8217;, &#8216;UnicodeTranslateError&#8217;, &#8216;UnicodeWarning&#8217;, &#8216;UserWarning&#8217;, &#8216;ValueError&#8217;, &#8216;Warning&#8217;, &#8216;WindowsError&#8217;, &#8216;ZeroDivisionError&#8217;, &#8216;_&#8217;, &#8216;__build_class__&#8217;, &#8216;__debug__&#8217;, &#8216;__doc__&#8217;, &#8216;__import__&#8217;, &#8216;__loader__&#8217;, &#8216;__name__&#8217;, &#8216;__package__&#8217;, &#8216;__spec__&#8217;, &#8216;abs&#8217;, &#8216;all&#8217;, &#8216;any&#8217;, &#8216;ascii&#8217;, &#8216;bin&#8217;, &#8216;bool&#8217;, &#8216;bytearray&#8217;, &#8216;bytes&#8217;, &#8216;callable&#8217;, &#8216;chr&#8217;, &#8216;classmethod&#8217;, &#8216;compile&#8217;, &#8216;complex&#8217;, &#8216;copyright&#8217;, &#8216;credits&#8217;, &#8216;delattr&#8217;, &#8216;dict&#8217;, &#8216;dir&#8217;, &#8216;divmod&#8217;, &#8216;enumerate&#8217;, &#8216;eval&#8217;, &#8216;exec&#8217;, &#8216;exit&#8217;, &#8216;filter&#8217;, &#8216;float&#8217;, &#8216;format&#8217;, &#8216;frozenset&#8217;, &#8216;getattr&#8217;, &#8216;globals&#8217;, &#8216;hasattr&#8217;, &#8216;hash&#8217;, &#8216;help&#8217;, &#8216;hex&#8217;, &#8216;id&#8217;, &#8216;input&#8217;, &#8216;int&#8217;, &#8216;isinstance&#8217;, &#8216;issubclass&#8217;, &#8216;iter&#8217;, &#8216;len&#8217;, &#8216;license&#8217;, &#8216;list&#8217;, &#8216;locals&#8217;, &#8216;map&#8217;, &#8216;max&#8217;, &#8216;memoryview&#8217;, &#8216;min&#8217;, &#8216;next&#8217;, &#8216;object&#8217;, &#8216;oct&#8217;, &#8216;open&#8217;, &#8216;ord&#8217;, &#8216;pow&#8217;, &#8216;print&#8217;, &#8216;property&#8217;, &#8216;quit&#8217;, &#8216;range&#8217;, &#8216;repr&#8217;, &#8216;reversed&#8217;, &#8217;round&#8217;, &#8216;set&#8217;, &#8216;setattr&#8217;, &#8216;slice&#8217;, &#8216;sorted&#8217;, &#8216;staticmethod&#8217;, &#8216;str&#8217;, &#8216;sum&#8217;, &#8216;super&#8217;, &#8216;tuple&#8217;, &#8216;type&#8217;, &#8216;vars&#8217;, &#8216;zip&#8217;]<\/div>\n<p>This was all on Python modules. Hope the Python Modules article was informative.<\/p>\n<h3>Python Interview Questions on Modules<\/h3>\n<ol>\n<li>What is a module in Python? Explain with an example.<\/li>\n<li>Explain the purpose of using Python modules.<\/li>\n<li>How many modules are there in Python?<\/li>\n<li>How to install and open a Python module?<\/li>\n<li>Where are Python modules stored?<\/li>\n<\/ol>\n<h3>Conclusion<\/h3>\n<p>While this is all about Python modules, we suggest you also read about Packages in Python. Then, maybe you should switch to Packages vs Modules.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this Python Module tutorial, we will discuss what a module is in the Python programming language. Moreover, we will talk about how to create Python modules and import modules in Python. Along with&#46;&#46;&#46;<\/p>\n","protected":false},"author":5,"featured_media":31072,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[2789,3117,6644,8782,10687,13731,14670,14680],"class_list":["post-11100","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-compiled-python-files","tag-creating-a-python-module","tag-importing-modules-in-python","tag-modules-in-python","tag-python-modules","tag-standard-python-modules","tag-the-dir-function","tag-the-module-search-path"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Advance Python Modules - How to Create &amp; Import with dir Function - DataFlair<\/title>\n<meta name=\"description\" content=\"Let&#039;s start with the Modules in Python Tutorial, where we will discuss what a Python module is, how to create and import modules in Python.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/python-modules\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Advance Python Modules - How to Create &amp; Import with dir Function - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Let&#039;s start with the Modules in Python Tutorial, where we will discuss what a Python module is, how to create and import modules in Python.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/python-modules\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-03-19T22:45:37+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-04-24T09:04:33+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/03\/Python-Modules-01-1.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Advance Python Modules - How to Create &amp; Import with dir Function - DataFlair","description":"Let's start with the Modules in Python Tutorial, where we will discuss what a Python module is, how to create and import modules in Python.","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:\/\/data-flair.training\/blogs\/python-modules\/","og_locale":"en_US","og_type":"article","og_title":"Advance Python Modules - How to Create &amp; Import with dir Function - DataFlair","og_description":"Let's start with the Modules in Python Tutorial, where we will discuss what a Python module is, how to create and import modules in Python.","og_url":"https:\/\/data-flair.training\/blogs\/python-modules\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2018-03-19T22:45:37+00:00","article_modified_time":"2026-04-24T09:04:33+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/03\/Python-Modules-01-1.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/python-modules\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/python-modules\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823"},"headline":"Advance Python Modules &#8211; How to Create &amp; Import with dir Function","datePublished":"2018-03-19T22:45:37+00:00","dateModified":"2026-04-24T09:04:33+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/python-modules\/"},"wordCount":1416,"commentCount":8,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/python-modules\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/03\/Python-Modules-01-1.jpg","keywords":["Compiled Python Files","Creating a Python Module","Importing Modules in Python","Modules in Python","Python Modules","Standard Python Modules","The dir() Function","The Module Search Path"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/python-modules\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/python-modules\/","url":"https:\/\/data-flair.training\/blogs\/python-modules\/","name":"Advance Python Modules - How to Create &amp; Import with dir Function - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/python-modules\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/python-modules\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/03\/Python-Modules-01-1.jpg","datePublished":"2018-03-19T22:45:37+00:00","dateModified":"2026-04-24T09:04:33+00:00","description":"Let's start with the Modules in Python Tutorial, where we will discuss what a Python module is, how to create and import modules in Python.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/python-modules\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/python-modules\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/python-modules\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/03\/Python-Modules-01-1.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/03\/Python-Modules-01-1.jpg","width":1200,"height":628,"caption":"Python Modules"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/python-modules\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Python Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/python\/"},{"@type":"ListItem","position":3,"name":"Advance Python Modules &#8211; How to Create &amp; Import with dir Function"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team creates expert-level guides on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our goal is to empower learners with easy-to-understand content. Explore our resources for career growth and practical learning.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam1\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/11100","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=11100"}],"version-history":[{"count":12,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/11100\/revisions"}],"predecessor-version":[{"id":147832,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/11100\/revisions\/147832"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/31072"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=11100"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=11100"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=11100"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}