I have compiled Blender as a python module:
ant@asus:~$ ls -l /home/ant/.local/lib/python3.6/site-packages/bpy/
drwxrwxr-x 3 ant ant 4096 Jun 6 14:44 2.79
-rwxrwxr-x 1 ant ant 64221200 Jun 6 14:44 bpy.so
If I run python from package folder - import works fine:
ant@asus:~/.local/lib/python3.6/site-packages/bpy/$ python3
>>> import bpy
Color management: using fallback mode for management
>>> dir(bpy)
['__all__', '__builtins__', '__cached__', '__doc__', '__file__',
'__loader__', '__name__', '__package__', '__path__', '__spec__',
'app', 'context', 'data', 'ops', 'path', 'props', 'types', 'utils']
But normal import gives empty module:
ant@asus:~$ python3
>>> import bpy
>>> dir(bpy)
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__',
'__name__', '__package__', '__path__', '__spec__']
I've tried to add file
__init__.py
with different content:
import bpy
ant@asus:~$ python3
>>> dir(bpy)
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__',
'__name__', '__package__', '__path__', '__spec__', 'bpy']
>>> dir(bpy.bpy)
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__',
'__name__', '__package__', '__path__', '__spec__', 'bpy']
import os,ctypes
basedir = os.path.abspath(os.path.dirname(__file__))
libpath = os.path.join(basedir, 'bpy.so')
dll = ctypes.CDLL(libpath)
print(dll)
ant@asus:~$ python3
>>> import bpy
<CDLL '/home/ant/.local/lib/python3.6/site-packages/bpy/bpy.so', handle d06750 at 0x7ff30917deb8>
>>> dir(bpy)
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__',
'__name__', '__package__', '__path__', '__spec__', 'basedir',
'ctypes', 'dll', 'libpath', 'os']
>>> dir(bpy.dll)
['_FuncPtr', '__class__', '__delattr__', '__dict__', '__dir__',
'__doc__', '__eq__', '__format__', '__ge__', '__getattr__',
'__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__',
'__init_subclass__', '__le__', '__lt__', '__module__', '__ne__',
'__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__',
'__sizeof__', '__str__', '__subclasshook__', '__weakref__',
'_func_flags_', '_func_restype_', '_handle', '_name']
This variant seems almost working, but gives a lot of errors from blender:
from . import bpy
ant@asus:~$ python3
>>> import bpy
Color management: using fallback mode for management
RNA_string_set: OperatorProperties.data_path not found.
RNA_string_set: OperatorProperties.value not found.
...
There were also a lot of attempts to change PYTHONPATH, sys.path and so on.
Finally, using strace, I've found the solution - just rename
bpy.so -> __init__.so
This works, but looks too hacky. So, how should it be done in the right way?