Import packages from current project directory in VScode
<p>When I build or debug a particular file in my Python project (which imports a user defined package) I get an import error. How can I solve this problem?</p>

<p>test.py</p>

<pre><code>def sum(a,b):
    return a+b
</code></pre>

<p>test2.py</p>

<pre><code>from test import sum
sum(3,4)
</code></pre>

<p>The above code will give an import error <code>cannot import test</code>.</p>

<p>Directory tree</p>

<pre><code>âââ graphs
âÂ Â  âââ Dijkstra's\ Algorithm.py
âÂ Â  âââ Floyd\ Warshall\ DP.py
âÂ Â  âââ Kruskal's\ algorithm.py
âÂ Â  âââ Prim's\ Algoritm.py
âÂ Â  âââ __init__.py
âÂ Â  âââ graph.py
âââ heap
âÂ Â  âââ __init__.py
âÂ Â  âââ heap.py
âÂ Â  âââ priority_queue.py
</code></pre>

<p>Trying to import in graphs;</p>

<pre><code>from heap.heap import Heap
</code></pre>

<p>About the <code>heap</code> file, make sure that you are running on the project root folder.</p>

<p>If these <code>test.py</code> files are running on the same folder, try to add a <code>__init__.py</code> empty file on this folder. </p>

<blockquote>
  <p>The <code>__init__.py</code> files are required to make Python treat the directories as containing packages; this is done to prevent directories with a common name, such as string, from unintentionally hiding valid modules that occur later (deeper) on the module search path. In the simplest case, <code>__init__.py</code> can just be an empty file, but it can also execute initialisation code for the package or set the <code>__all__</code> variable, described later.</p>
</blockquote>

python vscode