1

我和这里的人有同样的问题

Boost Python Hello World 示例在 Python 中不起作用

谁解决了他的问题,但没有说明如何解决。那里的帖子已经很老了,在也在那里发布之后,我现在在这里创建一个新问题,以使事情有所更新。所以... :

我的配置:windows 8 62bits,python 2.7.3 32 bits,visual studio 2010 Ultimate

我跟着

http://www.boost.org/doc/libs/1_53_0/libs/python/doc/tutorial/doc/html/python/exposing.html

我在 Visual Studio 中创建了一个名为 CLIB 的 win32application 项目,并勾选了“dll”而不是“导出符号”。

在属性中的 VC++ 目录中,我放了:

C:\Python27x86\include;C:\boost\boost_1_53_0;$(IncludePath)

对于包含目录和

C:\boost\boost_1_53_0\stage\lib;C:\Python27x86\libs;$(LibraryPath)

用于库目录。

在 CLIB.cpp 我把

// CLIB.cpp : Defines the exported functions for the DLL application.
//

#include "stdafx.h"
#include <iostream>
#include <string>
#include <boost/python.hpp>

using namespace boost::python;

struct World
{
    void set(std::string msg) { this->msg = msg; }
    std::string greet() { return msg; }
    std::string msg;
};

BOOST_PYTHON_MODULE(hello)
{
    boost::python::class_<World>("World")
        .def("greet", &World::greet)
        .def("set", &World::set)
        ;
}

我尝试添加或不添加

#define BOOST_PYTHON_STATIC_LIB

然后我在调试模式下编译(成功),输出.dll(或.pyd,我通过在项目的“配置属性”中的“通用”中将目标扩展名从.dll修改为.pyd都尝试过,但都没有成功) CLIB.dll 位于我也将其路径添加到 PYTHONPATH 的目录中。我什至尝试将一个空白的init .py 放在与 dll 相同的目录中,而且,无论我做什么,我都无法做到

import hello

在 python 脚本中(使用 notepad++ 或 pyscripter 32 位从新鲜创建)。因此,如果有人可以提供准确的帮助,那将是完美的。

多谢。

////////////////////// 编辑1 /////////////////// /////

同时我看到python 2.7是用mvs2008编译的,所以抹掉一切,安装python 3.3,用python 3.3重建boost和boost.python,然后重试,但是......结果一样......

////////////////////// 编辑2 /////////////////// /////

正如建议的那样,使用非常冗长的 -vv 命令运行 python 脚本,结果如​​下:

PS C:\Users\TheUser\Desktop\CLIB\Release> python -vv .\script.py
import _frozen_importlib # frozen
import imp # builtin
import sys # builtin
# installing zipimport hook
import 'zipimport' # <class '_frozen_importlib.BuiltinImporter'>
# installed zipimport hook
# C:\Python33x86\Lib\encodings\__pycache__\__init__.cpython-33.pyc matches C:\Python33x86\Lib\encodings\__init__.py
# code object from C:\Python33x86\Lib\encodings\__pycache__\__init__.cpython-33.pyc
# C:\Python33x86\Lib\__pycache__\codecs.cpython-33.pyc matches C:\Python33x86\Lib\codecs.py
# code object from C:\Python33x86\Lib\__pycache__\codecs.cpython-33.pyc
import '_codecs' # <class '_frozen_importlib.BuiltinImporter'>
import 'codecs' # <_frozen_importlib.SourceFileLoader object at 0x01E50870>
# C:\Python33x86\Lib\encodings\__pycache__\aliases.cpython-33.pyc matches C:\Python33x86\Lib\encodings\aliases.py
# code object from C:\Python33x86\Lib\encodings\__pycache__\aliases.cpython-33.pyc
import 'encodings.aliases' # <_frozen_importlib.SourceFileLoader object at 0x01E91B90>
import 'encodings' # <_frozen_importlib.SourceFileLoader object at 0x01E503F0>
# C:\Python33x86\Lib\encodings\__pycache__\mbcs.cpython-33.pyc matches C:\Python33x86\Lib\encodings\mbcs.py
# code object from C:\Python33x86\Lib\encodings\__pycache__\mbcs.cpython-33.pyc
import 'encodings.mbcs' # <_frozen_importlib.SourceFileLoader object at 0x01E9E730>
# C:\Python33x86\Lib\encodings\__pycache__\utf_8.cpython-33.pyc matches C:\Python33x86\Lib\encodings\utf_8.py
# code object from C:\Python33x86\Lib\encodings\__pycache__\utf_8.cpython-33.pyc
import 'encodings.utf_8' # <_frozen_importlib.SourceFileLoader object at 0x01E9E8D0>
# C:\Python33x86\Lib\encodings\__pycache__\latin_1.cpython-33.pyc matches C:\Python33x86\Lib\encodings\latin_1.py
# code object from C:\Python33x86\Lib\encodings\__pycache__\latin_1.cpython-33.pyc
import 'encodings.latin_1' # <_frozen_importlib.SourceFileLoader object at 0x01E9E9B0>
# C:\Python33x86\Lib\__pycache__\io.cpython-33.pyc matches C:\Python33x86\Lib\io.py
# code object from C:\Python33x86\Lib\__pycache__\io.cpython-33.pyc
# C:\Python33x86\Lib\__pycache__\abc.cpython-33.pyc matches C:\Python33x86\Lib\abc.py
# code object from C:\Python33x86\Lib\__pycache__\abc.cpython-33.pyc
# C:\Python33x86\Lib\__pycache__\_weakrefset.cpython-33.pyc matches C:\Python33x86\Lib\_weakrefset.py
# code object from C:\Python33x86\Lib\__pycache__\_weakrefset.cpython-33.pyc
import '_weakrefset' # <_frozen_importlib.SourceFileLoader object at 0x01EA7150>
import 'abc' # <_frozen_importlib.SourceFileLoader object at 0x01E9EC30>
import 'io' # <_frozen_importlib.SourceFileLoader object at 0x01E9EB10>
# C:\Python33x86\Lib\encodings\__pycache__\cp850.cpython-33.pyc matches C:\Python33x86\Lib\encodings\cp850.py
# code object from C:\Python33x86\Lib\encodings\__pycache__\cp850.cpython-33.pyc
import 'encodings.cp850' # <_frozen_importlib.SourceFileLoader object at 0x01EA7F50>
# C:\Python33x86\Lib\__pycache__\site.cpython-33.pyc matches C:\Python33x86\Lib\site.py
# code object from C:\Python33x86\Lib\__pycache__\site.cpython-33.pyc
# C:\Python33x86\Lib\__pycache__\os.cpython-33.pyc matches C:\Python33x86\Lib\os.py
# code object from C:\Python33x86\Lib\__pycache__\os.cpython-33.pyc
import 'errno' # <class '_frozen_importlib.BuiltinImporter'>
# C:\Python33x86\Lib\__pycache__\stat.cpython-33.pyc matches C:\Python33x86\Lib\stat.py
# code object from C:\Python33x86\Lib\__pycache__\stat.cpython-33.pyc
import 'stat' # <_frozen_importlib.SourceFileLoader object at 0x01EBD190>
# C:\Python33x86\Lib\__pycache__\ntpath.cpython-33.pyc matches C:\Python33x86\Lib\ntpath.py
# code object from C:\Python33x86\Lib\__pycache__\ntpath.cpython-33.pyc
# C:\Python33x86\Lib\__pycache__\genericpath.cpython-33.pyc matches C:\Python33x86\Lib\genericpath.py
# code object from C:\Python33x86\Lib\__pycache__\genericpath.cpython-33.pyc
import 'genericpath' # <_frozen_importlib.SourceFileLoader object at 0x01EBDE50>
import 'ntpath' # <_frozen_importlib.SourceFileLoader object at 0x01EBDAD0>
# C:\Python33x86\Lib\collections\__pycache__\__init__.cpython-33.pyc matches C:\Python33x86\Lib\collections\__init__.py
# code object from C:\Python33x86\Lib\collections\__pycache__\__init__.cpython-33.pyc
# C:\Python33x86\Lib\collections\__pycache__\abc.cpython-33.pyc matches C:\Python33x86\Lib\collections\abc.py
# code object from C:\Python33x86\Lib\collections\__pycache__\abc.cpython-33.pyc
import 'collections.abc' # <_frozen_importlib.SourceFileLoader object at 0x027C3FB0>
import '_collections' # <class '_frozen_importlib.BuiltinImporter'>
import 'operator' # <class '_frozen_importlib.BuiltinImporter'>
# C:\Python33x86\Lib\__pycache__\keyword.cpython-33.pyc matches C:\Python33x86\Lib\keyword.py
# code object from C:\Python33x86\Lib\__pycache__\keyword.cpython-33.pyc
import 'keyword' # <_frozen_importlib.SourceFileLoader object at 0x027E1330>
# C:\Python33x86\Lib\__pycache__\heapq.cpython-33.pyc matches C:\Python33x86\Lib\heapq.py
# code object from C:\Python33x86\Lib\__pycache__\heapq.cpython-33.pyc
import 'itertools' # <class '_frozen_importlib.BuiltinImporter'>
# C:\Python33x86\Lib\__pycache__\bisect.cpython-33.pyc matches C:\Python33x86\Lib\bisect.py
# code object from C:\Python33x86\Lib\__pycache__\bisect.cpython-33.pyc
import '_bisect' # <class '_frozen_importlib.BuiltinImporter'>
import 'bisect' # <_frozen_importlib.SourceFileLoader object at 0x027E1EF0>
import '_heapq' # <class '_frozen_importlib.BuiltinImporter'>
import 'heapq' # <_frozen_importlib.SourceFileLoader object at 0x027E1910>
# C:\Python33x86\Lib\__pycache__\weakref.cpython-33.pyc matches C:\Python33x86\Lib\weakref.py
# code object from C:\Python33x86\Lib\__pycache__\weakref.cpython-33.pyc
import 'weakref' # <_frozen_importlib.SourceFileLoader object at 0x027E1950>
# C:\Python33x86\Lib\__pycache__\reprlib.cpython-33.pyc matches C:\Python33x86\Lib\reprlib.py
# code object from C:\Python33x86\Lib\__pycache__\reprlib.cpython-33.pyc
import 'reprlib' # <_frozen_importlib.SourceFileLoader object at 0x027E9CF0>
import 'collections' # <_frozen_importlib.SourceFileLoader object at 0x01EC0CD0>
# C:\Python33x86\Lib\__pycache__\copyreg.cpython-33.pyc matches C:\Python33x86\Lib\copyreg.py
# code object from C:\Python33x86\Lib\__pycache__\copyreg.cpython-33.pyc
import 'copyreg' # <_frozen_importlib.SourceFileLoader object at 0x027EFD10>
import 'os' # <_frozen_importlib.SourceFileLoader object at 0x01EB3130>
# C:\Python33x86\Lib\__pycache__\re.cpython-33.pyc matches C:\Python33x86\Lib\re.py
# code object from C:\Python33x86\Lib\__pycache__\re.cpython-33.pyc
# C:\Python33x86\Lib\__pycache__\sre_compile.cpython-33.pyc matches C:\Python33x86\Lib\sre_compile.py
# code object from C:\Python33x86\Lib\__pycache__\sre_compile.cpython-33.pyc
import '_sre' # <class '_frozen_importlib.BuiltinImporter'>
# C:\Python33x86\Lib\__pycache__\sre_parse.cpython-33.pyc matches C:\Python33x86\Lib\sre_parse.py
# code object from C:\Python33x86\Lib\__pycache__\sre_parse.cpython-33.pyc
# C:\Python33x86\Lib\__pycache__\sre_constants.cpython-33.pyc matches C:\Python33x86\Lib\sre_constants.py
# code object from C:\Python33x86\Lib\__pycache__\sre_constants.cpython-33.pyc
import 'sre_constants' # <_frozen_importlib.SourceFileLoader object at 0x028054F0>
import 'sre_parse' # <_frozen_importlib.SourceFileLoader object at 0x027FD530>
import 'sre_compile' # <_frozen_importlib.SourceFileLoader object at 0x01EB3690>
# C:\Python33x86\Lib\__pycache__\functools.cpython-33.pyc matches C:\Python33x86\Lib\functools.py
# code object from C:\Python33x86\Lib\__pycache__\functools.cpython-33.pyc
import '_functools' # <class '_frozen_importlib.BuiltinImporter'>
import 'functools' # <_frozen_importlib.SourceFileLoader object at 0x027FD8D0>
import 're' # <_frozen_importlib.SourceFileLoader object at 0x01EBD5D0>
# C:\Python33x86\Lib\__pycache__\sysconfig.cpython-33.pyc matches C:\Python33x86\Lib\sysconfig.py
# code object from C:\Python33x86\Lib\__pycache__\sysconfig.cpython-33.pyc
import 'sysconfig' # <_frozen_importlib.SourceFileLoader object at 0x0280D9F0>
# C:\Python33x86\Lib\__pycache__\locale.cpython-33.pyc matches C:\Python33x86\Lib\locale.py
# code object from C:\Python33x86\Lib\__pycache__\locale.cpython-33.pyc
import '_locale' # <class '_frozen_importlib.BuiltinImporter'>
import 'locale' # <_frozen_importlib.SourceFileLoader object at 0x0281B810>
# C:\Python33x86\Lib\encodings\__pycache__\cp1252.cpython-33.pyc matches C:\Python33x86\Lib\encodings\cp1252.py
# code object from C:\Python33x86\Lib\encodings\__pycache__\cp1252.cpython-33.pyc
import 'encodings.cp1252' # <_frozen_importlib.SourceFileLoader object at 0x02825350>
import 'site' # <_frozen_importlib.SourceFileLoader object at 0x01EA4290>
Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:55:48) [MSC v.1600 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
import 'math' # <class '_frozen_importlib.BuiltinImporter'>
Traceback (most recent call last):
  File ".\script.py", line 2, in <module>
    import CLIB
  File "<frozen importlib._bootstrap>", line 1558, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1525, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 586, in _check_name_wrapper
  File "<frozen importlib._bootstrap>", line 497, in set_package_wrapper
  File "<frozen importlib._bootstrap>", line 510, in set_loader_wrapper
  File "<frozen importlib._bootstrap>", line 1130, in load_module
  File "<frozen importlib._bootstrap>", line 313, in _call_with_frames_removed
ImportError: DLL load failed: The specified module could not be found.
4

1 回答 1

0

导入语句、扩展模块名称和扩展库名称之间似乎存在差异:

BOOST_PYTHON_MODULE宏定义了模块初始化函数。CLIB.cpp变化中

BOOST_PYTHON_MODULE(你好

BOOST_PYTHON_MODULE( CLIB )

接下来,验证生成的输出库是否具有 CLIB 前缀。Python3.3 删除了对一些很少使用的模块名称变体的支持。根据文档,我建议先尝试.pyd文件扩展名,.dll如果.pyd不起作用则求助。因此,生成的库应该是CLIB.pyd.

最后,在尝试导入扩展时使用

import CLIB

代替

import hello
于 2013-03-25T23:43:18.110 回答