Boost.Python 创建的 dll 无法导入(按照 Boost Python 的 QuickStart)

The dll created by Boost.Python cannot be imported (following Boost Python's QuickStart)

我正在尝试按照说明 here 使用 Boost.Python。源代码在该网页中。我可以编译 link 这个简单的示例代码,但我无法在 python 命令行中导入生成的模块。它总是错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named hello_ext

我不知道这是怎么回事,因为那个页面只是说:"That's it. We're done. We can now build this as a shared library. The resulting DLL is now visible to Python."这是我的构建环境:

那么,你能告诉我如何在 python 中导入模块吗?非常感谢。

我自己解决了这个问题。感谢jagerman的实用建议。

(1) 只需将输出文件名从 ConsoleApplication1.dll 更改为 hello_ext.pyd。您可以通过将 Pages->General->Target Extension 设置为“.pyd”来自动重命名。确保文件 hello_ext.pyd 在 python 的搜索路径中。你可以把它扔到 C:\Python27\DLLs 这是 python 的内置搜索路径之一。

(2) 现在您将得到一个不同的导入错误:DLL 加载失败:找不到指定的模块。如果您仔细查看 hello_ext.pyd 的文件大小,您可能会注意到一些有线的东西——它只有 19KB。这意味着它不包含导入 python 所需的所有内容,因此 python 必须找到缺失的部分才能正确导入它。是的,您可能会猜到——唯一可能缺少的东西是 Boost.Python 库,所以将它的路径添加到 PATH 环境变量中——对我来说,它是 C:\local\boost_1_64_0\lib64-msvc-14.0.

那么问题就解决了。注意:其他相关问题中的一些答案可能建议构建为静态库,这样,您将收到另一个导入错误:DLL 加载失败:%1 不是有效的 Win32 应用程序。所以只需构建为 DLL。 PS:不需要在属性 Pages->Linker->Input-中指定boost_python-vc140-mt-1_64.lib或boost_python-vc140-mt-gd-1_64.lib >一些评论建议的附加依赖项。