Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you want to load python code dynamically from different locations, you can extend the search __path__ attributes by using the <a href="http://docs.python.org/library/pkgutil.html#pkgutil.extend_path" rel="nofollow">pkgutil module</a>:</p> <p>By placing these lines into each <code>pydraw/__init__.py</code> and <code>pydraw/shapes/__init__.py</code>:</p> <pre><code>from pkgutil import extend_path __path__ = extend_path(__path__, __name__) </code></pre> <p>You will be able to write import statement as if you had a unique package:</p> <pre><code>&gt;&gt;&gt; import pydraw.shapes &gt;&gt;&gt; pydraw.shapes.__path__ ['/usr/lib/python/pydraw/shapes', '/home/someuser/python/pydraw/shapes'] &gt;&gt;&gt; from pydraw.shapes import circle, square &gt;&gt;&gt; </code></pre> <p>You may think about auto-registration of your plugins. You can still use basic python code for that by setting a module variable (which will act as a kind of singleton pattern).</p> <p>Add the last line in every <code>pydraw/shapes/__init__.py</code> file:</p> <pre><code> from pkgutil import extend_path __path__ = extend_path(__path__, __name__) # your shape registry __shapes__ = [] </code></pre> <p>You can now register a shape in top of its related module (<code>circle.py</code> or <code>square.py</code> here).</p> <pre><code> from pydraw.shapes import __shapes__ __shapes__.append(__name__) </code></pre> <p>Last check:</p> <pre><code> &gt;&gt;&gt; from pydraw.shapes import circle,square &gt;&gt;&gt; from pydraw.shapes import circle,square,__shapes__ &gt;&gt;&gt; __shapes__ ['pydraw.shapes.circle', 'pydraw.shapes.square'] </code></pre>
 

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