{"id":11945,"date":"2021-05-21T07:30:52","date_gmt":"2021-05-21T02:00:52","guid":{"rendered":"https:\/\/web.archive.org\/web\/20240926035341\/https:\/\/www.pythonpool.com\/?p=11945"},"modified":"2021-05-21T07:30:55","modified_gmt":"2021-05-21T02:00:55","slug":"python-conditional-import","status":"publish","type":"post","link":"https:\/\/web.archive.org\/web\/20240926035341\/https:\/\/www.pythonpool.com\/python-conditional-import\/","title":{"rendered":"Learning about Python Conditional Import"},"content":{"rendered":"\n<p>Python conditional import comes into use while importing modules. In python, modules are pieces of code containing a set of functions that can be imported into other code. Modules <a href=\"https:\/\/www.pythonpool.com\/how-to-check-python-version\/\" target=\"_blank\" rel=\"noreferrer noopener\"><span style=\"text-decoration: underline;\">help<\/span><\/a> in managing code by supporting readability. We don&#8217;t have to write the same piece of code every time. Instead, we can import the modules and avoid developing redundant codes. The syntax for importing a module is <em><code>import module_name<\/code><\/em>.<\/p>\n\n\n\n<p><strong>But sometimes, when the module does not exist, or the importing was not successful, then python will throw ImportError. That is when conditional import comes into play. So using python conditional import, if a given import statement is not executing successfully, we can provide an alternate solution. <\/strong><\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_65 counter-hierarchy ez-toc-counter ez-toc-transparent ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title \" >Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #990303;color:#990303\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #990303;color:#990303\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 eztoc-toggle-hide-by-default' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.pythonpool.com\/python-conditional-import\/#What_is_ImportError\" title=\"What is ImportError?\">What is ImportError?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.pythonpool.com\/python-conditional-import\/#Need_of_conditional_imports\" title=\"Need of conditional imports\">Need of conditional imports<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.pythonpool.com\/python-conditional-import\/#Conditional_import_for_version_checking\" title=\"Conditional import for version checking\">Conditional import for version checking<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.pythonpool.com\/python-conditional-import\/#Contional_import_for_other_conditional_statements\" title=\"Contional import for other conditional statements\">Contional import for other conditional statements<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.pythonpool.com\/python-conditional-import\/#Conditional_import_by_exception_handling\" title=\"Conditional import by exception handling\">Conditional import by exception handling<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.pythonpool.com\/python-conditional-import\/#FAQs_on_python_conditional_import\" title=\"FAQ&#8217;s on python conditional import\">FAQ&#8217;s on python conditional import<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\" id=\"h-what-is-importerror\"><span class=\"ez-toc-section\" id=\"What_is_ImportError\"><\/span>What is ImportError?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>We use the keyword &#8216;import&#8217; while importing a module. But sometimes, python will throw an error while importing a module. The error would be ImportError. As the name suggests, ImportError arises when python fails to execute the import statement. Usually, the import error arises in any of the two cases:<\/p>\n\n\n\n<ul><li>If the module does not longer exist<\/li><li>If you are trying to import a submodule from a module and the submodule does not exist in that module. <\/li><\/ul>\n\n\n\n<p>The syntax for importing a submodule from a module is:                                                             <\/p>\n\n\n\n<pre class=\"wp-block-preformatted has-medium-font-size\"><em>from modulename import submodulename<\/em>.<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-need-of-conditional-imports\"><span class=\"ez-toc-section\" id=\"Need_of_conditional_imports\"><\/span>Need of conditional imports<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>While importing modules, it fails because when <a href=\"https:\/\/www.pythonpool.com\/how-to-check-python-version\/\" target=\"_blank\" rel=\"noreferrer noopener\"><span style=\"text-decoration: underline;\">version<\/span><\/a> changes occur in python, some functions are shifted from one module to some other module. Due to <a href=\"https:\/\/www.pythonpool.com\/how-to-check-python-version\/\" target=\"_blank\" rel=\"noreferrer noopener\"><span style=\"text-decoration: underline;\">different versions<\/span><\/a>, the module name of the module in which the function was present, also changes. <\/p>\n\n\n\n<p>Also, if a particular dependency is missing, we can refrain from importing that module and prevent the failure of the code. If a module is to be used only if a particular condition is met, we would want to avoid importing that module if the condition is not met. <\/p>\n\n\n\n<p>Some modules take a lot of import time. But, on the other hand, it will be a waste of computation time if the module is not going to be used at all. So, conditional imports come in handy when we want to avoid those scenarios.  <\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-conditional-import-for-version-checking\"><span class=\"ez-toc-section\" id=\"Conditional_import_for_version_checking\"><\/span><strong>Conditional import for version checking<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>When version changes occur in python, python may fail to import some modules. Because changes in version often mean that the location of a function in a module may have been shifted to another module. <\/p>\n\n\n\n<p>Therefore, it is important to perform python conditional import by <a href=\"https:\/\/www.pythonpool.com\/how-to-check-python-version\/\" target=\"_blank\" rel=\"noreferrer noopener\"><span style=\"text-decoration: underline;\">checking the version<\/span><\/a> before importing a module. <\/p>\n\n\n\n<p>For checking the version, we will have to import the sys module. Sys.version_info will check the python script version.  <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport sys\nif sys.version_info &gt; (version_number,):\n    import module1\nelse:\n    import module2\n<\/pre><\/div>\n\n\n<p>Here, it will first check the version. If the version is greater than the specified version, then python will import module1. If the version is less than the given version, then it will import module2. <\/p>\n\n\n\n<p>In this way, we would import the correct module, which will be compatible with the <a href=\"https:\/\/www.pythonpool.com\/how-to-check-python-version\/\" target=\"_blank\" rel=\"noreferrer noopener\"><strong><span style=\"text-decoration: underline;\">python version<\/span><\/strong><\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-contional-import-for-other-conditional-statements\"><span class=\"ez-toc-section\" id=\"Contional_import_for_other_conditional_statements\"><\/span><strong>Contional import for other condition<\/strong>al statements<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>We can also use python conditional import for handling other conditional statements. Like we checked the condition for <a href=\"https:\/\/www.pythonpool.com\/how-to-check-python-version\/\" target=\"_blank\" rel=\"noreferrer noopener\"><span style=\"text-decoration: underline;\">python&#8217;s version<\/span><\/a>, we can also do that for other conditions. <\/p>\n\n\n\n<p>For example, we can choose to import a certain module only if a given variable has a certain value. And if the variable&#8217;s value is different, we would want to import some other module.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nif var == value1:\n  import module1\nelif var == value2:\n  import module2\n<\/pre><\/div>\n\n\n<p>In the above example, the value of the variable <em>var<\/em> is the conditional criteria. If the value of <em>var<\/em> is equal to <em>value1<\/em>, python will import module1. But, if the value of variable <em>var<\/em> is equal to <em>value2<\/em>, then python would import module2. <\/p>\n\n\n\n<p>This way, we can avoid importing the wrong module and instead import the module which suits our program&#8217;s requirements.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-conditional-import-by-exception-handling\"><span class=\"ez-toc-section\" id=\"Conditional_import_by_exception_handling\"><\/span><strong>Conditional import<\/strong> by exception handling<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Another way of including python conditional import is by exceptional handling. The code will contain a try expression. If the execution of the code inside the try block fails, then python would execute the except block. <\/p>\n\n\n\n<p>We would import a given module inside the try block. If the given module does not work in terms of version compatibility or python fails to import it, it will go to the except block. <\/p>\n\n\n\n<p>If the thrown <a href=\"https:\/\/www.pythonpool.com\/expected-an-indented-block-solved\/\" target=\"_blank\" rel=\"noreferrer noopener\"><span style=\"text-decoration: underline;\">exception<\/span><\/a> is similar to the exception in the except statement, then python would execute the lines of code written in except block. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ntry:\n    import module1\nexcept ImportError:\n    import module2\n<\/pre><\/div>\n\n\n<p>Here first, we will try to import module1. If that does not work, then try will be thrown an ImportError, and except block will catch that error. And by doing so, it will <strong>import module2.<\/strong><\/p>\n\n\n\n<p>We can also use exception handling to check if a given module exists or not. Python will import a module only if it exists in the program and prevent the unnecessary error from being raised in the program. We can either print a message or pass. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ntry:\n    import module1\nexcept ImportError:\n    pass\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-faq-s-on-python-conditional-import\"><span class=\"ez-toc-section\" id=\"FAQs_on_python_conditional_import\"><\/span>FAQ&#8217;s on python conditional import<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-q-what-is-selective-import-in-python\">Q. <strong>What is selective import in python?<\/strong><\/h4>\n\n\n\n<p><strong>A. <\/strong>In python, when we import using the import <a href=\"https:\/\/docs.python.org\/3\/library\/keyword.html\" target=\"_blank\" rel=\"noreferrer noopener\">keyword<\/a>, we are importing the entire package. But, instead, if we want to import only a single functionality, we use selective import.                   If we execute <em>:<\/em><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport sklearn\n<\/pre><\/div>\n\n\n<p>The above would import the entire sklearn module. But, if we want only to import the class <strong>linear_model<\/strong>, we will use selective import.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfrom sklearn import linear_model\n<\/pre><\/div>\n\n\n<h4 class=\"wp-block-heading\" id=\"h-q-what-is-python-dynamic-import\">Q. <strong>What is python dynamic import?<\/strong><\/h4>\n\n\n\n<p><strong>A. <\/strong>Python allows us to import modules dynamically. By doing so, we will be able to import modules without knowing beforehand which module exactly. There are two main functions for the importing modules dynamically &#8211;<strong> <\/strong><\/p>\n\n\n\n<p><strong>__import__()<\/strong> function and <strong>importlib.import_module()<\/strong> function. <\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-q-what-is-optional-import\">Q. <strong>What is optional import?<\/strong><\/h4>\n\n\n\n<p><strong>A.<\/strong> Optional Import is a library in python which is used for handling ImportError. If we use optional import, it will prevent ImportError from being raised.<\/p>\n\n\n\n<p>Have any thought to share? We would love to hear in the comments.<\/p>\n\n\n\n<p><em>Till then, Happy Learning! <\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python conditional import comes into use while importing modules. In python, modules are pieces of code containing a set of functions that can be imported &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Learning about Python Conditional Import\" class=\"read-more button\" href=\"https:\/\/www.pythonpool.com\/python-conditional-import\/#more-11945\" aria-label=\"More on Learning about Python Conditional Import\">Read more<\/a><\/p>\n","protected":false},"author":20,"featured_media":12050,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[15],"tags":[4040,4039,4037,4038],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v20.1 (Yoast SEO v22.4) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Learning about Python Conditional Import - Python Pool<\/title>\n<meta name=\"description\" content=\"If a module does not exist then instead of throwing an ImportError, python conditional import gives alternatives to avoid unnecessary imports\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.pythonpool.com\/python-conditional-import\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Learning about Python Conditional Import\" \/>\n<meta property=\"og:description\" content=\"Python conditional import comes into use while importing modules. In python, modules are pieces of code containing a set of functions that can be imported\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pythonpool.com\/python-conditional-import\/\" \/>\n<meta property=\"og:site_name\" content=\"Python Pool\" \/>\n<meta property=\"article:published_time\" content=\"2021-05-21T02:00:52+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-05-21T02:00:55+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/05\/Learning-about-Python-Conditional-Import.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1350\" \/>\n\t<meta property=\"og:image:height\" content=\"650\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Dhruvi Vikma\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@pythonpool\" \/>\n<meta name=\"twitter:site\" content=\"@pythonpool\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Dhruvi Vikma\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.pythonpool.com\/python-conditional-import\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-conditional-import\/\"},\"author\":{\"name\":\"Dhruvi Vikma\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/person\/bed8fc40c7b71baf7d76b1cfefd79f23\"},\"headline\":\"Learning about Python Conditional Import\",\"datePublished\":\"2021-05-21T02:00:52+00:00\",\"dateModified\":\"2021-05-21T02:00:55+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-conditional-import\/\"},\"wordCount\":955,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.pythonpool.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-conditional-import\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/05\/Learning-about-Python-Conditional-Import.jpg\",\"keywords\":[\"conditional import python\",\"how to write a conditional import statment python\",\"python conditional import of json object\",\"python conditional import os version\"],\"articleSection\":[\"Tutorials\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.pythonpool.com\/python-conditional-import\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.pythonpool.com\/python-conditional-import\/\",\"url\":\"https:\/\/www.pythonpool.com\/python-conditional-import\/\",\"name\":\"Learning about Python Conditional Import - Python Pool\",\"isPartOf\":{\"@id\":\"https:\/\/www.pythonpool.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-conditional-import\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-conditional-import\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/05\/Learning-about-Python-Conditional-Import.jpg\",\"datePublished\":\"2021-05-21T02:00:52+00:00\",\"dateModified\":\"2021-05-21T02:00:55+00:00\",\"description\":\"If a module does not exist then instead of throwing an ImportError, python conditional import gives alternatives to avoid unnecessary imports\",\"breadcrumb\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-conditional-import\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.pythonpool.com\/python-conditional-import\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.pythonpool.com\/python-conditional-import\/#primaryimage\",\"url\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/05\/Learning-about-Python-Conditional-Import.jpg\",\"contentUrl\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/05\/Learning-about-Python-Conditional-Import.jpg\",\"width\":1350,\"height\":650,\"caption\":\"python conditional import\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.pythonpool.com\/python-conditional-import\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.pythonpool.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Learning about Python Conditional Import\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.pythonpool.com\/#website\",\"url\":\"https:\/\/www.pythonpool.com\/\",\"name\":\"Python Pool\",\"description\":\"Your One-Stop Python Learning Destination\",\"publisher\":{\"@id\":\"https:\/\/www.pythonpool.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.pythonpool.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.pythonpool.com\/#organization\",\"name\":\"Python Pool\",\"url\":\"https:\/\/www.pythonpool.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png\",\"contentUrl\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png\",\"width\":452,\"height\":185,\"caption\":\"Python Pool\"},\"image\":{\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/twitter.com\/pythonpool\",\"https:\/\/www.youtube.com\/c\/pythonpool\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/person\/bed8fc40c7b71baf7d76b1cfefd79f23\",\"name\":\"Dhruvi Vikma\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/be16ea990b460da532c2b8cc5b8b658c?s=96&d=wavatar&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/be16ea990b460da532c2b8cc5b8b658c?s=96&d=wavatar&r=g\",\"caption\":\"Dhruvi Vikma\"}}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Learning about Python Conditional Import - Python Pool","description":"If a module does not exist then instead of throwing an ImportError, python conditional import gives alternatives to avoid unnecessary imports","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:\/\/web.archive.org\/web\/20240926035341\/https:\/\/www.pythonpool.com\/python-conditional-import\/","og_locale":"en_US","og_type":"article","og_title":"Learning about Python Conditional Import","og_description":"Python conditional import comes into use while importing modules. In python, modules are pieces of code containing a set of functions that can be imported","og_url":"https:\/\/web.archive.org\/web\/20240926035341\/https:\/\/www.pythonpool.com\/python-conditional-import\/","og_site_name":"Python Pool","article_published_time":"2021-05-21T02:00:52+00:00","article_modified_time":"2021-05-21T02:00:55+00:00","og_image":[{"width":1350,"height":650,"url":"https:\/\/web.archive.org\/web\/20240926035341\/https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/05\/Learning-about-Python-Conditional-Import.jpg","type":"image\/jpeg"}],"author":"Dhruvi Vikma","twitter_card":"summary_large_image","twitter_creator":"@pythonpool","twitter_site":"@pythonpool","twitter_misc":{"Written by":"Dhruvi Vikma","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/web.archive.org\/web\/20240926035341\/https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/web.archive.org\/web\/20240926035341\/https:\/\/www.pythonpool.com\/python-conditional-import\/#article","isPartOf":{"@id":"https:\/\/web.archive.org\/web\/20240926035341\/https:\/\/www.pythonpool.com\/python-conditional-import\/"},"author":{"name":"Dhruvi Vikma","@id":"https:\/\/web.archive.org\/web\/20240926035341\/https:\/\/www.pythonpool.com\/#\/schema\/person\/bed8fc40c7b71baf7d76b1cfefd79f23"},"headline":"Learning about Python Conditional Import","datePublished":"2021-05-21T02:00:52+00:00","dateModified":"2021-05-21T02:00:55+00:00","mainEntityOfPage":{"@id":"https:\/\/web.archive.org\/web\/20240926035341\/https:\/\/www.pythonpool.com\/python-conditional-import\/"},"wordCount":955,"commentCount":0,"publisher":{"@id":"https:\/\/web.archive.org\/web\/20240926035341\/https:\/\/www.pythonpool.com\/#organization"},"image":{"@id":"https:\/\/web.archive.org\/web\/20240926035341\/https:\/\/www.pythonpool.com\/python-conditional-import\/#primaryimage"},"thumbnailUrl":"https:\/\/web.archive.org\/web\/20240926035341\/https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/05\/Learning-about-Python-Conditional-Import.jpg","keywords":["conditional import python","how to write a conditional import statment python","python conditional import of json object","python conditional import os version"],"articleSection":["Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/web.archive.org\/web\/20240926035341\/https:\/\/www.pythonpool.com\/python-conditional-import\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/web.archive.org\/web\/20240926035341\/https:\/\/www.pythonpool.com\/python-conditional-import\/","url":"https:\/\/web.archive.org\/web\/20240926035341\/https:\/\/www.pythonpool.com\/python-conditional-import\/","name":"Learning about Python Conditional Import - Python Pool","isPartOf":{"@id":"https:\/\/web.archive.org\/web\/20240926035341\/https:\/\/www.pythonpool.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/web.archive.org\/web\/20240926035341\/https:\/\/www.pythonpool.com\/python-conditional-import\/#primaryimage"},"image":{"@id":"https:\/\/web.archive.org\/web\/20240926035341\/https:\/\/www.pythonpool.com\/python-conditional-import\/#primaryimage"},"thumbnailUrl":"https:\/\/web.archive.org\/web\/20240926035341\/https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/05\/Learning-about-Python-Conditional-Import.jpg","datePublished":"2021-05-21T02:00:52+00:00","dateModified":"2021-05-21T02:00:55+00:00","description":"If a module does not exist then instead of throwing an ImportError, python conditional import gives alternatives to avoid unnecessary imports","breadcrumb":{"@id":"https:\/\/web.archive.org\/web\/20240926035341\/https:\/\/www.pythonpool.com\/python-conditional-import\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/web.archive.org\/web\/20240926035341\/https:\/\/www.pythonpool.com\/python-conditional-import\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/web.archive.org\/web\/20240926035341\/https:\/\/www.pythonpool.com\/python-conditional-import\/#primaryimage","url":"https:\/\/web.archive.org\/web\/20240926035341\/https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/05\/Learning-about-Python-Conditional-Import.jpg","contentUrl":"https:\/\/web.archive.org\/web\/20240926035341\/https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/05\/Learning-about-Python-Conditional-Import.jpg","width":1350,"height":650,"caption":"python conditional import"},{"@type":"BreadcrumbList","@id":"https:\/\/web.archive.org\/web\/20240926035341\/https:\/\/www.pythonpool.com\/python-conditional-import\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/web.archive.org\/web\/20240926035341\/https:\/\/www.pythonpool.com\/"},{"@type":"ListItem","position":2,"name":"Learning about Python Conditional Import"}]},{"@type":"WebSite","@id":"https:\/\/web.archive.org\/web\/20240926035341\/https:\/\/www.pythonpool.com\/#website","url":"https:\/\/web.archive.org\/web\/20240926035341\/https:\/\/www.pythonpool.com\/","name":"Python Pool","description":"Your One-Stop Python Learning Destination","publisher":{"@id":"https:\/\/web.archive.org\/web\/20240926035341\/https:\/\/www.pythonpool.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/web.archive.org\/web\/20240926035341\/https:\/\/www.pythonpool.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/web.archive.org\/web\/20240926035341\/https:\/\/www.pythonpool.com\/#organization","name":"Python Pool","url":"https:\/\/web.archive.org\/web\/20240926035341\/https:\/\/www.pythonpool.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/web.archive.org\/web\/20240926035341\/https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/","url":"https:\/\/web.archive.org\/web\/20240926035341\/https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png","contentUrl":"https:\/\/web.archive.org\/web\/20240926035341\/https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png","width":452,"height":185,"caption":"Python Pool"},"image":{"@id":"https:\/\/web.archive.org\/web\/20240926035341\/https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/web.archive.org\/web\/20240926035341\/https:\/\/twitter.com\/pythonpool","https:\/\/web.archive.org\/web\/20240926035341\/https:\/\/www.youtube.com\/c\/pythonpool"]},{"@type":"Person","@id":"https:\/\/web.archive.org\/web\/20240926035341\/https:\/\/www.pythonpool.com\/#\/schema\/person\/bed8fc40c7b71baf7d76b1cfefd79f23","name":"Dhruvi Vikma","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/web.archive.org\/web\/20240926035341\/https:\/\/www.pythonpool.com\/#\/schema\/person\/image\/","url":"https:\/\/web.archive.org\/web\/20240926035341\/https:\/\/secure.gravatar.com\/avatar\/be16ea990b460da532c2b8cc5b8b658c?s=96&d=wavatar&r=g","contentUrl":"https:\/\/web.archive.org\/web\/20240926035341\/https:\/\/secure.gravatar.com\/avatar\/be16ea990b460da532c2b8cc5b8b658c?s=96&d=wavatar&r=g","caption":"Dhruvi Vikma"}}]}},"_links":{"self":[{"href":"https:\/\/web.archive.org\/web\/20240926035341\/https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/11945"}],"collection":[{"href":"https:\/\/web.archive.org\/web\/20240926035341\/https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/web.archive.org\/web\/20240926035341\/https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/web.archive.org\/web\/20240926035341\/https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/users\/20"}],"replies":[{"embeddable":true,"href":"https:\/\/web.archive.org\/web\/20240926035341\/https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/comments?post=11945"}],"version-history":[{"count":23,"href":"https:\/\/web.archive.org\/web\/20240926035341\/https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/11945\/revisions"}],"predecessor-version":[{"id":12052,"href":"https:\/\/web.archive.org\/web\/20240926035341\/https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/11945\/revisions\/12052"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/web.archive.org\/web\/20240926035341\/https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/media\/12050"}],"wp:attachment":[{"href":"https:\/\/web.archive.org\/web\/20240926035341\/https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/media?parent=11945"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/web.archive.org\/web\/20240926035341\/https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/categories?post=11945"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/web.archive.org\/web\/20240926035341\/https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/tags?post=11945"}],"curies":[{"name":"wp","href":"https:\/\/web.archive.org\/web\/20240926035341\/https:\/\/api.w.org\/{rel}","templated":true}]}}