Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

__init__.so (instead of __init__.py) masks subpackages

I am writing some code in cython, and I have some "Packages “within” modules". — This is actually a follow up to my question there, and the structure should be the same. The problem is that this is cython, so I'm dealing with .so's not .py's.

Having __init__.so works to the extent that,

import mystuff

will work, but it seems to mask all the subpackages below. That is,

import mystuff.test.test1

will not. I get an ImportError: No module named ... error.

How can I work around this? Is there something I need to add to the .pyx before compiling it to .c? Or perhaps I can rename __init__.so to something else, and somehow pull it into the __init__.py (Note, an __init__.py still needs to exist alongside the .so to show it's a package)? Or something else.


Update: __path__ attribute not defined for .so packages ...

Ok, I had a thought that maybe I could get around this by manipulating the __path__ attribute of the package. Interestingly enough, this is defined for .py packages, but causes an error with the .so's. It's not a solution, but I wonder if it is the root of the problem.

like image 772
tjm Avatar asked Oct 09 '22 13:10

tjm


2 Answers

Have your Cython code be in a different file than __init__.py, and import it into a normal python __init__.py See my answer to your previous question.

like image 68
Ethan Furman Avatar answered Oct 12 '22 11:10

Ethan Furman


Probably the most simple solution to given problem would be to rename your __init__.so module into something like _native.so. Afterwards you can create __init__.py which would contain following line:

from _native import *

And it should work as you describe.

like image 30
Denys Shabalin Avatar answered Oct 12 '22 10:10

Denys Shabalin