我安装了 2 个版本的 python,但 cmake 使用的是旧版本。如何强制 cmake 使用较新版本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15291500/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 19:42:41  来源:igfitidea点击:

I have 2 versions of python installed, but cmake is using older version. How do I force cmake to use the newer version?

pythonpython-2.7cmake

提问by Sanjeev

I have 2 versions of python installed, but cmakeis using older version. How do I force cmaketo use the newer version?

我安装了 2 个版本的 python,但cmake使用的是旧版本。如何强制cmake使用较新版本?

回答by Paul

Try to add -DPYTHON_EXECUTABLE:FILEPATH=/path/to/python2.7It might be a path problem?

尝试添加-DPYTHON_EXECUTABLE:FILEPATH=/path/to/python2.7可能是路径问题?

Also could specify the path to your python library,use your version that you want:

也可以指定你的 python 库的路径,使用你想要的版本:

 cmake -DPYTHON_LIBRARIES=/Library/Frameworks/Python.framework/Versions/2.7/lib/libpython2.7.dylib .

回答by jpaugh

I had a similar problem, and resolved it using Paul's answeras a hint. I needed to use python2.7to compile an older library, but cmakekeeps picking up my python3.2libraries (and executable).

我有一个类似的问题,并使用保罗的回答作为提示解决了它。我需要用来python2.7编译一个较旧的库,但cmake一直在选择我的python3.2库(和可执行文件)。

First, I ran cmakewith default options, then edited the CMakeCache.txtfile which it generated. I did it this way primarily because I didn't know the proper -D...incantations to cause cmaketo get the python library and include paths, etc right in the first place.

首先,我cmake使用默认选项运行,然后编辑CMakeCache.txt它生成的文件。我这样做主要是因为我一开始不知道正确的-D...咒语cmake来获取 python 库并包含路径等。

In my CmakeCache.txt, I found lines like this

在我的CmakeCache.txt, 我发现了这样的行

  • Path to a program

    PYTHON_EXECUTABLE:FILEPATH=/usr/bin/python
    
  • Path to a directory

    PYTHON_INCLUDE_DIR:PATH=/usr/include/python3.2
    
  • Path to a library

    PYTHON_LIBRARY:FILEPATH=/usr/lib/libpython3.2.so
    
  • 程序路径

    PYTHON_EXECUTABLE:FILEPATH=/usr/bin/python
    
  • 目录路径

    PYTHON_INCLUDE_DIR:PATH=/usr/include/python3.2
    
  • 图书馆的路径

    PYTHON_LIBRARY:FILEPATH=/usr/lib/libpython3.2.so
    

And replaced every occurrence of python3.2with python2.7. I also had to rename the PYTHON_EXECUTABLEto use python2.7, since pythonis a symlink to python3.2on my system.

和更换的每次出现python3.2python2.7。我还必须重命名PYTHON_EXECUTABLEto use python2.7,因为它pythonpython3.2我系统上的符号链接。

Then I reran cmake. Because it prefers its cached values to actually looking for the libraries, this should work in all cases. At least, it did in mine.

然后我又跑了cmake。因为它更喜欢它的缓存值而不是实际查找库,所以这应该适用于所有情况。至少,它在我的身上做到了。

回答by jadelord

You may try either of these depending on what you need:

您可以根据需要尝试以下任一方法:

For CMake >= 3.12

为了 CMake >= 3.12

According to the changelog:

根据变更日志:

New "FindPython3" and "FindPython2" modules, as well as a new
"FindPython" module, have been added to provide a new way to locate
python environments.

添加了新的“FindPython3”和“FindPython2”模块以及新的
“FindPython”模块,以提供一种定位
python环境的新方法。

find_package(Python COMPONENTS Interpreter Development)

Docs:

文档

This module looks preferably for version 3 of Python. If not found, version 2 is searched. To manage concurrent versions 3 and 2 of Python, use FindPython3 and FindPython2 modules rather than this one.

该模块最好用于 Python 版本 3。如果未找到,则搜索版本 2。要管理 Python 的并发版本 3 和 2,请使用 FindPython3 和 FindPython2 模块而不是这个模块。

For CMake < 3.12

为了 CMake < 3.12

Docs:

文档

find_package(PythonInterp 2.7 REQUIRED)
find_package(PythonLibs 2.7 REQUIRED)

回答by X.H. Jiang

I use anaconda(python 2.7.8) as well as python 2.7.6.

我使用 anaconda(python 2.7.8) 以及 python 2.7.6。

I tried -DPYTHON_EXECUTABLE:FILEPATH=$ANACONDA_HOME/bin, but version 1.4 found (weird:).

我试过了-DPYTHON_EXECUTABLE:FILEPATH=$ANACONDA_HOME/bin,但发现 1.4 版(奇怪:)。

My solution is changing it to PYTHON_EXECUTABLE:

我的解决方案是将其更改为 PYTHON_EXECUTABLE:

cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -DBUILD_TIFF=ON \
-DPYTHON_LIBRARY=$ANACONDA_HOME/lib/libpython2.7.so \
-DPYTHON_INCLUDE_DIR=$ANACONDA_HOME/include/python2.7/ \
-DPYTHON_EXECUTABLE=$ANACONDA_HOME/bin/python

回答by Laryx Decidua

My use case was a rather large project in which C++ classes were made available to Python scripts via Boost.Python. After having fought the various quirks of CMake's Python interpreter and library detection, I finally gave up and rolled my own. My approach is based on a slightly after-edited version of the python-configscript that is sometimes(but not always!) put into a newly created virtual environment (see this SO post on pyvenvfor these issues, but I digress). This script is invoked by a small CMake snippet pyconfig.cmake. Both are freely available from the GitHub repo cmake-python-config.

我的用例是一个相当大的项目,其中 C++ 类通过Boost.Python. 在与 CMake 的 Python 解释器和库检测的各种怪癖作斗争之后,我终于放弃并推出了自己的。我的方法基于python-config脚本的稍微经过编辑的版本,该版本有时(但并非总是!)放入新创建的虚拟环境中(有关pyvenv这些问题,请参阅此 SO 帖子,但我离题了)。此脚本由一个小的 CMake 代码段调用pyconfig.cmake。两者都可以从 GitHub 存储库cmake-python-config 免费获得

Warning: The scripts assume that you have a Python 3 interpreter in your PATH. Detection of Python 2 is not attempted. The scripts do notattempt to find allinstalled versions of Python3 either.

警告:脚本假设您的PATH. 未尝试检测 Python 2。这些脚本也不会尝试查找所有已安装的 Python3 版本。