简体   繁体   中英

Boost Python Hello World example not working in Python, AGAIN

I have the same problem than the guy here

Boost Python Hello World example not working in Python

who solved his problem, without indicating how. The posts there being quite old, after having posted there also, I create now a new question here, for refreshing things a bit. So... :

My configuration : windows 8 62bits , python 2.7.3 32 bits, visual studio 2010 ultimate

I followed

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

I have created a win32application project called CLIB in visual studio, and ticked "dll" but not "export symbols".

In VC++ directories in properties I put :

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

for include directories and

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

for library directories.

In CLIB.cpp I put

// 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)
        ;
}

I tried with or without adding

#define BOOST_PYTHON_STATIC_LIB

Then I compiled in debug mode (with success), the output .dll (or .pyd, I have tried both without any success by modifying the target extension from .dll to .pyd in "general" in "configuration properties" of the project) CLIB.dll is in a directory whose path I added to the PYTHONPATH also. I have even tried to put a blank init .py in the same directory than the dll, and, whatever I do, I am not able to do a

import hello

in the python script (created from fresh either with notepad++ or with pyscripter 32 bits). So if someone could give precise help, that would be perfect.

Thx a lot.

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

In the meantime I saw python 2.7 was compiled with mvs2008, so erase everything, installed python 3.3, rebuilt boost and boost.python with python 3.3, and retried, but... same result...

////////////////////////// EDIT 2 ///////////////////////////

As suggested a run the python script with the very verbose -vv command, here is the result :

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.

There appears to be a difference between the the import statement(s), the extension module name, and the extension library name:

The BOOST_PYTHON_MODULE macro defines the module initializer function. In CLIB.cpp change

BOOST_PYTHON_MODULE()

to

BOOST_PYTHON_MODULE()

Next, verify that the resulting output library has a CLIB prefix. Python3.3 removed support for some of the rarely used module name variants. Per the documentation , I would suggest trying a .pyd file extension first, and resorting to .dll if the .pyd does not work. Thus, the resulting library should be CLIB.pyd .

Finally, when trying to import the extension use

import CLIB

instead of

import hello

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM