81

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

Sanjeev
  • 811
  • 1
  • 6
  • 5

6 Answers6

54

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

For 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.

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.

For CMake < 3.12

Docs:

find_package(PythonInterp 2.7 REQUIRED)
find_package(PythonLibs 2.7 REQUIRED)
jadelord
  • 1,511
  • 14
  • 19
  • Works nicely, also using Travis. – Michael Koenig Mar 18 '16 at 14:04
  • 1
    So easy... I feel dumb. – dividebyzero May 09 '16 at 09:55
  • Works also for python 3.6 installed with Conda in user home directory! – Alejandro Daniel Noel Mar 15 '18 at 06:39
  • Wait i dont get this, how do you use this? like what is the command i can use in my command line? – Tarun May 05 '20 at 17:53
  • 1
    @Tarun it goes into CMakeLists.txt and cmake is the command, here is a [more complete example](https://github.com/dev-cafe/cmake-cookbook/blob/f8175d47f1b178368f67e04049375fd494831047/chapter-03/recipe-01/example/CMakeLists.txt) – jadelord May 20 '20 at 13:30
  • cold somebody provide the command for "just find me any python2 version"? Are those parameters: `Python COMPONENTS Interpreter Development` – Cutton Eye Sep 10 '20 at 10:29
  • Same here, I do not get what to do with or where to put this thing: find_package(Python COMPONENTS Interpreter Development) and if I should put it as it is or should replace COMPONENTS Interpreter Development for something else – Jesus Almaral - Hackaprende Jan 23 '21 at 20:18
  • As I understand `COMPONENTS` is a keyword / name of the option and `Interpreter Development` are the values. Other components like `Compiler` and `NumPy` are also supported. See [find_package docs](https://cmake.org/cmake/help/latest/command/find_package.html#command:find_package) – jadelord Mar 04 '21 at 22:08
  • 1
    How do you specify python 3.4 or 3.6 or 3.73 ? I want to explicitly specify python. – Dariusz Mar 02 '23 at 14:15
44

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

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

 cmake -DPYTHON_LIBRARIES=/Library/Frameworks/Python.framework/Versions/2.7/lib/libpython2.7.dylib .
Paul
  • 5,756
  • 6
  • 48
  • 78
  • 4
    Note that at least for cmake 2.8 on Ubuntu 16.04, you need to set -DPYTHON_LIBRARY instead of -DPYTHON_LIBRARIES – phiresky Jan 24 '17 at 03:40
36

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).

First, I ran cmake with default options, then edited the CMakeCache.txt file which it generated. 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.

In my CmakeCache.txt, I found lines like this

  • 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. I also had to rename the PYTHON_EXECUTABLE to use python2.7, since python is a symlink to python3.2 on my system.

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.

jpaugh
  • 6,634
  • 4
  • 38
  • 90
  • 2
    This is what worked for me. Providing cmake environment variables did not work. Editing the CmakeCache file did. It's easier to work with anyways – JohnAllen Jun 10 '16 at 16:52
12

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

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

My solution is changing it to 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
jpaugh
  • 6,634
  • 4
  • 38
  • 90
X.H. Jiang
  • 121
  • 1
  • 2
  • For anaconda in particular, this worked for me (and will hopefully generalize more easily): https://github.com/jkhoogland/FindPythonAnaconda (though I had to make two small changes -- they're both in my GitHub fork if upstream doesn't quite work for you either) – Braham Snyder Sep 13 '17 at 01:04
1

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-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). This script is invoked by a small CMake snippet pyconfig.cmake. Both are freely available from the GitHub repo 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 not attempt to find all installed versions of Python3 either.

András Aszódi
  • 8,948
  • 5
  • 48
  • 51
0

on my windows platform like below, unix should be similar:

-DPython3_EXECUTABLE="C:\Users\xx\AppData\Local\Programs\Python\Python310\python.exe" `
-DPython3_LIBRARY="C:\Users\xx\AppData\Local\Programs\Python\Python310\libs\python310.lib" `
-DPython3_INCLUDE_DIR="C:\Users\xx\AppData\Local\Programs\Python\Python310\Include" `
andrewchan2022
  • 4,953
  • 45
  • 48