Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><a href="https://stackoverflow.com/questions/1675734/how-do-i-create-a-namespace-package-in-python">This</a> is the solution to my problem, albeit it might not be obvious at first.</p> <p>In my projects, I have now introduced a convention of one namespace per customer. In every customer folder (<code>cust1</code>, <code>cust2</code>, etc.), there is an <code>__init__.py</code> file with this code:</p> <pre><code>import pkgutil __path__ = pkgutil.extend_path(__path__, __name__) </code></pre> <p>All the other <code>__init__.py</code> files in my packages are empty (mostly because I haven't had the time yet to find out what else to do with them).</p> <p>As explained <a href="http://www.doughellmann.com/PyMOTW/pkgutil/" rel="nofollow noreferrer">here</a>, <code>extend_path</code> makes sure Python is aware there is more than one sub-package within a package, physically located elsewhere and - from what I understand - the interpreter then does not stop searching after it fails to find a module under the first package path it encounters in <code>sys.path</code>, but searches all paths in <code>__path__</code>.</p> <p>I can now access all code in a consistent manner criss-cross between all projects, e.g.</p> <pre><code>from cust1.proj1.pack1.mod1 import something from cust3.proj4.pack1.mod4 import something_else from cust3.proj1.pack4.mod4 import yet_something_else </code></pre> <p>On a downside, I had to create an even deeper project folder structure:</p> <pre><code>/path/ to/ projects/ cust1/ proj1/ Development/ code/ python/ cust1/ __init__.py &lt;--- contains code as described above proj1/ __init__.py &lt;--- empty pack1/ __init__.py &lt;--- empty mod1.py </code></pre> <p>but that seems very acceptable to me, especially considering how little effort I need to make to maintain this convention. <code>sys.path</code> is extended by <code>/path/to/projects/cust1/proj1/Development/code/python</code> for this project.</p> <p>On a sidenote, I noticed that of all the <code>__init__.py</code> files for the same customer, the one in the path that appears first in <code>sys.path</code> is executed, no matter from which project I import something.</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