Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
199 views
in Technique[技术] by (71.8m points)

python - Cmake can't find python37.dll on Windows 10, but file path is in the Cache file. (pybind11)

I am working on a Windows 10 machine 64-bit installation with python 3.7.9 installed (not under Anaconda) in the typical "Program Files" directory and pybind11 installed in my working directory. The goal here is simply to get pybind11 running from C++. The program compiles fine, but the executable doesn't run, giving the error "The code execution cannot proceed because python37.dll was not found." Reinstalling the program may fix this problem." However, the python37.dll (and the debug version) file locations are in the CMakeCache.txt file generated (included below along with other details). I tried putting quotes around the file paths in CMakeCache.txt in hopes that the error was simply due to the space in the "Program Files" directory name, and have the short code versions of "Program Files" in the environment path variables (PROGRA~1), but the error persists. I am new to Cmake and C++. What am I missing?

The other related posts I found addressed moving files (https://www.reddit.com/r/techsupport/comments/c2z5rl/python37dll_not_found_but_i_find_it_in_windows/, which is not the issue here), or a system where python was not installed (python37.dll not linked in executable, again not the case here), problems with the Anaconda installation (Why do I get a python37.dll error when starting jupyter notebook via conda, but I'm not running Anaconda), or other software I'm not using. All the others links point to download sites.


There were no errors in the installation processes of either program. The program directories, relevant codes, build commands and output are given below.

From the ./NewKamodo/build directory, by commands at the cmd prompt with the output are (I have replaced my internal structure with ... for privacy):

...NewKamodouild>cmake .. -G"Visual Studio 15 2017" -A x64  
-- Selecting Windows SDK version 10.0.17763.0 to target Windows 10.0.19041.  
-- The C compiler identification is MSVC 19.16.27045.0  
-- The CXX compiler identification is MSVC 19.16.27045.0  
-- Detecting C compiler ABI info  
-- Detecting C compiler ABI info - done  
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/BuildTools/VC/Tools/MSVC/14.16.27023/bin/Hostx86/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 (x86)/Microsoft Visual Studio/2017/BuildTools/VC/Tools/MSVC/14.16.27023/bin/Hostx86/x64/cl.exe - skipped  
-- Detecting CXX compile features  
-- Detecting CXX compile features - done  
-- pybind11 v2.6.3 dev1  
-- Found PythonInterp: C:/Program Files/Python37/python.exe (found version "3.7.9")  
-- Found PythonLibs: C:/Program Files/Python37/libs/python37.lib  
-- Performing Test HAS_MSVC_GL_LTCG  
-- Performing Test HAS_MSVC_GL_LTCG - Success  
-- Found Python3: C:/Program Files/Python37/python.exe (found version "3.7.9") found components: Interpreter Development Development.Module Development.Embed  
-- Configuring done  
-- Generating done  
-- Build files have been written to: .../NewKamodo/build  

...NewKamodouild>cmake --build .  
Microsoft (R) Build Engine version 15.9.21+g9802d43bc3 for .NET Framework  
Copyright (C) Microsoft Corporation. All rights reserved.  

  Checking Build System  
  Building Custom Rule .../NewKamodo/CMakeLists.txt  
  KamodoCXX.cpp  
  KamodoCXX.vcxproj -> ...NewKamodouildDebugKamodoCXX.exe  
  Building Custom Rule .../NewKamodo/CMakeLists.txt  

...NewKamodouild>cd Debug  
...NewKamodouildDebug>KamodoCXX.exe  

pop up window error: "The code execution cannot proceed because python37.dll was not found. Reinstalling the program may fix this problem."


My program directory structure is:
NewKamodo

  • ./build
  • ./pybind11
  • CMakeLists.txt
  • KamodoCXX.cpp

The C++ code in KamodoCXX.cpp is the hello world example taken from https://pybind11.readthedocs.io/en/stable/advanced/embedding.html and copied below.

#include <pybind11/embed.h> // everything needed for embedding
namespace py = pybind11;

int main() {
    py::scoped_interpreter guard{}; // start the interpreter and keep it alive

    py::print("Hello, World!"); // use the Python API
}

The CMakeLists.txt file is also simple:

cmake_minimum_required(VERSION 3.4)
project(KamodoCXX)

#specify the C++ standard
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_REQUIRED True)

add_subdirectory(pybind11)
find_package(Python3 COMPONENTS Interpreter Development)

add_executable(KamodoCXX KamodoCXX.cpp)
target_link_libraries(KamodoCXX PRIVATE pybind11::embed)

CMakeCache.txt contents (python portion of file only for brevity):
...

PYBIND11_INCLUDE_DIR:INTERNAL=.../NewKamodo/pybind11/include
//ADVANCED property for variable: PYTHON_EXECUTABLE
PYTHON_EXECUTABLE-ADVANCED:INTERNAL=1
PYTHON_INCLUDE_DIRS:INTERNAL=C:/Program Files/Python37/include
PYTHON_IS_DEBUG:INTERNAL=0
PYTHON_LIBRARIES:INTERNAL=C:/Program Files/Python37/libs/python37.lib
PYTHON_MODULE_EXTENSION:INTERNAL=.cp37-win_amd64.pyd
PYTHON_MODULE_PREFIX:INTERNAL=
PYTHON_VERSION:INTERNAL=3.7.9
PYTHON_VERSION_MAJOR:INTERNAL=3  
PYTHON_VERSION_MINOR:INTERNAL=7  
Python_ADDITIONAL_VERSIONS:INTERNAL=3.10;3.9;3.8;3.7;3.6;3.5;3.4  
//CMAKE_INSTALL_PREFIX during last run  
_GNUInstallDirs_LAST_CMAKE_INSTALL_PREFIX:INTERNAL=C:/Program Files/KamodoCXX  
_Python:INTERNAL=PYTHON  
_Python3_DEVELOPMENT_EMBED_SIGNATURE:INTERNAL=78025eb1b345e021c9dadb673f40d7b6  
_Python3_DEVELOPMENT_MODULE_SIGNATURE:INTERNAL=78025eb1b345e021c9dadb673f40d7b6  
//Path to a program.  
_Python3_EXECUTABLE:INTERNAL=C:/Program Files/Python37/python.exe  
//Path to a file.  
_Python3_INCLUDE_DIR:INTERNAL=C:/Program Files/Python37/include  
//Python3 Properties  
_Python3_INTERPRETER_PROPERTIES:INTERNAL=Python;3;7;9;64;;cp37-win_amd64;C:Program FilesPython37Lib;C:Program FilesPython37Lib;C:Program FilesPython37Libsite-packages;C:Program FilesPython37  Libsite-packages  
_Python3_INTERPRETER_SIGNATURE:INTERNAL=d8451d76c667fb34c5414ba253c4dadd  
//Path to a library.  
_Python3_LIBRARY_DEBUG:INTERNAL=C:/Program Files/Python37/libs/python37_d.lib  
//Path to a library.  
_Python3_LIBRARY_RELEASE:INTERNAL=C:/Program Files/Python37/libs/python37.lib  
//Path to a library.  
_Python3_RUNTIME_LIBRARY_DEBUG:INTERNAL=C:/Program Files/Python37/python37_d.dll  
//Path to a library.  
_Python3_RUNTIME_LIBRARY_RELEASE:INTERNAL=C:/Program Files/Python37/python37.dll  
//True if pybind11 and all required components found on the system
pybind11_FOUND:INTERNAL=TRUE  
//Directory where pybind11 headers are located  
pybind11_INCLUDE_DIR:INTERNAL=.../NewKamodo/pybind11/include  
//Directories where pybind11 and possibly Python headers are located  
pybind11_INCLUDE_DIRS:INTERNAL=.../NewKamodo/pybind11/include;C:/Program Files/Python37/include 

question from:https://stackoverflow.com/questions/66052355/cmake-cant-find-python37-dll-on-windows-10-but-file-path-is-in-the-cache-file

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here's what we used to do with python 2.7 but it might work for 3+. We obtain the full path to the python libraries by running python from cmake.

add_subdirectory(pybind11)
find_package(Python3 COMPONENTS Interpreter Development)
FIND_PACKAGE(PythonLibs "${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR}" REQUIRED)
execute_process(COMMAND ${PYTHON_EXECUTABLE} -c "import sys; from distutils.sysconfig import get_python_lib; print(get_python_lib().split(sys.prefix)[-1][1:])"
                    OUTPUT_VARIABLE PYTHON_SITE_PACKAGES OUTPUT_STRIP_TRAILING_WHITESPACE)

*edited for python3


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...