how to call def from another .py in different folder
<p>I have following structure:
utils_dir has generator.py file which has 3 defs.</p>

<p>I have test.py in inline_dir. And I am trying to use defs from generator.py in test.py. </p>

<p>inline_dir and utils_dir are in different folders.
How can I achieve it to use defs?</p>

<p>Tried with creating <code>_init_.py</code> and then calling <code>import generator</code>. - Not worked.
Tried <code>from utils import generator</code> - Not worked</p>

<p><code>Dir structure</code></p>

<p><code>
Support_dir
âââ dir_A
â   âââ dir_aa
â   âââ----- main.py   [Want to use a and b from generator.py]
âââ utils
    |
    âââ generator.py
             |
             |___ def a
             |___ def b
</code></p>

<p>It sounds like you're trying to execute a .py file in a subdirectory.</p>

<p>Assuming the following directory structure:</p>

<pre><code>.
âââ inline
âÂ Â  âââ __init__.py
âÂ Â  âââ main.py
âââ utils
    âââ __init__.py
    âââ generator.py
</code></pre>

<p>And your <code>main.py</code> containing a simple import like (the function <code>a()</code> being defined in <code>generator.py</code>):</p>

<pre><code>from utils.generator import a

if __name__ == '__main__':
    a()
</code></pre>

<p>And <code>generator.py</code> would look something like this:</p>

<pre><code>def a():
    print "hi there"
</code></pre>

<p>You won't be able to run your program using <code>python inline/main.py</code> because this will set the module search path to <code>inline/</code></p>

<p>If you want to execute a file in a subdirectory while importing from your project-level, you could do the following:</p>

<pre><code>PYTHONPATH=. python inline/main.py
</code></pre>

<p><strong>UPDATE:</strong> Added example <code>generator.py</code></p>

python