cmake_example icon indicating copy to clipboard operation
cmake_example copied to clipboard

Copy DLL to Python site packages

Open technikamateur opened this issue 1 year ago • 8 comments

Hi everybody, I created a project based on this example. What my project basicaly does, is fetching data from an API. The API consists of a .lib, .h and .DLL file. I included the .h file in my CPP code and added the .lib via target_link_libraries in CMakeLists.txt.

After running pip install . the .pyd file gets copied to site packages, but after an import in python it fails because of the missing .DLL

I solved the problem by copying the DLL next to the pyd under site packages, but would like to automate this process. What would be the best way, to accomplish this?

I already tried adding package_data to setup.py without success.

technikamateur avatar May 24 '24 16:05 technikamateur

I'm not super familiar with this system and learning it myself, but is this something that MANIFEST.in would do?

Xevion avatar May 29 '24 18:05 Xevion

I would highly recommend using pybind/scikit_build_example. This is the old way to do things, and is much harder to use and prone to breaking with every setuptools release. There you should be able to install the .DLL (install command in CMake).

henryiii avatar May 29 '24 18:05 henryiii

Hello @technikamateur

I have a similar problem. I am using VSCODE with Python 3.10.14 to generate .pyd files.

The file "example.cp310-win_amd64.pyd" is generated. But I get the error "ImportError: DLL load failed while importing example: The specified procedure could not be found." during python import.

Which .dll did you place near the .pyd file?

mfatih7 avatar Jul 24 '24 14:07 mfatih7

Which .dll did you place near the .pyd file?

The .dll of the libraries I am using in my C code.

technikamateur avatar Jul 24 '24 17:07 technikamateur

And thanks @henryiii . I will look into it. So there is no way to do it with this project?

technikamateur avatar Jul 24 '24 17:07 technikamateur

You should be able to use CMake to copy the DLL in the right spot. Though cmake does some path fixing when it installs, which this project doesn't include, but if you are manually copying it and it works, you should be able to copy it with CMake too.

henryiii avatar Jul 24 '24 20:07 henryiii

Hello again

When I generate the .pyd file with VSCODE on a computer with Visual Studio 17 2022, the generated example.pyd can easily be imported from Python scripts.

The log printed during examle.pyd generation is like below:

*  Executing task: cmake -S C:\Users\THE_USER\Desktop\pybind11_00 -B C:\Users\mfatih\Desktop\pybind11_00/build 

-- Building for: Visual Studio 17 2022
-- Selecting Windows SDK version 10.0.22621.0 to target Windows 10.0.19045.
-- The C compiler identification is MSVC 19.37.32824.0
-- The CXX compiler identification is MSVC 19.37.32824.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.37.32822/bin/Hostx64/x64/cl.exe - skipped
-- Detecting C compile features       
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.37.32822/bin/Hostx64/x64/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found Python3: C:/ProgramData/anaconda3/envs/env3_11/python.exe (found version "3.11.9") found components: Interpreter Development Development.Module Development.Embed
CMake Deprecation Warning at thirdparty/pybind11/CMakeLists.txt:8 (cmake_minimum_required):
 Compatibility with CMake < 3.5 will be removed from a future version of
 CMake.

 Update the VERSION argument <min> value or use a ...<max> suffix to tell
 CMake that the project does not need compatibility with older versions.


-- pybind11 v2.9.2 
-- Performing Test HAS_MSVC_GL_LTCG
-- Performing Test HAS_MSVC_GL_LTCG - Success
-- Configuring done (6.9s)
-- Generating done (0.1s)
-- Build files have been written to: C:/Users/THE_USER/Desktop/pybind11_00/build
*  Terminal will be reused by tasks, press any key to close it. 

*  Executing task: cmake --build C:\Users\THE_USER\Desktop\pybind11_00/build --target example 

I am still not sure that pybind11 is compatible with VSCODE usage. You can check here. It is written that: On Windows, only Visual Studio 2017 and newer are supported.

I can share the VSCODE project if needed.

mfatih7 avatar Jul 24 '24 22:07 mfatih7

Hello

Now .pyd generation and .pyd import give no error. The problem in my case was only related to the compiler. Using Visual Studio compiler is fine for evrything.

mfatih7 avatar Jul 26 '24 09:07 mfatih7