[C++-sig] [Boost.Python] tutorial python extension fails on import with ImportError: No module named hello_ext

TP wingusr at gmail.com
Wed Feb 10 10:49:03 CET 2010


I know this is a little late, but I just started using Boost.Python on
Windows XP, and saw the same exact problems as the original poster so
I thought I should give a solution since no one else has (and I've
seen the same question go unanswered elsewhere).

On 11/10/2009 10:36 PM, Charles Lentz wrote:

> When using boost_1_38_0\libs\python\example\tutorial, I try to import the
> extension that is created (hello_ext) but get the exception, ImportError: No
> module named hello_ext

> I modify Jamroot so that the last few lines where it creates tests are
> commented out.  When I run bjam again, hello_ext.pyd remains (previously it
> was being created, used for the test, and then deleted).  I try to run
> hello.py (which is the script that the test uses) from IDLE (which comes
> with the installation of python) and it throws an exception for my import
> statement

As others note elsewhere you can instead add "--preserve-test-targets"
to your bjam command line. Also adding "-d+2" lets you see more
verbose output of what bjam is doing behind the scenes.

>
> Traceback (most recent call last):
>
>  File
> "C:\Users\Charles\Desktop\boost_1_38_0\boost_1_38_0\libs\python\example\tuto
> rial\hello.py", line 6, in <module>

>    import hello_ext
>
> ImportError: No module named hello_ext
>
> Hrmm.  Okay.  So I move the hello.py file into the same directory as the
> hello_ext.pyd file.  Run it again using IDLE, still get the exception
>

After much googling and looking at the bjam output with "-d+2" turned
on, I discovered that the problem is not just with the location (and
existence) of hello_ext.pyd.

Not only does hello_ext.pyd have to be somewhere in your PATH (copying
it to the same dir as hello.py solves that problem), but the
Boost.Python dll ALSO has to be in the path. In my case I had to add
C:\boost_1_42_0\stage\lib to my PATH.

Of course, this is obvious after the fact. However, the error message
is confusing. What it really means in this case is:

  Can't find boost_python-vc90-mt-1_42.dll or
  boost_python-vc90-mt-gd-1_42.dll.

In addition the "Getting Started on Windows" page at
http://www.boost.org/doc/libs/1_42_0/more/getting_started/windows.html
NEVER mentions having to add C:\boost_1_42_0\stage\lib to the PATH if
you compile the library yourself.

I imagine this doesn't come up more often because most people
initially use the pre-built binaries from BoostPro Computing, and that
installer probably correctly sets the PATH.

I also had problems running bjam since I had copied the tutorial to my
F: drive. I was getting:

  Unable to load Boost.Build: could not find "boost-build.jam"
  ---------------------------------------------------------------
  Attempted search from F:\Boost\BoostPythonTutorial up to the root

Changing the tutorial's jamroot file to say:

  use-project boost
    : C:/boost_1_42_0 ;

didn't help.

I eventually had to copy C:\boost_1_42_0\boost-build.jam to F:\Boost
and comment everything out and add:

  boost-build C:/boost_1_42_0/tools/build/v2 ;

(This last part may not have normally been necessary but I got some
error message about not being able to find a dll when I first ran
bootstrap.bat. I solved that problem by downloading the prebuilt
version of bjam.exe)

Turns out all of this is explained here:

 Knowledge Base/CPP/Boost Python Step By Step
 http://www.thalesians.com/finance/index.php/Knowledge_Base/CPP/Boost_Python_Step_By_Step#Building_the_Boost.Python_Tutorial_in_Another_Location


More information about the Cplusplus-sig mailing list