[C++-sig] Error importing a python module in c++

Tobias Opitz Opitz.student at mmi.rwth-aachen.de
Sat Nov 2 15:17:08 CET 2013


Hello mailinglist,

I'm having trouble embedding python into c++. I use msvc-9.0, building 32-bit on a 64-architecture running win7pro. My Python version is 2.7.5. I have a python release and debug version (every file in C.\python27\libs exists with and without _d)

I would like to embed a couple of python scripts into a c++-application. Therefore I first tried to use the standard Python C-API. The problem I encountered during this is the same as described  here<http://wangqiang8511.com/2012/07/18/embedding-python-in-c-application/>.
So I tried to use boost.python but this was neither successfully. I'm receiving a acces violation. Here is my example source code:

#include <Python.h> // without this line the compiler criticizes a missing python27.lib -
// but I'm trying to build a debug-variant...
#include <iostream>
#include <boost/python.hpp>

int main() {
    std::cout << "Boost.Python Test" << std::endl;

    // Start Python interpreter
    Py_Initialize();

    // Setup sys.path
    boost::python::object sys = boost::python::import("sys");
    // => throws "0x00000000 in Python_C_API_Test.exe: 0xC0000005: Access violation." in boost/libs/python/src/import.cpp
    boost::python::list path = boost::python::extract<boost::python::list>(sys.attr("path"));

    std::cout << "Module loaded" << std::endl;

    return 0;
}

I have build boost.python a dozend times with different flags. ("D:\boost_1_53_0>b2 --build-dir="D:\boost_1_53_0\python" toolset=msvc-9.0 link=shared variant=debug --python-debugging python" and using the resulting "boost_python-vc90-mt-gd-1_53.lib"

What could I have missed? Do you have any solutions or approachs for me? Thanks in advance.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20131102/a7c4d19d/attachment.html>


More information about the Cplusplus-sig mailing list