12-30-2020, 08:08 AM
Python — How to Import Modules From Another Folder?
<div><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>
<p><strong>Problem</strong>: How to import a file or a module from another folder or directory in Python?</p>
<p><strong>Example</strong>: Say, you’ve given the following folder structure:</p>
<pre class="wp-block-preformatted">application ├── app │ └── folder │ └── file_1.py └── app2 └── some_folder └── file_2.py</pre>
<p>Your goal is to import functions from <code>file_1.py</code> in <code>file_2.py</code>.</p>
<h2>Method 1: sys.path.append()</h2>
<p>The first method appends the path of the <code>file_1.py</code> to the system’s path variable. </p>
<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
import sys
sys.path.append('/.../application/app/folder') import file_1</pre>
<p>Note that you need to replace the first three dots in <code>'/…/application/app/folder'</code> with the concrete path to the <code>applications</code> folder. </p>
<h2>Method 2: sys.path.insert()</h2>
<p>A similar alternative is to insert the path of <code>file_1.py</code> to position 1 of the system’s path variable. This ensures that it’s loaded with higher priority and avoids some naming conflicts:</p>
<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
import sys
sys.path.insert(1, '/.../application/app/folder') import file</pre>
<p>Again, replace the first three dots in <code>'/…/application/app/folder'</code> with the concrete path to the <code>applications</code> folder.</p>
<h2>Method 3: Dot Notation with __init__.py</h2>
<p>You can also do the following trick—creating a new package.</p>
<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
from application.app.folder.file_1 import func_name</pre>
<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>
<h2>Method 4: Importlib</h2>
<p>A not-so Pythonic alternative is to use the <code>importlib</code> module:</p>
<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
spec = importlib.util.spec_from_file_location("file_2", '/.../application/app/folder')
lib = importlib.util.module_from_spec(spec)
spec.loader.exec_module(foo)
lib.function()</pre>
<p><strong>References</strong></p>
<ul>
<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>
<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>
</ul>
<h2>Where to Go From Here?</h2>
<p>Enough theory, let’s get some practice!</p>
<p>To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?</p>
<p><strong>Practice projects is how you sharpen your saw in coding!</strong></p>
<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>
<p>Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.</p>
<p>Join my free webinar <a rel="noreferrer noopener" href="https://blog.finxter.com/webinar-freelancer/" target="_blank">“How to Build Your High-Income Skill Python”</a> and watch how I grew my coding business online and how you can, too—from the comfort of your own home.</p>
<p><a href="https://blog.finxter.com/webinar-freelancer/" target="_blank" rel="noreferrer noopener">Join the free webinar now!</a></p>
<p>The post <a href="https://blog.finxter.com/python-how-to-import-modules-from-another-folder/" target="_blank" rel="noopener noreferrer">Python — 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>
</div>
https://www.sickgaming.net/blog/2020/12/...er-folder/
<div><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>
<p><strong>Problem</strong>: How to import a file or a module from another folder or directory in Python?</p>
<p><strong>Example</strong>: Say, you’ve given the following folder structure:</p>
<pre class="wp-block-preformatted">application ├── app │ └── folder │ └── file_1.py └── app2 └── some_folder └── file_2.py</pre>
<p>Your goal is to import functions from <code>file_1.py</code> in <code>file_2.py</code>.</p>
<h2>Method 1: sys.path.append()</h2>
<p>The first method appends the path of the <code>file_1.py</code> to the system’s path variable. </p>
<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
import sys
sys.path.append('/.../application/app/folder') import file_1</pre>
<p>Note that you need to replace the first three dots in <code>'/…/application/app/folder'</code> with the concrete path to the <code>applications</code> folder. </p>
<h2>Method 2: sys.path.insert()</h2>
<p>A similar alternative is to insert the path of <code>file_1.py</code> to position 1 of the system’s path variable. This ensures that it’s loaded with higher priority and avoids some naming conflicts:</p>
<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
import sys
sys.path.insert(1, '/.../application/app/folder') import file</pre>
<p>Again, replace the first three dots in <code>'/…/application/app/folder'</code> with the concrete path to the <code>applications</code> folder.</p>
<h2>Method 3: Dot Notation with __init__.py</h2>
<p>You can also do the following trick—creating a new package.</p>
<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
from application.app.folder.file_1 import func_name</pre>
<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>
<h2>Method 4: Importlib</h2>
<p>A not-so Pythonic alternative is to use the <code>importlib</code> module:</p>
<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
spec = importlib.util.spec_from_file_location("file_2", '/.../application/app/folder')
lib = importlib.util.module_from_spec(spec)
spec.loader.exec_module(foo)
lib.function()</pre>
<p><strong>References</strong></p>
<ul>
<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>
<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>
</ul>
<h2>Where to Go From Here?</h2>
<p>Enough theory, let’s get some practice!</p>
<p>To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?</p>
<p><strong>Practice projects is how you sharpen your saw in coding!</strong></p>
<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>
<p>Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.</p>
<p>Join my free webinar <a rel="noreferrer noopener" href="https://blog.finxter.com/webinar-freelancer/" target="_blank">“How to Build Your High-Income Skill Python”</a> and watch how I grew my coding business online and how you can, too—from the comfort of your own home.</p>
<p><a href="https://blog.finxter.com/webinar-freelancer/" target="_blank" rel="noreferrer noopener">Join the free webinar now!</a></p>
<p>The post <a href="https://blog.finxter.com/python-how-to-import-modules-from-another-folder/" target="_blank" rel="noopener noreferrer">Python — 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>
</div>
https://www.sickgaming.net/blog/2020/12/...er-folder/