On Thu, 1 Jul 2004 16:12:13 -0700, Graeme Lufkin <graeme.lufkin@gmail.com> wrote:
Okay, so you're embedding in your extension. I don't think you need to call Py_Initialize(), as this should already have been done by the python executable that you start to import your module. I don't think it's wrong to call it multiple times, but should be unnecessary.
On Thu, 1 Jul 2004 22:48:24 +0000 (UTC), Faheem Mitha <faheem@email.unc.edu> wrote:
In [1]: import embed --------------------------------------------------------------------------- ImportError Traceback (most recent call last)
/usr/local/src/libboost-python1.31.0/boost-1.31.0/libs/ python/example/bin/example/embed.so/gcc/debug/shared-linkable-true/<console>
ImportError: libboost_python.so.1.31.0: cannot open shared object file: No such file or directory
Any idea how to fix this? Debian has /usr/lib/libboost_python.so and libboost_python-gcc-mt-1_31.so, where the former is a symbolic link to the latter.
This looks like libbost isn't in your LD_LIBRARY_PATH. When you make a Python extension with the 'extension' target in Boost.Build, it will build a copy of libboost and your extension (look in the bin directory). This one is the one that your extension gets linked against, and so is the one it's looking for when you import the module. Since you've got Debian's version of the compiled Boost libraries installed, it might be better to link against them.
Yes, I just realised that this is what was going on.
You can do that with a Jamfile entry similar to:
extension getting_started1-external : getting_started1.cpp <find-library>boost_python ;
Since you're doing embedding in the extension, you probably also need the embedding flags, so it would look like:
extension embed_in_extend_test : embed_in_extend.cpp <find-library>boost_python $(BOOST_PYTHON_V2_PROPERTIES) <find-library>$(PYTHON_EMBEDDED_LIBRARY) ;
where $(BOOST_PYTHON_V2_PROPERTIES) and $(PYTHON_EMBEDDED_LIBRARY) are variables that are defined by the Boost.Python library Jamfile.
The Jamfile entry for the test I ran (that compiled and ran fine) is: exe simpletest : simpletest.cpp : $(BOOST_PYTHON_V2_PROPERTIES) <find-library>$(PYTHON_EMBEDDED_LIBRARY) <find-library>boost_python-gcc <library-path>$(HOME)/Projects/boost_install/lib ; Note the different name for the shared library to link against, that's because I'm using the libraries built by running bjam in the root of my Boost installation. They got put in a different directory (which is also in my LD_LIBRARY_PATH), so I add that to the list of linker paths.
This is very useful information. Thanks. Ok. I am able to successfully build and run an executable, using a recipe similar to the one you provided. It creates a file called hello.txt in the directory. However, I still cannot get a Python extension module with the interpreter imbedded to work, however. I still get the same error as before. Details below. Again, I really appreciate your help. The extension module file follows. When I try to load this I get In [1]: import embed_ext In [2]: embed_ext.foo() --------------------------------------------------------------------------- NameError Traceback (most recent call last) /usr/local/src/boost/boost-1.31.0/libs/python/example/ bin/example/embed_ext.so/gcc/debug/shared-linkable-true/<console> /usr/local/src/boost/boost-1.31.0/libs/python/example/ bin/example/embed_ext.so/gcc/debug/shared-linkable-true/<string> NameError: name 'file' is not defined So there is still some problem. It is not clear from your message whether you actually built and tried to run the extension module with the embedded interpreter. If you have not, then I'd be much obliged if you try out the following code, and see if you can reproduce this problem. Any idea what I am doing wrong at this point? Thanks, Faheem. The extension module file: ******************************************************************** embed_ext.cc ******************************************************************** #include <boost/python.hpp> using namespace boost::python; void foo() { handle<> main_module(borrowed( PyImport_AddModule("__main__") )); dict main_namespace = extract<dict>(PyModule_GetDict(main_module.get())); handle<> unused(PyRun_String("hello = file('hello. txt', 'w')\n" "hello.write('Hello world!')\n" "hello.close()", Py_file_input, main_namespace.ptr(),main_namespace.ptr())); } BOOST_PYTHON_MODULE(embed_ext) { def("foo",foo); } ****************************************************************** The executable file: ****************************************************************** embed.cc ****************************************************************** #include <boost/python.hpp> using namespace boost::python; int main() { Py_Initialize(); handle<> main_module(borrowed( PyImport_AddModule("__main__") )); dict main_namespace = extract<dict>(PyModule_GetDict(main_module.get())); handle<> unused(PyRun_String("hello = file('hello. txt', 'w')\n" "hello.write('Hello world!')\n" "hello.close()", Py_file_input, main_namespace.ptr(),main_namespace.ptr())); Py_Finalize(); } ******************************************************************** The Jamfile: ******************************************************************** Jamfile ******************************************************************** # This is the top of our own project tree project-root ; # Include definitions needed for Python modules import python ; # Declare an executable called embed exe embed : embed.cc : $(BOOST_PYTHON_V2_PROPERTIES) <find-library>$(PYTHON_EMBEDDED_LIBRARY) <find-library>boost_python <find-library>python2.3 ; # Declare a Python extension called embed_ext extension embed_ext : embed_ext.cc : $(BOOST_PYTHON_V2_PROPERTIES) <find-library>$(PYTHON_EMBEDDED_LIBRARY) <find-library>boost_python <find-library>python2.3 ; ***********************************************************************