{"id":18009,"date":"2022-02-22T06:02:55","date_gmt":"2022-02-22T06:02:55","guid":{"rendered":"https:\/\/machinelearningplus.com\/?p=18009"},"modified":"2022-03-08T15:30:07","modified_gmt":"2022-03-08T15:30:07","slug":"python-module-packages","status":"publish","type":"post","link":"https:\/\/machinelearningplus.com\/python\/python-module-packages\/","title":{"rendered":"Python Module &#8211; What are modules and packages in python?"},"content":{"rendered":"<p>Any Python file with a <code>.py<\/code> extension is a Module in Python. A python package is a collection of such modules along with a <code>__init__.py<\/code> file. Let&#8217;s understand how to work with modules and packages with clear examples.<\/p>\n<h2>Introduction<\/h2>\n<p>When you work on python projects, it&#8217;s not a good practice to have all you python code in one single python file (.py).<br \/>\nYou are better off splitting your code, classes, functions and variables thoughtfully in separate python files (.py files), aka <strong>modules<\/strong>. Python allows you to import code in one module for use in other modules.<\/p>\n<p>This will:<br \/>\n1. Make your code modular, there by make the python objects reusable across modules.<br \/>\n2. Allows you to focus on a small part of problem at a time without disturbing the whole.<br \/>\n3. Makes bug fixing easier.<br \/>\n4. Allow multiple developers to contribute to your project effectively<br \/>\n5. Organize the code and maintain the project a lot more easier.<\/p>\n<p>So what is a <strong>Package<\/strong>? and how is it different from a module?<\/p>\n<h2>What is a Module and Package?<\/h2>\n<p>A Python <strong>module<\/strong> is any Python files with a <code>.py<\/code> extension. It can be imported into python without the <code>.py<\/code> part.<\/p>\n<p>A Python <strong>package<\/strong> is nothing but a collection of modules along with a <code>__init__.py<\/code> file. The modules can also be arranged in hierarchy of folders inside a package.<\/p>\n<p>Just by adding an empty <code>__init__.py<\/code> file to the in the folder, Python knows it is a Package.<\/p>\n<p>In fact, a package is also really a module that contains other modules.<\/p>\n<p>Python provides a wide variety of modules as standard modules. You can find the full list <a href=\"https:\/\/docs.python.org\/3\/py-modindex.html\">here<\/a>.<\/p>\n<p><strong>Pre-requisite to follow along the code below<\/strong><\/p>\n<p>To follow along the code, download this <a href=\"https:\/\/machinelearningplus.com\/wp-content\/uploads\/2022\/02\/package_and_modules_mlplus.7z\">file<\/a> and extract it. Then, open your project from it. That is in your terminal or command prompt, do <code>cd folder_name<\/code> and type <code>jupyter notebook<\/code> if you have <a href=\"https:\/\/anaconda.com\/\">anaconda<\/a> installed. Or if you are using <a href=\"https:\/\/code.visualstudio.com\/\">VSCode<\/a>, <a href=\"https:\/\/www.jetbrains.com\/pycharm\/\">Pycharm<\/a> etc, open your project from this folder.<\/p>\n<p>Once inside you can see python files and folders.<\/p>\n<p>The python file <code>cars.py<\/code> can be imported in python as a <code>module<\/code>. In fact, the type such an imported object is <code>module<\/code>.<\/p>\n<pre><code class=\"language-python\"># Import the cars.py file as a `cars` module\nimport cars\ntype(cars)\n<\/code><\/pre>\n<p>#&gt; module<\/p>\n<p>Inside the python module (the <code>.py<\/code> file) you can define one or more class and import them.<\/p>\n<pre><code class=\"language-python\"># Import a class from the file\nfrom cars import Car\n<\/code><\/pre>\n<p>Initialize and start using.<\/p>\n<pre><code class=\"language-python\"># Start the car\ncar1 = Car(make=\"Toyota\", model=\"Camry\")\n\n# Start driving\ncar1.start()\n<\/code><\/pre>\n<p>#&gt; &#8230;VROOOOM&#8230;.Started!<\/p>\n<p>Stop.<\/p>\n<pre><code class=\"language-python\">car1.stop()\ncar1.check_speed_and_gear()\n<\/code><\/pre>\n<p>I&#8217;m driving at: 0 in gear: 0<\/p>\n<h2>Package Example<\/h2>\n<p>You can also import from the <strong>cars package<\/strong>. You will see the <code>carspackage<\/code> folder inside. Since this contains a <code>__init__.py<\/code>, this is a python package.<\/p>\n<p>Inside it also contains <code>cars.py<\/code> and <code>suv.py<\/code>.<\/p>\n<pre><code class=\"language-python\">from carspackage import cars as carsp\nfrom carspackage import suv as suvp\n<\/code><\/pre>\n<p>#&gt; <strong>name<\/strong>: carspackage.cars<br \/>\n#&gt; I am outside the guard!<\/p>\n<p>Instantiate<\/p>\n<pre><code class=\"language-python\">car2 = carsp.Car(make=\"Toyota\", model=\"Camry\")\ncar2\n<\/code><\/pre>\n<p>#&gt; Make Toyota, model: Camry<\/p>\n<p>Start using..<\/p>\n<pre><code class=\"language-python\"># Start driving\ncar2.start()\n<\/code><\/pre>\n<p>#&gt; &#8230;VROOOOM&#8230;.Started!<\/p>\n<p>Accelerate..<\/p>\n<pre><code class=\"language-python\">car1.accelerate()\ncar1.check_speed_and_gear()\ncar1.stop()\n<\/code><\/pre>\n<p>#&gt; I&#8217;m driving at: 5 in gear: 0<\/p>\n<p>Now, let&#8217;s try running the SUV<\/p>\n<pre><code class=\"language-python\">suv1 = suvp.SUV(make=\"Honda\", model=\"CRV\")\nsuv1.start_drive()\nsuv1.check_speed_and_gear()\n<\/code><\/pre>\n<p>#&gt; Init success!!<br \/>\n#&gt; Shift Up and Drive.<br \/>\n#&gt; I am driving at 5 mph<br \/>\n#&gt; I&#8217;m driving at: 5 in gear: 1<\/p>\n<pre><code class=\"language-python\">suv1.stop()\n<\/code><\/pre>\n<h2>Purpose of <code>__<\/code>main<code>__<\/code>.py<\/h2>\n<p>Just like how you call a python script in terminal <code>python my_code.py<\/code>, you can call your package from command prompt \/ terminal as well via <code>python {pacakge_name}<\/code>.<\/p>\n<p>But when called so, which module \/ code will be run?<\/p>\n<p>That is the purpose of <code>__main__.py<\/code><\/p>\n<p>When a package is called from terminal, the Python will look for executing the contents of <code>__main__.py<\/code> file inside the package.<\/p>\n<p>In practical uses, have a python package designed to do a spacific task, say convert a color image to b\/w, you can build your scripts as a package and pass the path to image you to convert as an argument to <code>python pkgname --image.png<\/code>.<\/p>\n<p>Let&#8217;s call <code>carspackage<\/code> from terminal \/ command prompt.<\/p>\n<pre><code class=\"language-python\">!python carspackage\n<\/code><\/pre>\n<p>#&gt; <strong>name<\/strong>: cars<br \/>\n#&gt; Let&#8217;s create a Toyota RAV4!<br \/>\n#&gt; Make is Toyota, Model is RAV4<\/p>\n<p>#&gt; &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<\/p>\n<p>That simply executed the <code>__main__.py<\/code>.<\/p>\n<p>You can make it receive arguments from the user as well.<\/p>\n<h3>Receiving command line arguments<\/h3>\n<p><strong>What are command line arguments?<\/strong><\/p>\n<p>When you call a python program or a pacakge, you can pass additional input values based on which the output of your python program can change.<\/p>\n<p>For example:<\/p>\n<p>a. An email sending program may receive the &#8216;To address&#8217; as an input<br \/>\nb. A program to process data can take the number of lines of data as input.<\/p>\n<p>The simplest way to pass argument to your python script from command is using <code>sys.argv()<\/code><\/p>\n<p>Now, uncomment the <code>sys.argv<\/code> part inside <code>__main.py__<\/code> and run the below code.<\/p>\n<pre><code class=\"language-python\">!python carspackage make=\"Toyota\" model=\"RAV4\n<\/code><\/pre>\n<p>#&gt; <strong>name<\/strong>: cars<br \/>\n#&gt; Let&#8217;s create a Toyota RAV4!<br \/>\n#&gt; Make is Toyota, Model is RAV4<\/p>\n<p>#&gt; &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<\/p>\n<p>A more sophisticated and convenient way of receiving and processing arguments is provided by the <a href=\"https:\/\/docs.python.org\/3\/library\/argparse.html\"><code>argparse<\/code><\/a> package. This is part of python standard library and has been adopted by developers.<\/p>\n<h2>Packages with hierarchy<\/h2>\n<p>The <code>carspackagedeep<\/code> folder contains contains folders that contain the python files. So, it&#8217;s 1 additional level deep.<\/p>\n<p>So, you need to point to that folder and then import the module.<\/p>\n<pre><code class=\"language-python\">from carspackagedeep.Car import cars\n<\/code><\/pre>\n<p>#&gt; <strong>name<\/strong>: carspackagedeep.Car.cars<\/p>\n<p>Now, import <code>suv<\/code> as well.<\/p>\n<pre><code class=\"language-python\">from carspackagedeep.Suv import suv\n<\/code><\/pre>\n<p>#&gt; I am outside the guard!<\/p>\n<p>If you notice the contents of <code>suv.py<\/code>, it contains a <code>__name__ == \"__main__\"<\/code> statement. why?<\/p>\n<h2>What does <code>__<\/code>name<code>__<\/code> == &#8220;<code>__<\/code>main<code>__<\/code>&#8221; do?<\/h2>\n<p>Whenever the Python interpreter reads a source file, it does two things:<\/p>\n<ol>\n<li>It sets a few special variables like <code>__name__<\/code><\/li>\n<li>It executes all of the code found in the file.<\/li>\n<\/ol>\n<p>When you import a python package or module, all the code present in the module is run.<\/p>\n<p>So, when you run <code>import mypackage<\/code>, there is every chance that certain code present in <code>mypackage<\/code> you didn&#8217;t want to execute may get executed on import.<\/p>\n<p>You can prevent this by checking the condition: <code>__name__ == \"__main__\"<\/code>. It acts as a <strong>guard<\/strong>. The parts of your code that you don&#8217;t want to be run can be placed inside the <code>__name__ == \"__main__\"<\/code> condition block.<\/p>\n<p>If the code is run when getting imported from another package, the value of <code>__name__<\/code> will bear the <code>path\/name<\/code> of the module. For example: the value of <code>__name__<\/code> for &#8216;carspackage\/cars.py&#8217; when called from other places will be <code>carspackage.cars<\/code>.<\/p>\n<p>Only when you are directly running <code>python carspackage\/cars.py<\/code>, that is, only when you run the module as the main program, the value of <code>__name__<\/code> will be <code>__main__<\/code>.<\/p>\n<pre><code class=\"language-python\">!python carspackage\/cars.py\n<\/code><\/pre>\n<p>When run this way, all the code inside the guard will get executed.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Any Python file with a .py extension is a Module in Python. A python package is a collection of such modules along with a __init__.py file. Let&#8217;s understand how to work with modules and packages with clear examples. Introduction When you work on python projects, it&#8217;s not a good practice to have all you python [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":18003,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"site-sidebar-layout":"default","site-content-layout":"default","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"default","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[21],"tags":[1986,1987,22],"class_list":["post-18009","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-modules","tag-packages","tag-python"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python Module - What are modules and packages in python? - machinelearningplus<\/title>\n<meta name=\"description\" content=\"Any Python files is a Module in Python. A python package is a collection of such modules along with a `__init__.py` file. Let&#039;s understand how to work with modules and packages with clear examples.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/localhost:8080\/python\/python-module-packages\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Module - What are modules and packages in python? - machinelearningplus\" \/>\n<meta property=\"og:description\" content=\"Any Python files is a Module in Python. A python package is a collection of such modules along with a `__init__.py` file. Let&#039;s understand how to work with modules and packages with clear examples.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/localhost:8080\/python\/python-module-packages\/\" \/>\n<meta property=\"og:site_name\" content=\"machinelearningplus\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/rtipaday\/\" \/>\n<meta property=\"article:published_time\" content=\"2022-02-22T06:02:55+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-03-08T15:30:07+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/localhost:8080\/wp-content\/uploads\/2022\/02\/Python-1.png\" \/>\n\t<meta property=\"og:image:width\" content=\"200\" \/>\n\t<meta property=\"og:image:height\" content=\"200\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Selva Prabhakaran\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/R_Programming\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Selva Prabhakaran\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"TechArticle\",\"@id\":\"https:\\\/\\\/localhost:8080\\\/python\\\/python-module-packages\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/localhost:8080\\\/python\\\/python-module-packages\\\/\"},\"author\":{\"name\":\"Selva Prabhakaran\",\"@id\":\"https:\\\/\\\/machinelearningplus.com\\\/#\\\/schema\\\/person\\\/510885c0515804366fa644c38258391e\"},\"headline\":\"Python Module &#8211; What are modules and packages in python?\",\"datePublished\":\"2022-02-22T06:02:55+00:00\",\"dateModified\":\"2022-03-08T15:30:07+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/localhost:8080\\\/python\\\/python-module-packages\\\/\"},\"wordCount\":995,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/machinelearningplus.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/localhost:8080\\\/python\\\/python-module-packages\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/machinelearningplus.com\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/Python-1.png\",\"keywords\":[\"Modules\",\"Packages\",\"Python\"],\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/localhost:8080\\\/python\\\/python-module-packages\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/localhost:8080\\\/python\\\/python-module-packages\\\/\",\"url\":\"https:\\\/\\\/localhost:8080\\\/python\\\/python-module-packages\\\/\",\"name\":\"Python Module - What are modules and packages in python? - machinelearningplus\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/machinelearningplus.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/localhost:8080\\\/python\\\/python-module-packages\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/localhost:8080\\\/python\\\/python-module-packages\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/machinelearningplus.com\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/Python-1.png\",\"datePublished\":\"2022-02-22T06:02:55+00:00\",\"dateModified\":\"2022-03-08T15:30:07+00:00\",\"description\":\"Any Python files is a Module in Python. A python package is a collection of such modules along with a `__init__.py` file. Let's understand how to work with modules and packages with clear examples.\",\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/localhost:8080\\\/python\\\/python-module-packages\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/localhost:8080\\\/python\\\/python-module-packages\\\/#primaryimage\",\"url\":\"https:\\\/\\\/machinelearningplus.com\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/Python-1.png\",\"contentUrl\":\"https:\\\/\\\/machinelearningplus.com\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/Python-1.png\",\"width\":200,\"height\":200,\"caption\":\"Python\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/machinelearningplus.com\\\/#website\",\"url\":\"https:\\\/\\\/machinelearningplus.com\\\/\",\"name\":\"machinelearningplus\",\"description\":\"Learn Data Science (AI \\\/ ML) Online\",\"publisher\":{\"@id\":\"https:\\\/\\\/machinelearningplus.com\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/machinelearningplus.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/machinelearningplus.com\\\/#organization\",\"name\":\"machinelearningplus\",\"url\":\"https:\\\/\\\/machinelearningplus.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/machinelearningplus.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/machinelearningplus.com\\\/wp-content\\\/uploads\\\/2022\\\/05\\\/MachineLearningplus-logo.svg\",\"contentUrl\":\"https:\\\/\\\/machinelearningplus.com\\\/wp-content\\\/uploads\\\/2022\\\/05\\\/MachineLearningplus-logo.svg\",\"width\":348,\"height\":36,\"caption\":\"machinelearningplus\"},\"image\":{\"@id\":\"https:\\\/\\\/machinelearningplus.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/machinelearningplus.com\\\/#\\\/schema\\\/person\\\/510885c0515804366fa644c38258391e\",\"name\":\"Selva Prabhakaran\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/machinelearningplus.com\\\/wp-content\\\/litespeed\\\/avatar\\\/a994280177da541405c016f593e86ea7.jpg?ver=1776363207\",\"url\":\"https:\\\/\\\/machinelearningplus.com\\\/wp-content\\\/litespeed\\\/avatar\\\/a994280177da541405c016f593e86ea7.jpg?ver=1776363207\",\"contentUrl\":\"https:\\\/\\\/machinelearningplus.com\\\/wp-content\\\/litespeed\\\/avatar\\\/a994280177da541405c016f593e86ea7.jpg?ver=1776363207\",\"caption\":\"Selva Prabhakaran\"},\"description\":\"Selva is an experienced Data Scientist and leader, specializing in executing AI projects for large companies. Selva started machinelearningplus to make Data Science \\\/ ML \\\/ AI accessible to everyone. The website enjoys 4 Million+ readership. His courses, lessons, and videos are loved by hundreds of thousands of students and practitioners.\",\"sameAs\":[\"https:\\\/\\\/localhost:8080\\\/\",\"https:\\\/\\\/www.facebook.com\\\/rtipaday\\\/\",\"https:\\\/\\\/x.com\\\/https:\\\/\\\/twitter.com\\\/R_Programming\"],\"url\":\"https:\\\/\\\/machinelearningplus.com\\\/author\\\/selva86\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Module - What are modules and packages in python? - machinelearningplus","description":"Any Python files is a Module in Python. A python package is a collection of such modules along with a `__init__.py` file. Let's understand how to work with modules and packages with clear examples.","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:\/\/localhost:8080\/python\/python-module-packages\/","og_locale":"en_US","og_type":"article","og_title":"Python Module - What are modules and packages in python? - machinelearningplus","og_description":"Any Python files is a Module in Python. A python package is a collection of such modules along with a `__init__.py` file. Let's understand how to work with modules and packages with clear examples.","og_url":"https:\/\/localhost:8080\/python\/python-module-packages\/","og_site_name":"machinelearningplus","article_author":"https:\/\/www.facebook.com\/rtipaday\/","article_published_time":"2022-02-22T06:02:55+00:00","article_modified_time":"2022-03-08T15:30:07+00:00","og_image":[{"width":200,"height":200,"url":"https:\/\/localhost:8080\/wp-content\/uploads\/2022\/02\/Python-1.png","type":"image\/png"}],"author":"Selva Prabhakaran","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/R_Programming","twitter_misc":{"Written by":"Selva Prabhakaran","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"TechArticle","@id":"https:\/\/localhost:8080\/python\/python-module-packages\/#article","isPartOf":{"@id":"https:\/\/localhost:8080\/python\/python-module-packages\/"},"author":{"name":"Selva Prabhakaran","@id":"https:\/\/machinelearningplus.com\/#\/schema\/person\/510885c0515804366fa644c38258391e"},"headline":"Python Module &#8211; What are modules and packages in python?","datePublished":"2022-02-22T06:02:55+00:00","dateModified":"2022-03-08T15:30:07+00:00","mainEntityOfPage":{"@id":"https:\/\/localhost:8080\/python\/python-module-packages\/"},"wordCount":995,"commentCount":0,"publisher":{"@id":"https:\/\/machinelearningplus.com\/#organization"},"image":{"@id":"https:\/\/localhost:8080\/python\/python-module-packages\/#primaryimage"},"thumbnailUrl":"https:\/\/machinelearningplus.com\/wp-content\/uploads\/2022\/02\/Python-1.png","keywords":["Modules","Packages","Python"],"articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/localhost:8080\/python\/python-module-packages\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/localhost:8080\/python\/python-module-packages\/","url":"https:\/\/localhost:8080\/python\/python-module-packages\/","name":"Python Module - What are modules and packages in python? - machinelearningplus","isPartOf":{"@id":"https:\/\/machinelearningplus.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/localhost:8080\/python\/python-module-packages\/#primaryimage"},"image":{"@id":"https:\/\/localhost:8080\/python\/python-module-packages\/#primaryimage"},"thumbnailUrl":"https:\/\/machinelearningplus.com\/wp-content\/uploads\/2022\/02\/Python-1.png","datePublished":"2022-02-22T06:02:55+00:00","dateModified":"2022-03-08T15:30:07+00:00","description":"Any Python files is a Module in Python. A python package is a collection of such modules along with a `__init__.py` file. Let's understand how to work with modules and packages with clear examples.","inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/localhost:8080\/python\/python-module-packages\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/localhost:8080\/python\/python-module-packages\/#primaryimage","url":"https:\/\/machinelearningplus.com\/wp-content\/uploads\/2022\/02\/Python-1.png","contentUrl":"https:\/\/machinelearningplus.com\/wp-content\/uploads\/2022\/02\/Python-1.png","width":200,"height":200,"caption":"Python"},{"@type":"WebSite","@id":"https:\/\/machinelearningplus.com\/#website","url":"https:\/\/machinelearningplus.com\/","name":"machinelearningplus","description":"Learn Data Science (AI \/ ML) Online","publisher":{"@id":"https:\/\/machinelearningplus.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/machinelearningplus.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/machinelearningplus.com\/#organization","name":"machinelearningplus","url":"https:\/\/machinelearningplus.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/machinelearningplus.com\/#\/schema\/logo\/image\/","url":"https:\/\/machinelearningplus.com\/wp-content\/uploads\/2022\/05\/MachineLearningplus-logo.svg","contentUrl":"https:\/\/machinelearningplus.com\/wp-content\/uploads\/2022\/05\/MachineLearningplus-logo.svg","width":348,"height":36,"caption":"machinelearningplus"},"image":{"@id":"https:\/\/machinelearningplus.com\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/machinelearningplus.com\/#\/schema\/person\/510885c0515804366fa644c38258391e","name":"Selva Prabhakaran","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/machinelearningplus.com\/wp-content\/litespeed\/avatar\/a994280177da541405c016f593e86ea7.jpg?ver=1776363207","url":"https:\/\/machinelearningplus.com\/wp-content\/litespeed\/avatar\/a994280177da541405c016f593e86ea7.jpg?ver=1776363207","contentUrl":"https:\/\/machinelearningplus.com\/wp-content\/litespeed\/avatar\/a994280177da541405c016f593e86ea7.jpg?ver=1776363207","caption":"Selva Prabhakaran"},"description":"Selva is an experienced Data Scientist and leader, specializing in executing AI projects for large companies. Selva started machinelearningplus to make Data Science \/ ML \/ AI accessible to everyone. The website enjoys 4 Million+ readership. His courses, lessons, and videos are loved by hundreds of thousands of students and practitioners.","sameAs":["https:\/\/localhost:8080\/","https:\/\/www.facebook.com\/rtipaday\/","https:\/\/x.com\/https:\/\/twitter.com\/R_Programming"],"url":"https:\/\/machinelearningplus.com\/author\/selva86\/"}]}},"_links":{"self":[{"href":"https:\/\/machinelearningplus.com\/wp-json\/wp\/v2\/posts\/18009","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/machinelearningplus.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/machinelearningplus.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/machinelearningplus.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/machinelearningplus.com\/wp-json\/wp\/v2\/comments?post=18009"}],"version-history":[{"count":0,"href":"https:\/\/machinelearningplus.com\/wp-json\/wp\/v2\/posts\/18009\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/machinelearningplus.com\/wp-json\/wp\/v2\/media\/18003"}],"wp:attachment":[{"href":"https:\/\/machinelearningplus.com\/wp-json\/wp\/v2\/media?parent=18009"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/machinelearningplus.com\/wp-json\/wp\/v2\/categories?post=18009"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/machinelearningplus.com\/wp-json\/wp\/v2\/tags?post=18009"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}