出现此错误:
sudo: unable to resolve host coderw@ll
-- Could NOT find PythonLibs (missing: PYTHON_LIBRARIES PYTHON_INCLUDE_DIRS)
CMake Error at /usr/share/cmake-2.8/Modules/FindPackageHandleStandardArgs.cmake:108
(message):
Could NOT find PythonInterp (missing: PYTHON_EXECUTABLE)
Call Stack (most recent call first):
/usr/share/cmake-2.8/Modules/FindPackageHandleStandardArgs.cmake:315
(_FPHSA_FAILURE_MESSAGE)
/usr/share/cmake-2.8/Modules/FindPythonInterp.cmake:139
(FIND_PACKAGE_HANDLE_STANDARD_ARGS)
Code/cmake/Modules/FindNumPy.cmake:10 (find_package)
CMakeLists.txt:114 (find_package)
-- Configuring incomplete, errors occurred!
See also "/home/coderwall/Desktop/rdkit/build/CMakeFiles/CMakeOutput.log".
See also "/home/coderwall/Desktop/rdkit/build/CMakeFiles/CMakeError.log".
我已经安装了:
环境变量已设置如下:
PYTHON_INCLUDE_DIRS=/usr/include/python2.7
PYTHON_LIBRARIES=/usr/lib/python2.7/config/libpython2.7.so
python.h
的位置:/usr/lib/include/python2.7/python.h
python
库的位置:/usr/lib/python2.7/
如何解决这个问题?
您可以通过向
cmake
命令附加填充相应文件夹的 -DPYTHON_LIBRARY
和 -DPYTHON_INCLUDE_DIR
标志来修复错误。
因此,技巧就是用Python解释器返回的信息来填充这些参数,这是最可靠的。这可能与您的 python 位置/版本无关(也适用于 Anaconda 用户):
$ cmake .. \
-DPYTHON_INCLUDE_DIR=$(python -c "import sysconfig; print(sysconfig.get_path('include'))") \
-DPYTHON_LIBRARY=$(python -c "import sysconfig; print(sysconfig.get_config_var('LIBDIR'))")
如果您要链接到 cmake 的 python 版本是 Python3.X 并且默认的 python 符号链接指向 Python2.X,则可以使用
python3 -c ...
代替 python -c ...
。
如果错误仍然存在,您可能需要将
cmake
更新到@pdpcosta所述的更高版本,然后再次重复该过程。
对我来说这很有帮助:
# if using python2
apt-get install python-dev
# if using python3
apt-get install python3-dev
我遇到了同样的问题,并发现错误消息给出了误导性的变量名称。尝试设置以下内容(单数而不是复数):
PYTHON_INCLUDE_DIR=/usr/include/python2.7
PYTHON_LIBRARY=/usr/lib/python2.7/config/libpython2.7.so
您看到的错误消息涉及的(多个)变量是 PythonLibs 在正确初始化时设置的值。
即使按照上面的建议添加了
-DPYTHON_INCLUDE_DIR
和-DPYTHON_LIBRARY
,我仍然面临错误Could NOT find PythonInterp
。解决这个问题的方法是将 -DPYTHON_EXECUTABLE:FILEPATH=
添加到 cmake
,如 https://github.com/pybind/pybind11/issues/99#issuecomment-182071479: 中的建议
cmake .. \
-DPYTHON_INCLUDE_DIR=$(python -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())") \
-DPYTHON_LIBRARY=$(python -c "import distutils.sysconfig as sysconfig; print(sysconfig.get_config_var('LIBDIR'))") \
-DPYTHON_EXECUTABLE:FILEPATH=`which python`
我在尝试在 Xubuntu 14.04 Thrusty Tahr 系统上编译 OpenCV 3 时遇到了这个问题。 安装了Python的所有开发包后,配置过程总是返回消息:
Could NOT found PythonInterp: /usr/bin/python2.7 (found suitable version "2.7.6", minimum required is "2.7")
Could NOT find PythonLibs (missing: PYTHON_INCLUDE_DIRS) (found suitable exact version "2.7.6")
Found PythonInterp: /usr/bin/python3.4 (found suitable version "3.4", minimum required is "3.4")
Could NOT find PythonLibs (missing: PYTHON_LIBRARIES) (Required is exact version "3.4.0")
Thrusty Tahr 存储库上提供的 CMake 版本是 2.8。
一些帖子激励我升级 CMake。
我添加了一个 PPA CMake 存储库,它安装 CMake 版本 3.2。升级后一切顺利,编译成功。
尝试在
set(Python_ADDITIONAL_VERSIONS 3.4)
语句之前添加
find_package
。记得也要清洁
CMakeCache.txt
。
http://ericsilva.org/2012/10/11/restoring-your-python-registry-in-windows/
只需创建一个 .reg 文件来设置必要的键并进行相应的编辑以匹配您的设置。
Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\Software\Python]
[HKEY_CURRENT_USER\Software\Python\Pythoncore]
[HKEY_CURRENT_USER\Software\Python\Pythoncore\2.6]
[HKEY_CURRENT_USER\Software\Python\Pythoncore\2.6\InstallPath]
@="C:\\python26"
[HKEY_CURRENT_USER\Software\Python\Pythoncore\2.6\PythonPath]
@="C:\\python26;C:\\python26\\Lib\\;C:\\python26\\DLLs\\"
[HKEY_CURRENT_USER\Software\Python\Pythoncore\2.7]
[HKEY_CURRENT_USER\Software\Python\Pythoncore\2.7\InstallPath]
@="C:\\python27"
[HKEY_CURRENT_USER\Software\Python\Pythoncore\2.7\PythonPath]
@="C:\\python27;C:\\python27\\Lib\\;C:\\python27\\DLLs\\"
PythonInterp
和
PythonLibs
已更改为
Python
。所以我们使用:
find_package(Python ${PY_VERSION} REQUIRED)
find_package(PythonInterp ${PY_VERSION} REQUIRED)
find_package(PythonLibs ${PY_VERSION} REQUIRED)
请参阅 https://cmake.org/cmake/help/v3.12/module/FindPython.html 了解详细信息。
sudo apt-get install python3.8 python3.8-dev python3.8-distutils python3.8-venv
# find python
execute_process(COMMAND python-config --prefix OUTPUT_VARIABLE PYTHON_SEARCH_PATH)
string(REGEX REPLACE "\n$" "" PYTHON_SEARCH_PATH "${PYTHON_SEARCH_PATH}")
file(GLOB_RECURSE PYTHON_DY_LIBS ${PYTHON_SEARCH_PATH}/lib/libpython*.dylib ${PYTHON_SEARCH_PATH}/lib/libpython*.so)
if (PYTHON_DY_LIBS)
list(GET PYTHON_DY_LIBS 0 PYTHON_LIBRARY)
message("-- Find shared libpython: ${PYTHON_LIBRARY}")
else()
message(WARNING "Cannot find shared libpython, try find_package")
endif()
find_package(PythonInterp)
find_package(PythonLibs ${PYTHON_VERSION_STRING} EXACT)
https://cmake.org/cmake/help/v3.0/module/FindPythonLibs.html
您必须为 cmake 设置两个环境变量才能找到一致的版本。 不幸的是,这不是一个通用的解决方案......
cmake -DPYTHON_LIBRARY=${HOME}/.pyenv/versions/3.8.0/lib/libpython3.8.a -DPYTHON_INCLUDE_DIR=${HOME}/.pyenv/versions/3.8.0/include/python3.8/ cern_root/
sudo apt install python2.7-dev
然后PYTHON_INCLUDE_DIR
可以设置为
/usr/include/python2.7
distutils.sysconfig
已被弃用,取而代之的是
sysconfig
。要获取数据结构中的所有变量名称并检查情况,我们可以使用
get_paths函数
import sysconfig
sysconfig.get_paths()
这将返回一个 dict
,其中所有相关变量名称作为键,相应的路径作为值。 当我们知道键时,我们可以动态获取值,
>>> sysconfig.get_path("include")
'C:\\Program Files\\Python310\\Include'
但是,对于这种情况,sysutils
最方便的功能是它可以通过调用
python -m sysconfig
:从命令提示符一次列出所有变量
$ python -m sysconfig
Platform: "macosx-10.4-i386"
Python version: "3.2"
Current installation scheme: "posix_prefix"
Paths:
data = "/usr/local"
include = "/Users/tarek/Dev/svn.python.org/py3k/Include"
platinclude = "."
platlib = "/usr/local/lib/python3.2/site-packages"
platstdlib = "/usr/local/lib/python3.2"
purelib = "/usr/local/lib/python3.2/site-packages"
scripts = "/usr/local/bin"
stdlib = "/usr/local/lib/python3.2"
Variables:
AC_APPLE_UNIVERSAL_BUILD = "0"
AIX_GENUINE_CPLUSPLUS = "0"
AR = "ar"
ARFLAGS = "rc"
...
sudo apt-get install cython cython3
#include "Python.h"
int main() {
Py_Initialize();
PyRun_SimpleString("print('Hello World')");
Py_Exit(0);
return 0;
}
在我的CMakeLists.txt
中使用以下条目:
find_package(Python 3.12 COMPONENTS Interpreter Development REQUIRED)
target_include_directories(praktikum_2024_05_14_hashtable PRIVATE ${Python_INCLUDE_DIRS})
target_link_libraries(praktikum_2024_05_14_hashtable ${Python_LIBRARIES})