0

I'm trying to follow the instructions here to use the Boost.Python. The source code is in that webpage. I can compile, link this simple sample code but I cannot import the resulting module in python command line. It always error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named hello_ext

I have no idea what the matter is because that page just says: "That's it. We're done. We can now build this as a shared library. The resulting DLL is now visible to Python." This is my building environment:

  • Windows 7 64 bit, I am the Administrator and run cmd as Administrator
  • boost version is 1.64.0 (precompiled binary boost_1_64_0-msvc-14.0-64.exe downloaded from here)
  • python version is 2.7.13, 64 bit
  • Visual studio 2015, Update 3
  • The target is a DLL
  • The project name is ConsoleApplication1, so the output is ConsoleApplication1.dll. I have changed the filename to hello_ext.dll but same error.
  • I built with x64 configuration and I have verified with dumpbin that the output ConsoleApplication1.dll is really 64 bit
  • I have added the path ......\ConsoleApplication1\x64\Release which contains ConsoleApplication1.dll into sys.path inside python command line.

So, could you please tell me how to import the module in python? Thanks a lot.

user5280911
  • 723
  • 1
  • 8
  • 21
  • Save yourself all of this trouble and get the [prebuilt binaries for Visual Studio](https://sourceforge.net/projects/boost/files/boost-binaries/1.64.0/) – PaulMcKenzie May 24 '17 at 15:22
  • error: "unresolved external symbol __imp_PyString_Type" in x64 config. Tons of "error LNK2001: unresolved external symbol "__declspec(dllimport) ..." errors in x32 config. – user5280911 May 24 '17 at 15:48
  • Those errors that start with `__imp_` indicate you are not specifying the appropriate import library ".lib" file in your linker settings. I see nothing in your description that states that you have told the linker what the names of the import library or libraries that will be used. Setting paths doesn't do this, and DLL's play no role in the build process -- they are used only at runtime. The prebuilt binaries include the import libraries (the .lib files that are not very large, as opposed to the static libraries which are megabytes in size). – PaulMcKenzie May 24 '17 at 16:18
  • There are only two .lib files related to Boost.Python in lib64-msvc-14.0 folder of the prebuild binaries: boost_python-vc140-mt-1_64.lib and boost_python-vc140-mt-gd-1_64.lib. I added each of them in Property Pages->Linker->Input->Additional Dependencies (this is the only place I can find to specify library file in VS 2015). But same error in x64 config (lib64-msvc-14.0 means target is 64 bit so should not be used with x86 config, right?) – user5280911 May 24 '17 at 16:48
  • The default download provided by python's official website is 32 bit (****, who is using 32 bit CPU today? I saw everyone use 64 bit OS five years ago). I re-installed 64 bit python 2.7 and there is no linking error now. But I still have no idea how to import the DLL in python command line. – user5280911 May 25 '17 at 05:22
  • Please show **all** relevant code. If you have compiled a hello world module, show the source. If you have a sys.path.append somewhere in the command line, show it. Every little detail matters. – n. m. could be an AI May 25 '17 at 05:47
  • The source code is in the webpage I linked in the question. I just copy them literally into VS2015 without any change. – user5280911 May 25 '17 at 07:43

1 Answers1

5

I solved the problem myself. Thank jagerman for his useful suggestions.

(1) Just change the output filename from ConsoleApplication1.dll to hello_ext.pyd. You can automate this rename by setting Pages->General->Target Extension to ".pyd". Make sure the file hello_ext.pyd is in python's search path. You can just throw it to C:\Python27\DLLs which is one of python's built-in search paths.

(2) Now you will got a different import error: DLL load failed: The specified module could not be found. If you look closely at the file size of hello_ext.pyd, you'll likely notice something wired -- it's only 19KB. That means it doesn't contain everything needed to import into python, so python has to find the missing part to properly import it. Yes, you may guess that -- the only possible missing stuff is Boost.Python libraries, so add path to it into PATH environment variable -- for me, it is C:\local\boost_1_64_0\lib64-msvc-14.0.

Then the problem is solved. Note: some answers in other related questions may suggest build as a static library, That way, you will got another import error: DLL load failed: %1 is not a valid Win32 application. So just build as DLL. PS: you don't need to specify boost_python-vc140-mt-1_64.lib or boost_python-vc140-mt-gd-1_64.lib in Property Pages->Linker->Input->Additional Dependencies as some comments suggested.

user5280911
  • 723
  • 1
  • 8
  • 21
  • should use `os.add_dll_directory` first when importing in python [link](https://github.com/boostorg/python/issues/311) – SodaCris Feb 15 '22 at 10:08