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

I am writing code in cython and I have “Packages” inside “modules” . - Actually, it depends on my question, and the structure should be the same. The problem is that it is cython, so I am dealing with .so not .py .

Having __init__.so works to such an extent that

 import mystuff 

will work, but it seems to mask all the subpackages below. I.e

 import mystuff.test.test1 

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

How can I get around this? Is there anything I need to add to .pyx before compiling it to .c ? Or maybe I can rename __init__.so to something else and somehow pull it into __init__.py (Note: __init__.py still needs to exist with .so to show it the package)? Or something else.


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

Well, I had the thought that maybe I could get around this by manipulating the __path__ attribute of this package. Interestingly, this is specific to .py packages, but causes an error with .so . This is not a solution, but I wonder if it is the root of the problem.

+4
source share
2 answers

Your Cython code has a file other than __init__.py and import it into regular python __init__.py See my answer to the previous question.

+2
source

Probably the easiest solution to this problem would be to rename your __init__.so module to something like _native.so . Subsequently, you can create __init__.py , which will contain the following line:

 from _native import * 

And it should work as you describe.

+2
source

Source: https://habr.com/ru/post/1380262/


All Articles