2013-03-24 77 views
1

我有同樣的問題比這傢伙在這裏升壓Python的Hello World示例在Python不工作,請再次

Boost Python Hello World example not working in Python

誰解決了他的問題,沒有指示如何。那裏的帖子已經很老了,在發佈之後,我現在又創建了一個新的問題,讓它更新一些。所以...:

我的配置:Windows 8的62bits,蟒蛇2.7.3 32位,Visual Studio 2010的最終

我跟着

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

我創建了一個win32application項目在Visual Studio中稱爲CLIB,並勾選「dll」而不是「導出符號」。

在VC++目錄中的屬性,我把:

C:\ Python27x86 \包括:C:\提升\ boost_1_53_0; $(INCLUDEPATH)

爲包括目錄和

C:\升壓\ 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中。我甚至試圖把一個空白初始化的.py在同一目錄比DLL,而且,無論我做什麼,我不能夠在python腳本做

import hello 

(從新鮮要麼創建用記事本++或用32位編碼器)。所以如果有人能夠給予確切的幫助,那將是完美的。

Thx很多。

//////////////////////////編輯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. 
+0

請您模塊的本地目錄中運行代碼時。你得到了什麼確切的錯誤信息?只是「找不到模塊」或更具體的東西? – 2013-03-24 12:35:24

+0

試過你說的,同樣的錯誤:只是「導入錯誤:沒有名爲'你好'的模塊」。在此期間,我看到python 2.7是用mvs2008編譯的,所以刪除了所有內容,安裝了python 3.3,用python 3.3重建了boost和boost.python,然後重試,但是...結果相同...您怎麼看? – 2013-03-24 15:07:22

+0

正如[this](http://stackoverflow.com/a/15576882/1053968)中所述,請考慮使用['-vv']運行python(http://docs.python.org/3.1/using/cmdline .html#cmdoption-trace-v)參數。這應該提供洞察「import」嘗試的內容。 – 2013-03-25 17:01:28

回答

0

有似乎是進口之間的差異聲明,擴展模塊名稱和擴展庫名稱:

BOOST_PYTHON_MODULE宏定義模塊初始化函數。在CLIB.cpp變化

BOOST_PYTHON_MODULE(hello)

BOOST_PYTHON_MODULE(CLIB)

接下來,驗證結果輸出庫具有CLIB前綴。 Python3.3刪除了一些很少使用的模塊名稱變體的支持。根據documentation,我建議先嚐試一個.pyd文件擴展名,如果.pyd不起作用,則使用.dll。因此,得到的庫應該是CLIB.pyd

最後,試圖導入擴展用的

import CLIB 

代替

import hello 
+0

是的,你是對的,同時改變「你好」從 BOOST_PYTHON_MODULE(你好) { // ... } 到CLIB,而我在編制發佈模式,稱爲CLIB.pyd一個PYD文件,在一個目錄,我把我在其中也做我的python腳本一個「導入CLIB」,沒有任何成功。哼,我生氣了。 ;-)我不記得在堆棧中的哪個位置,我看到有人說,關於boost.python,如下所示:「呃,忘了它,它的代碼在藤上腐爛......」 – 2013-03-26 07:49:00

+0

@MisesEnForce:如果重命名文件到'CLIB.dll'不起作用,那麼你可能要考慮恢復到2.7.3,以便在使用'-vv'運行時導入更加有意義的冗餘。 – 2013-03-26 12:41:04

+0

嗯,我應該留在MVS2010嗎?我是否應該用mvsc-9.0(和python 2.7.3)重新編譯boost和boost.python?因爲在切換到3.3之前我做了所有這些,特別是因爲我想使用mvs2010,並且因爲我讀了2.7。不應該與mvs2010一起用於編譯boost.python?謝謝 – 2013-03-26 19:16:49