Cmake在Windows 10上找不到python37.dll,但文件路径在缓存文件中。(pybind11)

2024-10-01 02:26:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在进行Windows 10机器64位安装,在典型的“Program Files”目录中安装了python 3.7.9(不在Anaconda下),在我的工作目录中安装了pybind11。这里的目标只是让PybDun11从C++运行。程序编译正常,但可执行文件未运行,出现错误“代码执行无法继续,因为找不到python37.dll”。重新安装程序可能会解决此问题。但是,python37.dll(和调试版本)文件位置位于生成的CMakeCache.txt文件中(包括以下以及其他详细信息)我试着在CMakeCache.txt的文件路径中引用引文,希望这个错误仅仅是因为“程序文件”目录名中的空间,并且在环境路径变量(程序1)中有“程序文件”的短代码版本,但是错误仍然存在。我是Cmake和C++的新手。

我发现的其他相关帖子涉及移动文件(https://www.reddit.com/r/techsupport/comments/c2z5rl/python37dll_not_found_but_i_find_it_in_windows/,这不是这里的问题),或者没有安装python的系统(python37.dll not linked in executable,这里也不是这样),Anaconda安装的问题(Why do I get a python37.dll error when starting jupyter notebook via conda,但我没有运行Anaconda),或者其他我没有使用的软件。所有其他链接指向下载站点


两个程序的安装过程中都没有错误。下面给出了程序目录、相关代码、生成命令和输出

在./NewKamodo/build目录中,cmd提示符下的by命令的输出是(为了隐私起见,我将我的内部结构替换为…):

...\NewKamodo\build>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  

...\NewKamodo\build>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 -> ...\NewKamodo\build\Debug\KamodoCXX.exe  
  Building Custom Rule .../NewKamodo/CMakeLists.txt  

...\NewKamodo\build>cd Debug  
...\NewKamodo\build\Debug>KamodoCXX.exe  

弹出窗口错误:“由于未找到python37.dll,代码执行无法继续。重新安装程序可能会解决此问题。”


我的程序目录结构是:
纽卡莫多

  • /建造
  • /pybind11
  • CMakeLists.txt
  • KamodoCXX.cpp

< KamodoCXX.cpp中的C++代码是从^ {A4}中获取并复制到的Hello World示例。

#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
}

CMakeLists.txt文件也很简单:

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内容(仅为简洁起见,文件的python部分):

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 Files\Python37\Lib;C:\Program Files\Python37\Lib;C:\Program Files\Python37\Lib\site-packages;C:\Program Files\Python37  \Lib\site-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 


Tags: 文件tobuildcompilercxxfilesprogrampython3
2条回答

下面是我们过去使用python 2.7所做的,但它可能适用于3+。我们通过从cmake运行python获得python库的完整路径

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)

*为python3编辑

我和Pybind11开发人员在这篇BUG文章中合作,以实现最终的解决方案,我将在下面进行总结。一路上出现的另一个错误是:

Fatal Python Error: initfsencoding: unable to load the file system codec
ModuleNotFoundError: No module named ‘encodings’

尝试从命令提示符运行可执行文件时

https://github.com/pybind/pybind11/issues/2855#issuecomment-777648383


在Windows 10上(不使用linux的Windows子系统):

  • 安装Anaconda或miniconda
  • 使用命令构建conda环境
conda create -n embedtest -c conda-forge python==3.7.9 pybind11==2.6.2 cmake=3.19.4
conda activate embedtest

注意:3.15之后的任何版本的cmake似乎都可以工作。我没有指定pybind11或cmake的哪个版本,而是成功地使用了cmake 3.19.4。(Pybind11版本2.6.2是当前默认值)。此外,避免使用python版本的3.8或3.9,因为已知这些版本不起作用(请参阅pybind11文档)

CMakeLists.txt内容:

cmake_minimum_required(VERSION 3.15)
project(main)

find_package(Python COMPONENTS Interpreter Development) 
find_package(pybind11 REQUIRED)

add_executable(main main.cpp)
target_link_libraries(main pybind11::embed)
  • 接下来,在激活conda环境的windows命令提示符下运行以下命令,您已移动到main.cpp和CMakeLists.txt文件所在的目录。注意,-B命令在名为build的子目录中生成输出
cmake -B build -A x64
cmake  build build
set PYTHONHOME=C:\Users\test\Miniconda3\venvs\embedtest
build\Debug\main.exe

注意:必须从终端设置PYTHONHOME变量,因为使用cmake设置此变量不会使新定义在构建时或执行时可用,从而导致错误。这里给出的目录示例是python可执行文件在conda环境中的位置,在使用find_package(python组件…)找到python包后,可以通过向CMakeLists.txt代码添加以下行轻松获得该位置

message(STATUS ${Python_EXECUTABLE})

如果您的目标是在没有虚拟环境的情况下使用pybind11,则不需要set(仅限Python_VIRTUALENV)行,但调用的Python版本将是默认安装的版本


如果希望通过WSL在Windows 10上使用pybind11,则说明几乎相同。CMakeLists.txt和main.cpp文件相同,但终端命令略有不同:

cmake -B build
cmake  build build
cd build
chmod +x main
./main

相关问题 更多 >