简体   繁体   中英

How to set the default library path for python

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!!!

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>

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.

I think I have solved this problem. I added the following sentences to my .bashrc file and it works.

export PYTHON_LIBRARY=/ghome/mypath/anaconda2/lib/libpython2.7.so
export PYTHON_INCLUDE_DIR=/ghome/mypath/anaconda2/include:/ghome/mypath/anaconda2/include/python2.7

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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