使用共享库导入python包

时间:2018-06-07 17:16:04

标签: python python-import bpy

我编译了Blender as a python module

<p id="example1"></p>
<p id="example2"></p>

如果我从包文件夹运行python - 导入工作正常:

var instance =   new TypeIt('#example1', {
strings: ["FIRST TEXT"],
speed: 75,
autoStart: true
});


while(1)
{
  if(instance.isComplete)
   {
     new TypeIt('#example2', {
     strings: ["TEXT TO APPEAR AFTER FIRST IS COMPLETE"],
     speed: 75,
     autoStart: true
   });
    break;
   }
}

但正常导入会给出空模块:

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

我试图添加文件

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']

还有很多尝试改变PYTHONPATH,sys.path等等。

最后,使用strace,我找到了解决方案 - 只需重命名

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']

这有效,但看起来太hacky。 那么,应该如何以正确的方式完成呢?

0 个答案:

没有答案