Note that there are some explanatory texts on larger screens.

plurals
  1. POPython C-ext namespace mixed with regular python submodules?
    primarykey
    data
    text
    <p>This question has some resemblance with:</p> <p><a href="https://stackoverflow.com/questions/1681281/nested-python-c-extensions-modules">Nested Python C Extensions/Modules?</a></p> <p>Only with a slight twist. Here I'm not trying to mix two C-exts, but one C-ext and a regular python submodule instead.</p> <p>Is there a way for a C-extension to share the module namespace between the symbols "module.so" and those present in a submodule?</p> <p>My module structure looks like this:</p> <pre><code>facs/ facs/ __init__.py setup.py facs.so [*.c files] utils/ __init__.py galaxy.py </code></pre> <p>If I remove "utils" from the hierarchy, I can import facs and see the <code>facs.so</code> methods:</p> <pre><code>&gt;&gt;&gt; import facs &gt;&gt;&gt; dir(facs) ['__doc__', '__file__', '__name__', '__package__', 'build', 'query', 'remove'] </code></pre> <p>But when I put the utils submodule back and try to import the different parts, one namespace seems to mask the other (<code>utils</code> masks the symbols exported by <code>facs.so</code>):</p> <pre><code>&gt;&gt;&gt; import facs &gt;&gt;&gt; dir(facs) ['__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__'] &gt;&gt;&gt; import facs.utils &gt;&gt;&gt; facs.utils.galaxy.rsync_genomes("phix") Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &lt;module&gt; AttributeError: 'module' object has no attribute 'galaxy' &gt;&gt;&gt; from facs.utils import galaxy &gt;&gt;&gt; galaxy.rsync_genomes("phix") 'Hello world' </code></pre> <p>As you see, after <code>dir(facs)</code>, <code>build</code>, <code>query</code> and <code>remove</code> are gone and <code>galaxy</code> does not get imported properly unless I do a <code>from facs.utils import galaxy</code> instead of re-using the initial import statement and simply accessing via <code>facs.utils.galaxy.rsync_genomes()</code> directly. </p> <p>To sum up, my intended use of this module would be to do:</p> <pre><code>&gt;&gt;&gt; import facs &gt;&gt;&gt; dir(facs) ['__doc__', '__file__', '__name__', '__package__', 'build', 'query', 'remove' , 'utils'] &lt;--- (Directly accessible from "facs") &gt;&gt;&gt; facs.utils.galaxy.rsync_genomes("phix") 'Hello world' </code></pre> <p>The (currently under development) code is in:</p> <p><a href="https://github.com/brainstorm/facs/tree/develop" rel="nofollow noreferrer">https://github.com/brainstorm/facs/tree/develop</a></p> <p>In case someone wants to try it out by themselves. I'm using virtualenvs and my $PYTHONPATH seems correct:</p> <pre><code>/home/roman/.venvburrito/lib/python: /home/roman/.virtualenvs/py27/lib/python2.7/site-packages </code></pre> <p>And the installation seems to be successful too:</p> <pre><code>cd ~/.virtualenvs/py27/lib/python2.7/site-packages/facs-2.0dev-py2.7.egg/ (py27)$ ls EGG-INFO facs.py facs.pyc facs.so utils/ </code></pre> <p>It seems that no <code>__init__.py</code> file is actually copied to the top level directory but touching it there or not does not affect the importing behavior described above.</p> <p>Any ideas? Thanks in advance!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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