Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 266
pfc Avatar asked Nov 04 '25 23:11

pfc


2 Answers

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>
like image 96
Tsyvarev Avatar answered Nov 07 '25 16:11

Tsyvarev


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.

like image 31
beachgreatsquaremoon Avatar answered Nov 07 '25 15:11

beachgreatsquaremoon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!