Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'm not sure if this is the 'best practice' or not (I've not put much python software into proper distribution), but I would just make sure that the top-level app package was in <code>sys.path</code>. Something like putting the following into the top-level <code>__init__.py</code>:</p> <pre><code>try: import myapp except ImportError: import sys from os.path import abspath, dirname, split parent_dir = split(dirname(abspath(__file__)))[0] sys.path.append(parent_dir) </code></pre> <p>I think that should do the right thing in a cross platform way.</p> <p>EDIT: As kaizer.se points out this might not work in the <code>__init__.py</code> file, depending on how the code you're invoking is getting executed. It would only work if that file is evaluated. The key is to make sure that the top-level package is in <code>sys.path</code> from some the code that actually is running.</p> <p>Often times, so that I an execute individual files inside of a package directly (for testing with the <code>if __name__ eq '__main__'</code> idiom), I'll do something like place a statement:</p> <pre><code>import _setup </code></pre> <p>At the top of the individual file in question, and then create a file <code>_setup.py</code> which does the path munging as necessary. So, something like:</p> <pre><code>package/ __init__.py _setup.py mod1/ __init__.py _setup.py somemodule.py </code></pre> <p>If you <code>import _setup</code> from <code>somemodule.py</code>, that setup file can ensure that the top level package is in <code>sys.path</code> before the rest of the code in <code>somemodule.py</code> is evaluated.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload