简体   繁体   English

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

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

I have 2 versions of python installed, but cmake is using older version.我安装了 2 个版本的 python,但cmake使用的是旧版本。 How do I force cmake to use the newer version?如何强制cmake使用较新版本?

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新的“FindPython3”和“FindPython2”模块,以及一个新的
"FindPython" module, have been added to provide a new way to locate已添加“FindPython”模块以提供一种新的定位方式
python environments. python环境。

find_package(Python COMPONENTS Interpreter Development)

Docs : 文档

This module looks preferably for version 3 of Python.该模块最好用于 Python 版本 3。 If not found, version 2 is searched.如果未找到,则搜索版本 2。 To manage concurrent versions 3 and 2 of Python, use FindPython3 and FindPython2 modules rather than this one.要管理 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)

Try to add -DPYTHON_EXECUTABLE:FILEPATH=/path/to/python2.7 It 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 .

I had a similar problem, and resolved it using Paul's answer as a hint.我有一个类似的问题,并使用保罗的回答作为提示解决了它。 I needed to use python2.7 to compile an older library, but cmake keeps picking up my python3.2 libraries (and executable).我需要使用python2.7来编译一个较旧的库,但是cmake一直在选择我的python3.2库(和可执行文件)。

First, I ran cmake with default options, then edited the CMakeCache.txt file which it generated.首先,我使用默认选项运行cmake ,然后编辑它生成的CMakeCache.txt文件。 I did it this way primarily because I didn't know the proper -D... incantations to cause cmake to get the python library and include paths, etc right in the first place.我这样做主要是因为我不知道正确的-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

And replaced every occurrence of python3.2 with python2.7 .并用python2.7替换了每次出现的python3.2 I also had to rename the PYTHON_EXECUTABLE to use python2.7 , since python is a symlink to python3.2 on my system.我还必须重命名PYTHON_EXECUTABLE以使用python2.7 ,因为python是我系统上python3.2的符号链接。

Then I reran cmake .然后我重新运行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.至少,它在我的身上做到了。

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

My use case was a rather large project in which C++ classes were made available to Python scripts via Boost.Python .我的用例是一个相当大的项目,其中 C++ 类通过Boost.Python提供给 Python 脚本。 After having fought the various quirks of CMake's Python interpreter and library detection, I finally gave up and rolled my own.在与 CMake 的 Python 解释器和库检测的各种怪癖作斗争之后,我终于放弃并推出了自己的。 My approach is based on a slightly after-edited version of the python-config script that is sometimes (but not always!) put into a newly created virtual environment ( see this SO post on pyvenv for these issues, but I digress).我的方法基于python-config脚本的稍微经过编辑的版本,有时(但不总是!)放入新创建的虚拟环境中(有关这些问题, 请参阅pyvenv上的这篇 SO 帖子,但我离题了)。 This script is invoked by a small CMake snippet pyconfig.cmake .该脚本由一个小的 CMake 片段pyconfig.cmake调用。 Both are freely available from the GitHub repo cmake-python-config .两者都可以从 GitHub 存储库cmake-python-config 免费获得

Warning: The scripts assume that you have a Python 3 interpreter in your PATH .警告:脚本假设您的PATH有一个 Python 3 解释器。 Detection of Python 2 is not attempted.未尝试检测 Python 2。 The scripts do not attempt to find all installed versions of Python3 either.这些脚本也不会尝试查找所有已安装的 Python3 版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM