Cmake is not able to find Python-libraries
Asked Answered
D

15

81

Getting this error:

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

I have already installed:

  1. sudo apt-get install python-dev
  2. Environment variable are already set as follow:

    PYTHON_INCLUDE_DIRS=/usr/include/python2.7 
    PYTHON_LIBRARIES=/usr/lib/python2.7/config/libpython2.7.so
    

Location of python.h : /usr/lib/include/python2.7/python.h

Location of python libs: /usr/lib/python2.7/ How to solve this?

Dalenedalenna answered 11/6, 2014 at 23:48 Comment(3)
Hmmm, try setting PYTHON_EXECUTABLE=...?Stockinet
Tried and it recognized but still : Could NOT find PythonLibs (missing: PYTHON_LIBRARIES PYTHON_INCLUDE_DIRS) Dalenedalenna
Could you please include the code of the rdkit root CMakeLists.txt file to your question? That might help.Nihhi
V
79

You can fix the errors by appending to the cmake command the -DPYTHON_LIBRARY and -DPYTHON_INCLUDE_DIR flags filled with the respective folders.

Thus, the trick is to fill those parameters with the returned information from the python interpreter, which is the most reliable. This may work independently of your python location/version (also for Anaconda users):

$ 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'))")

If the version of python that you want to link against cmake is Python3.X and the default python symlink points to Python2.X, python3 -c ... can be used instead of python -c ....

In case that the error persists, you may need to update the cmake to a higher version as stated by @pdpcosta and repeat the process again.

Violoncellist answered 30/6, 2016 at 11:36 Comment(6)
print(sysconfig.get_config_var('LIBDIR'))" prints /opt/conda/lib, but then I get WARNING: Target "something-something" requests linking to directory "/opt/conda/lib". Targets may link only to libraries. CMake is dropping the item. - Do we need to give -DPYTHON_LIBRARY the path to the .so libpython?Sakti
@Sakti yes, I changed the command to give the path to the .so like so: -DPYTHON_LIBRARY=$(python -c "import distutils.sysconfig as sysconfig; import os; print(os.path.join(sysconfig.get_config_var('LIBDIR'), sysconfig.get_config_var('LDLIBRARY')))")Moulden
For anaconda in particular, this worked for me (and will hopefully generalize more easily): 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)Hengist
This is the best answer, as it automatically adjusts to the current python (which python) and works seamlessly when a conda env is active).Stelle
Note (I hope it helps M1 Mac people using Cmake with Python 3): in my case in order to make my specific problem work I needed to do DPYTHON3_LIBRARY and DPYTHON3_INCLUDE_DIR. It didn't work without the 3.Rapeseed
For anybody else seeing the deprecation warning for distutils.sysconfig. You can use sysconfig and call sysconfig.get_path('include') for the -DPYTHON_INCLUDE_DIR and sysconfig.get_config_var('LIBDIR')Comprehensive
J
49

For me this is helpful:

# if using python2
apt-get install python-dev

# if using python3
apt-get install python3-dev
Jacintha answered 7/4, 2015 at 20:18 Comment(5)
For me (on Centos7) this line solved the issue: yum install python34-develGelderland
This worked for me, without any of the cmake arguments (-DPYTHON_LIBRARY etc) suggested by the other answers. I had python-dev, but had forgotten python3-dev.Botsford
Very bad answer. It's like, if it doesn't work, try to install everything. It's either python2 or python3 you need... (Now in 2020, better update your answer as python2 is deprecated)Agitato
This should be marked as the correct answer.Methuselah
This solved the issue for me.Rachele
H
21

I hit the same issue,and discovered the error message gives misleading variable names. Try setting the following (singular instead of plural):

PYTHON_INCLUDE_DIR=/usr/include/python2.7 
PYTHON_LIBRARY=/usr/lib/python2.7/config/libpython2.7.so

The (plural) variables you see error messages about are values that the PythonLibs sets up when it is initialised correctly.

Headwards answered 15/4, 2015 at 17:50 Comment(1)
This really needs to be mentioned in the documentation for opencv installs. Also they could be a little more helpful about explaining the available cmake options when building from source. docs.opencv.org/master/d7/d9f/tutorial_linux_install.htmlReceive
W
19

Even after adding -DPYTHON_INCLUDE_DIR and -DPYTHON_LIBRARY as suggested above, I was still facing the error Could NOT find PythonInterp. What solved it was adding -DPYTHON_EXECUTABLE:FILEPATH= to cmake as suggested in 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`
Westmoreland answered 13/1, 2018 at 16:25 Comment(2)
Thanks! I also found that github page but I was only setting the PYTHON_EXECUTABLE:FILEPATH and that didn't work. Your solution solved my issue when trying to build OpenVDB on MacOSX (Mojave).Crystalloid
Thanks so much, I can't find libpython-x.so and failed to manual compile&install opencv-python, and this way solved my prolemCarafe
S
15

I was facing this problem while trying to compile OpenCV 3 on a Xubuntu 14.04 Thrusty Tahr system. With all the dev packages of Python installed, the configuration process was always returning the message:

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

The CMake version available on Thrusty Tahr repositories is 2.8. Some posts inspired me to upgrade CMake. I've added a PPA CMake repository which installs CMake version 3.2.

After the upgrade everything ran smoothly and the compilation was successful.

Sporangium answered 15/6, 2015 at 14:31 Comment(2)
And do not forget clean all cmake caches firstly!Backstroke
@Backstroke how do you do that? cmake clean?Eclectic
C
10

Some last version of Ubuntu installs Python 3.4 by default and the CMake version from Ubuntu (2.8) only searches up to Python 3.3.

Try to add set(Python_ADDITIONAL_VERSIONS 3.4) before the find_package statement.

Remember to clean CMakeCache.txt too.

Celtuce answered 9/2, 2015 at 16:25 Comment(2)
This was the problem for my 14.04 Ubuntu system. I fixed it by installing a newer cmake version (askubuntu.com/a/945589/187411)Clarkclarke
I solved the problem by Remember to clean CMakeCache.txt too. many thanks Patrizio. in the mbec-cli2 CMake (not found jinja2, intelhex, prettytable).Shenyang
V
7

This problem can also happen in Windows. Cmake looks into the registry and sometimes python values are not set. For those with similar problem:

http://ericsilva.org/2012/10/11/restoring-your-python-registry-in-windows/

Just create a .reg file to set the necessary keys and edit accordingly to match your setup.

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\\"
Vietcong answered 17/11, 2015 at 14:47 Comment(1)
Will this also work if I have both 32 and 64 bit versions installed at the same time and want to build both 32 and 64 bit C++ binaries?Drugi
N
2

Note that if you are using cMake version 3.12 or later, variable PythonInterp and PythonLibs has been changed into Python.

So we use:

find_package(Python ${PY_VERSION} REQUIRED)

instead of:

find_package(PythonInterp ${PY_VERSION} REQUIRED) find_package(PythonLibs ${PY_VERSION} REQUIRED)

see https://cmake.org/cmake/help/v3.12/module/FindPython.html for details.

Nab answered 19/11, 2018 at 3:54 Comment(0)
S
2

I had upgraded to python3.8 on my system and had an incomplete install. Managed to fix it by installing the rest of the 3.8 packages:

sudo apt-get install python3.8 python3.8-dev python3.8-distutils python3.8-venv
Stanford answered 13/12, 2021 at 19:41 Comment(0)
O
1

Paste this into your CMakeLists.txt:

# 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)
Olid answered 3/7, 2018 at 6:51 Comment(0)
T
1

In case that might help, I found a workaround for a similar problem, looking at the cmake doc : https://cmake.org/cmake/help/v3.0/module/FindPythonLibs.html

You must set two env vars for cmake to find coherent versions. Unfortunately this is not a generic solution...

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/
Thebaid answered 28/10, 2019 at 21:34 Comment(0)
E
1

On Ubuntu 20 at least, since Python 2.7 is deprecated, you can:

sudo apt install python2.7-dev

And then PYTHON_INCLUDE_DIR can be set to /usr/include/python2.7

Emulsify answered 15/4, 2022 at 7:55 Comment(0)
I
1

In Python 3.2 and onward distutils.sysconfig is deprecated in favor of sysconfig.

To get all the variable names in data structure and inspect the situation we can use get_paths function

import sysconfig
sysconfig.get_paths()

which will return us a dict with all the relevant variable names as keys, and corresponding paths as values. When we know the key we can get the value dynamically,

>>> sysconfig.get_path("include")
'C:\\Program Files\\Python310\\Include'

However, the most convenient feature of sysutils for this situation is that it can list all the variables at once from the command-prompt by invoking 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"
        ...
Icelandic answered 10/8, 2022 at 9:47 Comment(0)
P
0

Maybe below command line works for you if all of above methods didn't work.

sudo apt-get install cython cython3

Palua answered 21/6, 2021 at 3:7 Comment(0)
B
0

The question is a bit old and things have changed a lot so I stumbled while trying to find a solution today. Finally, I managed to run the following code

#include "Python.h"

int main() {
    Py_Initialize();
    PyRun_SimpleString("print('Hello World')");
    Py_Exit(0);
    return 0;
}

using the following entries in my 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})
Brow answered 13/5 at 16:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.