2

我正在使用 PyBind11 制作 Python 项目。

我的目录结构如下所示:

./
  my_pkg/
    __init__.py
    func1.py
    func2.py

我的 C++ 代码如下所示:

int myfunc(){
  return 1;
}

PYBIND11_PLUGIN(cppmodule) {
  py::module m("cppmodule", "My cpp module");

  m.def("myfunc",&myfunc,"This does stuff");

  return m.ptr();
}

我的setup.py样子是这样的:

from setuptools import setup, Extension
import glob

ext_modules = [
  Extension(
    "cppmodule",
    glob.glob('src/*.cpp'),
    include_dirs       = ['lib/include', 'lib/pybind11/'],
    language           = 'c++',
    extra_compile_args = ['-std=c++17'],
    define_macros      = [('DOCTEST_CONFIG_DISABLE',None)]
  )
]

setup(name = 'bob',
  version      = '0.1',
  description  = 'A package about shrimp',
  url          = 'http://github.com/shrimp',
  author       = 'Bob',
  author_email = '',
  license      = 'MIT',
  ext_modules  = ext_modules
)

现在,如果我跑

python setup.py install

一切都编译。

但这是奇怪的部分,稍后,我可以运行import cppmodule但不能import bob。或者,与其他摆弄,有时我可以同​​时运行。

我还没有想出怎么做,但我想做的是将 C++ 代码bob以相同的方式并入模块func1func2,以便我可以输入bob.myfunc()Python。

我怎样才能做到这一点?

4

1 回答 1

2

答案是将代码修改setup.py为如下所示:

from setuptools import setup, Extension, find_packages

setup(name = 'bob',
  version      = '0.1',
  description  = 'A package about shrimp',
  url          = 'http://github.com/shrimp',
  author       = 'Bob',
  author_email = '',
  license      = 'MIT',
  packages     = find_packages(),
  ext_modules  = ext_modules
)
于 2017-08-17T01:47:53.427 回答