If you are a scientist and interested in Python, you certainly already know the NumPy package.
In this tutorial I'll propose to explain how to install it on Windows in order to be used with the Boost.Python library.
We'll also make an Hello world example.
We need to install Python 3 and Boost on your computer.
So in order to have the exact same software and libraries installed in the exact same locations, I suggest to follow the 2 following tutorials:
Numpy was previously a library from Boost.
But with some recent changes it's not the case anymore.
Indeed NumPy has become a Boost.Python extension.
But to use it you'll have to build it as a library.
Yes it's clearly confuse, don't worry.
And for going further if you want to use it from the Boost binaries you won't be able to.
The binaries don't come with NumPy already installed.
It means that if you try to include the Numpy extension headers directly on your code you'll encounter the following error:
So how does it work then?
The answer is that you MUST download and install the Numpy package on your computer before using it.
Indeed this package isn't directly available from Python nor from the built Boost libraries that you have installed.
It's a Python package so we have to use the Package Installer for Python (pip).
Thus to get the NumPy package, just open a console from the directory of your choice then type the following command:
pip install numpy
You'll have this message and the NumPy package will start to download and install on your computer:
PS D:\dev\c++\boost\BadprogTutorial> pip install numpy Collecting numpy Downloading https://files.pythonhosted.org/packages/34/40/c6eae19892551ff91bdb15a884fef2d42d6f58da55ab18fa540851b48a32/numpy-1.17.4-cp37-cp37m-win_amd64.whl (12.7MB) 12.7MB Installing collected packages: numpy Successfully installed numpy-1.17.4
Great, NumPy is now installed on your computer.
Let's continue by building the NumPy extension from Boost.Python in order to get a NumPy library.
For that we are going to use the b2 tool (already installed from the previous Boost tutorial above).
Let's go in the following directory:
In this directory there is only one file:
We don't have to modify it, we just have to use it.
So open a console from this directory and type:
b2 address-model=64 release debug
If it doen't work try with:
..\..\..\b2.exe address-model=64 release debug
The b2 is building your libraries and you can see this at the beginning of the message:
PS C:\soft\boost_1_71_0\libs\python\build> ..\..\..\b2.exe address-model=64 release debug Performing configuration checks - default address-model : 32-bit - default architecture : x86 ...patience... ...patience... ...found 3362 targets... ...updating 97 targets...
Don't worry about the 32-bit default address-model and the x86 default architecture, it's a bug.
Indeed your x64 libraries have been successfully built.
To check that let's go at these locations to find the debug and release DLLs and LIBs:
From these 2 directories, copy and paste the following NumPy DLLs and LIBs:
To the following directory:
So you have now this:
Here we go we are ready to use the NumPy Boost.Python extension.
From Visual Studio > File > New > Project... > Installed > Visual C++ > Windows Desktop > Windows Console Application.
Then:
So you have now the following directory:
Be sure to set the include and library directories as suggested in the above links (tutorials for Boost and Python).
Then type the following code:
If you don't have the DLLs copied in the Boost directory, you'll have this error:
Otherwise everything will go smoothly and you'll see:
Hello from Badprog and NumPy :D
One more step towards the understanding of Boost and Python.
NumPy is now ready to be used.
Good job you did it.
Comments
Andrii Dehtiatiov (not verified)
Monday, May 25, 2020 - 11:42am
Permalink
Hello!
Hello!
Thanks for the great article, this one helped alot! I would like to suggest a little change: you mentioned the numpy extension build command "b2 address-model=64 release debug". I think you should add "threading=multi" option. So the command will be as follows: "b2 address-model=64 threading=multi release debug". This option will build lib with "mt" tag.
Add new comment