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
如果我从包文件夹运行 python - 导入工作正常:
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']
但是正常导入会给出空模块:
ant@asus:~$ python3
>>> import bpy
>>> dir(bpy)
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__',
'__name__', '__package__', '__path__', '__spec__']
我试图添加文件
__init__.py
内容不同:
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']
这个变体似乎几乎可以工作,但从搅拌机中给出了很多错误:
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.
...
也有很多尝试更改 PYTHONPATH、sys.path 等。
最后,使用 strace,我找到了解决方案 - 只需重命名
bpy.so -> __init__.so
这可行,但看起来太老套了。那么,应该如何以正确的方式进行呢?