How to import python module from .so file?

前端 未结 2 945
滥情空心
滥情空心 2020-12-12 21:13
[me@hostname python]$ cat hello_world.cc
#include 
#include 
#include 

namespace {
  std::string greet() { ret         


        
相关标签:
2条回答
  • 2020-12-12 21:27

    take that 'hello_world.so' file and and make new python file (in the same dir) named as 'hello_world.py'. Put the below code in it.. .

    def __bootstrap__():
       global __bootstrap__, __loader__, __file__
       import sys, pkg_resources, imp
       __file__ = pkg_resources.resource_filename(__name__,'hello_world.so')
       __loader__ = None; del __bootstrap__, __loader__
       imp.load_dynamic(__name__,__file__)
    __bootstrap__()
    

    now you can import this hello_world as:

    >>> import hello_world
    
    0 讨论(0)
  • 2020-12-12 21:42

    It must be called hello_world.so, not libhello_world.so.

    0 讨论(0)
提交回复
热议问题