簡體   English   中英

Boost Python Hello World示例在Python中無法正常運行

[英]Boost Python Hello World example not working in Python, AGAIN

我和這里的人有同樣的問題

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

誰解決了他的問題,卻沒有說明如何解決。 那里的帖子已經很老了,在也發布之后,我現在在這里創建一個新問題,以使事情有所刷新。 所以...

我的配置: Windows 8 62位,python 2.7.3 32位,Visual Studio 2010 Ultimate

我跟着

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

我已經在Visual Studio中創建了一個稱為CLIB的win32application項目,並在“ dll”上打了勾,但沒有打“ export symbol”。

在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位從全新創建)。 因此,如果有人可以提供精確的幫助,那將是完美的。

多謝。

/////////////////////////// EDIT 1 /////////////////////// /////

在此期間,我看到python 2.7是使用mvs2008編譯的,因此擦除了所有內容,安裝了python 3.3,使用python 3.3重建了boost和boost.python,然后重試,但是...結果相同...

/////////////////////////// EDIT 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.

導入語句,擴展模塊名稱和擴展庫名稱之間似乎有所不同:

BOOST_PYTHON_MODULE宏定義模塊初始化函數。 CLIB.cpp更改

BOOST_PYTHON_MODULE(hello)

BOOST_PYTHON_MODULE(CLIB)

接下來,驗證結果輸出庫是否具有CLIB前綴。 Python3.3刪除了對一些很少使用的模塊名稱變體的支持。 根據文檔 ,我建議您先嘗試.pyd文件擴展名,如果.pyd無法正常工作,請訴諸.dll 因此,生成的庫應為CLIB.pyd

最后,當嘗試導入擴展名時

import CLIB

代替

import hello

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM