Communication between 2 different language isn't so easy.
It's often possible to find libraries to help us achieve this behaviour.
That's what we are going to see in this Boost.Python tutorial for Windows.
Let's get started.
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:
For Visual Studio 2017 and MSVC 141, install Python 3.7 and Boost 1.71.0 for MSVC 141.
For Visual Studio 2019 and MSVC 142, install Python 3.9 and Boost 1.77.0 for MSVC 142.
Let's now create a new project:
From Visual Studio > File > New > Project... > Installed > Visual C++ > Windows Desktop > Dynamic-Link Library (DLL).
Then:
Click OK.
So you have now the following directory:
With classic files generated by Visual Studio like:
First thing is now to change the project platform in the Configuration manager:
From Visual Studio > Select (right click mouse) the badprog_lib project > Properties... > On upper right click the Configuration Mananger... push button > Change Active solution platform from x86 to x64 > Close.
Then still in the badprog_lib Property Pages, at top center, change the Platform from Win32 to x64 (if not automatically updated).
So you have now:
Then click OK.
You can stay in Debug mode or change it to Release (it won't change anything for our tutorial).
First let's set the includes.
From Visual Studio > Select the badprog_lib.cpp file.
Then right click mouse > Properties... > Configuration Properties > C/C++ > General > Additioal Include Directories > Edit > Add the 2 following lines:
For Visual Studio 2017 (and MSVC 141) you need to install Python 3.7:
For Visual Studio 2019 (and MSVC 142) you need to install Python 3.9:
We've now to set the libraries.
Same thing but with the linker:
From Visual Studio > Right click the badprog_lib project > Properties > Configuration Properties > Linker > General > Additioal Library Directories > Edit > Add the 5 following lines:
For MSVC 141 (2017)
Then OK > Apply > OK.
It's now time to code our Dinamic-Link Library (DLL) from Visual Studio.
In our badprog_lib.cpp file let's add the following code:
Then from Visual Studio > Build > Build badprog_lib.
After the build, you should have these lines in your Visual Studio Build Output:
1>------ Build started: Project: badprog_lib, Configuration: Debug x64 ------ 1>dllmain.cpp 1> Creating library C:\dev\c++\py\badprog_lib\x64\Debug\badprog_lib.lib and object C:\dev\c++\py\badprog_lib\x64\Debug\badprog_lib.exp 1>badprog_lib.vcxproj -> C:\dev\c++\py\badprog_lib\x64\Debug\badprog_lib.dll ========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
If yes you can check that the following DLL has been created:
This DLL will serve as API for our Python script.
Now take this DLL and move it to the following directory:
So we've this file like this:
Rename the extension from .dll to .pyd.
You have now the following file:
This extension does really matter because without it the Python script won't be able to import it.
We've now 2 options in order to get the code from the badprog_lib.pyd library.
From the C:\dev\c++\py\badprog_lib\ directory, open a console.
Then type:
py
Your console will become a Python interpreter.
And you should see something like this appears:
PS C:\dev\c++\py\badprog_lib> py Python 3.7.5 (tags/v3.7.5:5c02a39a0b, Oct 15 2019, 00:11:34) [MSC v.1916 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information.
If yes, good news, you are in the Python console.
Let's now import our library and type the following command:
import badprog_lib
If nothing has happened, then that's great.
Let's continue by calling our C++ code:
badprog_lib.welcome()
The welcoming message should appears:
'Hello world from Badprog :D'
So to sum up here what you should have typed and seen:
PS C:\dev\c++\py\badprog_lib> py Python 3.7.5 (tags/v3.7.5:5c02a39a0b, Oct 15 2019, 00:11:34) [MSC v.1916 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import badprog_lib >>> badprog_lib.welcome() 'Hello world from Badprog :D' >>>
Let's create a file named badprog_script.py in the same directory, so we've the following:
In this file we have to type the following code, that is exactly the same as the previous command in the Python interpreter:
And now from this directory, open any console (Git bash, PowerShell, Cmd, etc.) and type:
py badprog_script.py
The output result will be:
Hello world from Badprog :D
A lot of things to check and set.
But you are now able to build a shared library directly from Visual Studio, call the Boost.Python library from a C++ code and use it with Python.
Good job, you did it.
Comments
boostNoob (not verified)
Sunday, April 26, 2020 - 11:07am
Permalink
Hi.
Hi.
Thanks for the helpful tips on your website. I tried to follow these steps using MSVS 2019, boost 1.70, Python 3.7. When I convert the dll to pyd and try to import it in the interpreter it returns "ImportError: DLL load failed: The specified module could not be found." When I leave the file as a dll it imports fine but when I try to call the function it responds with "AttributeError: module 'moduleTest' has no attribute 'greet'". Please note that maindll.cpp has a number of other lines inserted by default. I tried with and without removing those lines but same results.
Mi-K
Sunday, April 26, 2020 - 6:08pm
Permalink
Hello,
Hello,
Yes it's a classic error.
I guess you shouldn't have the same name in your code that the one in the tutorial.
You MUST import the same module name as the one in your code.
Just have a look in your badprog_lib.cpp file, I think the following line:
may not be the same as the one you used.
Anyway, I suggest to follow the tutorials (the 2 tutorials for installing Boost and Python) and this one with the exact same name for directories and files.
And once you get exactly how it works, you can change it your way.
boostNoob (not verified)
Monday, April 27, 2020 - 12:21pm
Permalink
Thanks for the reply. I'm
Thanks for the reply. I'm sure I followed the steps correctly. I created another file just incase but I got the same results. I guess I'll have to install the exact same releases of python and boost.python and try again sometime later. Ill post again with the results.
boostNoob (not verified)
Tuesday, April 28, 2020 - 9:15am
Permalink
Hi. It finally worked. I used
Hi. It finally worked. I used VS2019, Boost 1_71, Python 3.7 on Windows 10. Maybe that was all that was needed to be done but I used a bit of a different approach this time in that I chose to create an empty project. Then, before adding a cpp file I went into the properties by right clicking on the solution on solution viewer>Configuration Properties>Platform Toolset and changing it to Visual Studio 2017 (v141). On the same tab I changed Target Extension and Configuration Type to .dll(could possibly use .pyd here but didn't risk it) and Dynamic Library(.dll), respectively. Then, I added a new item .cpp file and followed the code and instructions except that I didnt include "stdafx.h".
Im curious if this approach would work with the latest versions of Python, Boost.Python, and msvc toolset v142 as choosing the template creation option didnt work and was full of extra code.
boostNoob2 (not verified)
Friday, June 26, 2020 - 1:25am
Permalink
Dude your comment helped me
Dude your comment helped me so much I was stuck creating an exe, as for msvc 142 I managed to get it to work by building boost with msvc 142
Mi-K
Tuesday, April 28, 2020 - 9:07pm
Permalink
Hello,
Hello,
Very nice!
Happy that you succeeded.
Interesting question about Visual Studio 2019 (msvc-142).
Maybe in another tutorial.
boostNoob (not verified)
Wednesday, April 29, 2020 - 7:05am
Permalink
Thanks for the help. Keep up
Thanks for the help. Keep up the good work. Looking forward to more tutorials.
Mi-K
Saturday, May 2, 2020 - 11:11pm
Permalink
(No subject)
Ravi (not verified)
Friday, November 20, 2020 - 6:08am
Permalink
ImportError: DLL load failed:
ImportError: DLL load failed: The specified module could not be found, I am getting, can you please help me out in resolving, I am using windows 64bit, python3.7.4
Angelina Maria ... (not verified)
Wednesday, March 3, 2021 - 4:23pm
Permalink
when I use this instruction
when I use this instruction it works very well for me.
PS C:\dev\c++\py\badprog_lib> py
Python 3.7.5 (tags/v3.7.5:5c02a39a0b, Oct 15 2019, 00:11:34) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import badprog_lib
>>> badprog_lib.welcome()
'Hello world from Badprog :D'
>>>
--------------------------------------------------------
but when I create the .py file and place the following lines I get an error message (Traceback (most recent call last):
File "C: xxxxxxx.py", line 2, in <module>
import HolaMundo_d
ImportError: DLL load failed while importing badprog_lib: The specified module could not be found.)
import badprog_lib
print(badprog_lib.welcome())
Manu (not verified)
Tuesday, March 30, 2021 - 7:23pm
Permalink
This example is pretty useful
This example is pretty useful to understand Boost-python library. Can you please provide another example that passes a numpy array from python to c++ (defined as a dll/pyd file)? When I pass a numpy array I always get a message the specified module could not be found!
Johan Lundqvist (not verified)
Saturday, November 27, 2021 - 11:41am
Permalink
Great instructions that have
Great instructions that have worked in the past for me. But now I just cant work it out! I get "ImportError: DLL load failed while importing badprog_lib". I *am* importing the correct module name.
Actually followed the first comment from boostNoob to the point. But it still fails with that error.
Any updates on why this could be the case. Thanks!
Johan Lundqvist (not verified)
Saturday, November 27, 2021 - 12:14pm
Permalink
Found the solution myself.
Found the solution myself. Add
#define BOOST_PYTHON_STATIC_LIB
as the first row in badprog_lib.cpp
Mi-K
Sunday, November 28, 2021 - 4:43pm
Permalink
Indeed Johan,
Indeed Johan,
Thank you, it seems to work like that only with MSVC 142 (2019).
I updated the tutorial
Add new comment