{"id":122641,"date":"2020-12-29T16:37:27","date_gmt":"2020-12-29T16:37:27","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=19786"},"modified":"2020-12-29T16:37:27","modified_gmt":"2020-12-29T16:37:27","slug":"python-how-to-import-modules-from-another-folder","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2020\/12\/29\/python-how-to-import-modules-from-another-folder\/","title":{"rendered":"Python \u2014 How to Import Modules From Another Folder?"},"content":{"rendered":"<p class=\"has-pale-cyan-blue-background-color has-background\">The most Pythonic way to <strong><em>import a module from another folder <\/em><\/strong>is to place an empty file named <code>__init__.py<\/code> into that folder and use the <strong><em>relative path with the dot notation<\/em><\/strong>. For example, a module in the parent folder would be imported with <code>from .. import module<\/code>. The <code>__init__.py<\/code> file signals to Python that the folder should be treated as <strong><em>package<\/em><\/strong>. <\/p>\n<p><strong>Problem<\/strong>: How to import a file or a module from another folder or directory in Python?<\/p>\n<p><strong>Example<\/strong>: Say, you&#8217;ve given the following folder structure:<\/p>\n<pre class=\"wp-block-preformatted\">application \u251c\u2500\u2500 app \u2502 \u2514\u2500\u2500 folder \u2502 \u2514\u2500\u2500 file_1.py \u2514\u2500\u2500 app2 \u2514\u2500\u2500 some_folder \u2514\u2500\u2500 file_2.py<\/pre>\n<p>Your goal is to import functions from <code>file_1.py<\/code> in <code>file_2.py<\/code>.<\/p>\n<h2>Method 1: sys.path.append()<\/h2>\n<p>The first method appends the path of the <code>file_1.py<\/code> to the system&#8217;s path variable. <\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># file_2.py\nimport sys\nsys.path.append('\/...\/application\/app\/folder') import file_1<\/pre>\n<p>Note that you need to replace the first three dots in <code>'\/\u2026\/application\/app\/folder'<\/code> with the concrete path to the <code>applications<\/code> folder. <\/p>\n<h2>Method 2: sys.path.insert()<\/h2>\n<p>A similar alternative is to insert the path of <code>file_1.py<\/code> to position 1 of the system&#8217;s path variable. This ensures that it&#8217;s loaded with higher priority and avoids some naming conflicts:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># file_2.py\nimport sys\nsys.path.insert(1, '\/...\/application\/app\/folder') import file<\/pre>\n<p>Again, replace the first three dots in <code>'\/\u2026\/application\/app\/folder'<\/code> with the concrete path to the <code>applications<\/code> folder.<\/p>\n<h2>Method 3: Dot Notation with __init__.py<\/h2>\n<p>You can also do the following trick&#8212;creating a new package.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># file_2.py\nfrom application.app.folder.file_1 import func_name<\/pre>\n<p>However, you need to make sure to include an empty <code>__init__.py<\/code> file in the directory. This file tells Python to treat the directory as a package. It is considered to be the most Pythonic way of solving this problem. <\/p>\n<h2>Method 4: Importlib<\/h2>\n<p>A not-so Pythonic alternative is to use the <code>importlib<\/code> module:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import importlib.util\nspec = importlib.util.spec_from_file_location(\"file_2\", '\/...\/application\/app\/folder')\nlib = importlib.util.module_from_spec(spec)\nspec.loader.exec_module(foo)\nlib.function()<\/pre>\n<p><strong>References<\/strong><\/p>\n<ul>\n<li><a href=\"https:\/\/stackoverflow.com\/questions\/4383571\/importing-files-from-different-folder\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/stackoverflow.com\/questions\/4383571\/importing-files-from-different-folder<\/a><\/li>\n<li><a href=\"https:\/\/stackoverflow.com\/questions\/67631\/how-to-import-a-module-given-the-full-path?rq=1\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/stackoverflow.com\/questions\/67631\/how-to-import-a-module-given-the-full-path?rq=1<\/a><\/li>\n<\/ul>\n<h2>Where to Go From Here?<\/h2>\n<p>Enough theory, let\u2019s get some practice!<\/p>\n<p>To become successful in coding, you need to get out there and solve real problems for real people. That\u2019s how you can become a six-figure earner easily. And that\u2019s how you polish the skills you really need in practice. After all, what\u2019s the use of learning theory that nobody ever needs?<\/p>\n<p><strong>Practice projects is how you sharpen your saw in coding!<\/strong><\/p>\n<p>Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?<\/p>\n<p>Then become a Python freelance developer! It\u2019s the best way of approaching the task of improving your Python skills\u2014even if you are a complete beginner.<\/p>\n<p>Join my free webinar <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/webinar-freelancer\/\" target=\"_blank\">\u201cHow to Build Your High-Income Skill Python\u201d<\/a> and watch how I grew my coding business online and how you can, too\u2014from the comfort of your own home.<\/p>\n<p><a href=\"https:\/\/blog.finxter.com\/webinar-freelancer\/\" target=\"_blank\" rel=\"noreferrer noopener\">Join the free webinar now!<\/a><\/p>\n<p>The post <a href=\"https:\/\/blog.finxter.com\/python-how-to-import-modules-from-another-folder\/\" target=\"_blank\" rel=\"noopener noreferrer\">Python &#8212; How to Import Modules From Another Folder?<\/a> first appeared on <a href=\"https:\/\/blog.finxter.com\/\" target=\"_blank\" rel=\"noopener noreferrer\">Finxter<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The most Pythonic way to import a module from another folder is to place an empty file named __init__.py into that folder and use the relative path with the dot notation. For example, a module in the parent folder would be imported with from .. import module. The __init__.py file signals to Python that the [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-122641","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/122641","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=122641"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/122641\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=122641"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=122641"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=122641"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}