[C++-sig] boost/python tutorial

Scott VanSickle Scott.VanSickle at CanfieldSci.com
Tue Nov 20 19:13:10 CET 2007


Son,
We don't use the bjam command line method that you are using to build the module; we build a different way as follows.

We have a boostbuild.cmd that looks like this:

=======================
set BOOST_BUILD_PATH=%BOOST_ROOT%\tools\build\v2
set PATH=%PATH%;%BOOST_ROOT%;%PYTHON_ROOT%;**your optional additional paths go here**
set LIB=%LIB%;**additional optional LIB paths go here**;%BOOST_ROOT%\stage\lib
set BUILD="%1"
bjam toolset=msvc %BUILD%
=======================

We launch boostbuild.cmd with a command line parm of either debug or release

We also have a boost-build.jam file in the folder with boostbuild.cmd that looks like this (I don't think that this is being used):
=======================
# Copyright David Abrahams 2006. Distributed under the Boost
# Software License, Version 1.0. (See accompanying
# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

# Edit this path to point at the tools/build/v2 subdirectory of your
# Boost installation.  Absolute paths work, too.
boost-build $BOOST_ROOT/tools/build/v2 ;

=======================
Note that we use a BOOST_ROOT environment variable on all of our systems to point to where boost is.

In the folder with the CPP is a jamroot file that looks like this:
=======================
# Copyright David Abrahams 2006. Distributed under the Boost
# Software License, Version 1.0. (See accompanying
# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

# Specify the path to the Boost project.  If you move this project,
# adjust the path to refer to the Boost root directory.
use-project boost
  : $(BOOST_ROOT) ;

# Set up the project-wide requirements that everything uses the
# boost_python library defined in the project whose global ID is
# /boost/python.
project
  : requirements <library>/boost/python//boost_python ;

# Declare a Python extension
python-extension _**name of ext goes here** # Declare a Python extension
  : **name of CPP with wrapper class(es) goes here**    # Source
  : <include>**your includes go here**
    <define>**your defines go here**
  : <library-path>**your lib path goes here**<library-path>$(BOOST_ROOT)/stage/lib
  ;

=======================

Assuming that the wrapper CPP compiles (you'll get done quicker if you make sure that the CPP compiles in your C++ dev environment first), running "boostbuild debug" generates a bunch of files with a base name that matches the Python module name in jamroot, including the _name.lib and _name.pyd files.  We copy the lib and pyd to the folder where the CPP is, then everything works as long as you have the __init__.py that I previously described in that folder.  If you copy the pdb file also (or if it can be found in your path), you will be able to debug the pyd module in Visual Studio once Python loads it.  FYI - we use a Python wrapped class that dumps messages by calling the Windows OutputDebugString function and make use of the sysinternals DebugView tool to see the messages.  This can help you to easily see what's going on in your Python code.  There are probably other tools out there to do something like this that you could find; if you are using something like Komodo to debug your code you might not need anything else.

I hope that this helps.

Scott VanSickle


From: c++-sig-bounces at python.org [mailto:c++-sig-bounces at python.org] On Behalf Of Nguyen Xuan Son
Sent: Tuesday, November 20, 2007 12:14 PM
To: Development of Python/C++ integration
Subject: Re: [C++-sig] boost/python tutorial

Hi,
after the run there are two folders created

A/       \boost\libs\python\example\tutorial\bin\msvc-8.0\debug\threading-multi
I find:
20/11/2007  16:59    <DIR>          .
20/11/2007  16:59    <DIR>          ..
20/11/2007  13:26           406,907 hello.obj
20/11/2007  13:26               178 hello.obj.rsp
20/11/2007  13:28               535 hello_ext.exp
20/11/2007  13:28             1,752 hello_ext.lib
20/11/2007  13:28           543,744 hello_ext.pdb
20/11/2007  13:28               385 hello_ext.pyd.manifest
20/11/2007  13:28               173 hello_ext.pyd.rsp
20/11/2007  16:59                 0 list.txt
               8 File(s)        953,674 bytes
               2 Dir(s)   2,935,967,744 bytes free
B/       \boost\libs\python\example\tutorial\bin\hello.test\msvc-8.0\debug\threading-multi
I find:
20/11/2007  17:00    <DIR>          .
20/11/2007  17:00    <DIR>          ..
20/11/2007  13:28                34 hello
20/11/2007  13:28                34 hello.output
20/11/2007  13:28                 9 hello.test
20/11/2007  17:00                 0 list.txt
               4 File(s)             77 bytes
               2 Dir(s)   2,935,959,552 bytes free

There is no lib file there.

I copy files in A/ to C:\Python24\Lib\site-packages\hello_ext and try to launch the file hello.py localy (which i had copied to this folder as well).

So I am still stuck here. Please help me out:(
SOn

On Nov 20, 2007 2:55 PM, Scott VanSickle <Scott.VanSickle at canfieldsci.com> wrote:
You need to copy the generated pyd and lib files into a folder that corresponds to the name of your Python module under your Python root folder.  Note that the pyd is the DLL, the lib is the DLL's import library.  If you don't use namespaces, I think that the pyd and lib should be copied directly into your Python root folder.

Create an __init__.py file in the module folder that looks something like this

#============
import sys

sys.path.append('..\..')

# this brings in the DLL (pyd)
from _hello_ext import *

#============

You should then be able to import your hello_ext module in any of your Python code.

Scott V


From: c++-sig-bounces at python.org [mailto: c++-sig-bounces at python.org] On Behalf Of Nguyen Xuan Son
Sent: Tuesday, November 20, 2007 6:06 AM
To: c++-sig at python.org
Subject: [C++-sig] boost/python tutorial

Hi
I run the command line to build the \boost\libs\python\example\tutorial

>bjam -sPYTHON_ROOT=C:/Python24 -sPYTHON_VERSION=2.4  -sTOOLS=vc8

I see that everything run correctly and I check the folder
\boost\libs\python\example\tutorial\bin\hello.test\msvc-8.0\debug\threading-multi

and the output told me everything is OK. But I find no where the DLL, and I could not load the extension from the python command line:
>>> import hello_ext

Traceback (most recent call last):
 File "<pyshell#0>", line 1, in -toplevel-
   import hello_ext
ImportError: No module named hello_ext

Can someone help me out of this? I really confused.

Son
_______________________________________________
C++-sig mailing list
C++-sig at python.org
http://mail.python.org/mailman/listinfo/c++-sig




More information about the Cplusplus-sig mailing list