How to set the default library path for python

29,482

Solution 1

From documentation for CMake module FindPythonLibs:

If you’d like to specify the installation of Python to use, you should modify the following cache variables:

  • PYTHON_LIBRARY - path to the python library
  • PYTHON_INCLUDE_DIR - path to where Python.h is found

Because version is extracted from patchlevel.h header file (this is common practice for Find CMake modules), you need to specify both library and include directory:

cmake -DPYTHON_LIBRARY=/ghome/mypath/anaconda2/lib/libpython2.7.so -DPYTHON_INCLUDE_DIR=<...> <other-cmake-arguments>

Solution 2

You can also make additions to this path with the PYTHONPATH environment variable at runtime, in addition to:

import sys
sys.path.append('/home/user/python-libs')

You have not mentioned the OS , if its linux then you can try searching the directories listed in sys.path.

import sys
print '\n'.join(sys.path)

So Python will find any packages that have been installed to those locations. sys.path is populated using the current working directory, followed by directories listed in your PYTHONPATH environment variable, followed by installation-dependent default paths, which are controlled by the site module. Assuming your PYTHONPATH environment variable is not set, sys.path will consist of the current working directory plus any manipulations made to it by the site module.

Share:
29,482
pfc
Author by

pfc

Updated on March 02, 2020

Comments

  • pfc
    pfc about 4 years

    I'm using Centos 7.2 64-bit. The default version of python is 2.7.5. I installed an anaconda version which is 2.7.13. The default interpreter of python is set to be 2.7.13 as I add the bin path of anaconda to the PATH variable. However, when I was installing the opencv, I got this warning message when doing the cmake:

    Could NOT find PythonLibs: Found unsuitable version "2.7.5", but required is exact version "2.7.13" (found /lib64/libpython2.7.so)
    

    It seems the default path of python library is /lib64/libpython2.7.so. I searched for solutions and it said if I set the environment variable PYTHON_LIBRARY, this will be solved. So I add it as follows:

    export PYTHON_LIBRARY=/ghome/mypath/anaconda2/lib/libpython2.7.so
    

    I re-login. The problem still happens. My cmake command is as follows:

    cmake -D BUILD_opencv_gpu=OFF -D WITH_CUDA=OFF -D WITH_1394=OFF -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/ghome/mypath/software/try_opencv/installed ..
    

    I have been stuck in this problem for hours. Thank you all for helping me!!!