我找到相關的代碼在PyStan項目, 具體而言,pystan.model
模塊,其本質不正是我需要英寸我會在這裏爲後代編寫代碼片段。下面甚至可以處理Cython擴展,但如果你不需要,它可以很容易地適用於刪除Cython的依賴。
from distutils.core import Extension
import Cython
from Cython.Build.Inline import _get_build_extension
from Cython.Build.Dependencies import cythonize
# you can add include_dirs= and extra_compile_args= here
extension = Extension(name='mymodule', language='c++', sources=[srcfile])
build_extension = _get_build_extension()
build_extension.extensions = cythonize([extension],
include_path=[],
quiet=False)
build_extension.build_temp = os.path.dirname(srcfile)
build_extension.build_lib = build_dir # where you want the output
build_extension.run()
編譯之後,你可以把它變成路徑(或增加其路徑sys.path
)並調用importlib.import_module()
加載模塊。
來源
2017-08-29 09:05:17
cfh