在Python環(huán)境下執(zhí)行import dlib可能會報如下錯誤:
(tensorflow) wuchendeMacBook-Pro:~ wuchen$ python
Python 2.7.10 (default, Feb 7 2017, 00:08:15)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import dlib
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/wuchen/Libraries/tensorflow/lib/python2.7/site-packages/dlib/__init__.py", line 1, in <module>
from .dlib import *
ImportError: dlopen(/Users/wuchen/Libraries/tensorflow/lib/python2.7/site-packages/dlib/dlib.so, 2): Library not loaded: libboost_python.dylib
Referenced from: /Users/wuchen/Libraries/tensorflow/lib/python2.7/site-packages/dlib/dlib.so
Reason: image not found
問題出在編譯完成boost python之后书释,環(huán)境變量沒有正確地設置荡灾。
有兩個環(huán)境變量需要設置:
- 第一個是boost的根目錄
如果不設置這個路徑芯侥,編譯dlib的時候柑晒,還是會編譯通過不了疑务。
在蘋果電腦上宝与,這個環(huán)境變量是這樣設置的:
export PATH="/Users/wuchen/Libraries/boost_1_64_0:$PATH"
- 第二個是編譯成功的動態(tài)庫的路徑
如果沒有正確設置這個路徑,雖然dlib能成功安裝捉捅,但是在Python環(huán)境下執(zhí)行import dlib的時候就會報文章開頭提到的錯誤撤防。
在蘋果電腦上,這個環(huán)境變量是這么設置的:
export DYLD_LIBRARY_PATH="/Users/wuchen/Libraries/boost_1_64_0/stage/lib:$DYLD_LIBRARY_PATH"
這里DYLD_LIBRARY_PATH不能用PATH取代棒口,否則仍然會報錯寄月。
如果是Linux系統(tǒng),這個環(huán)境變量的名稱是LD_LIBRARY_PATH无牵。
如果是Cygwin環(huán)境下漾肮,則用PATH就可以了。
參考文獻:http://www.boost.org/doc/libs/1_64_0/more/getting_started/unix-variants.html