1. 程式人生 > >使用pip安裝tensorflow 0.80,python 使用tensorflow 0.80遇到的問題及處理方法

使用pip安裝tensorflow 0.80,python 使用tensorflow 0.80遇到的問題及處理方法

業務需要使用谷歌的深度學習框架tensorflow,安裝過程中遇到很多問題,真的很難處理,特此記錄。

CentOS6.4
tensorflow 0.80

Python-2.7.11


1、解除安裝原來的pip
2、下載個pip 網站https://pip.pypa.io/en/latest/installing/
3、yum install python-devel libffi-devel openssl-devel

#python get-pip.py

報錯:
出現:zipimport.ZipImportError: can't decompress data; zlib not available錯誤
解決辦法重新編譯一下Python原始碼安裝包,如下:
tar zxvf Python-2.7.11.tgz
cd Python-2.7.11
./configure
vi Modules/Setup
在這裡把454行左右的 找到
#zlib zlibmodule.c -I$(prefix)/include -L$(exec_prefix)/lib -lz
去掉註釋
zlib zlibmodule.c -I$(prefix)/include -L$(exec_prefix)/lib -lz
make
make install


報錯:ImportError:cannot import name HTTPSHandler
解決:
yum install -y openssl openssl-devel
然後重新編譯python

安裝tensorflow
pip install --upgrade https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.8.0-cp27-none-linux_x86_64.whl

Python 2.7.3 (default, Apr 26 2016, 11:18:30)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/python27/lib/python2.7/site-packages/tensorflow/__init__.py", line 23, in <module>
    from tensorflow.python import *
  File "/usr/local/python27/lib/python2.7/site-packages/tensorflow/python/__init__.py", line 45, in <module>
    from tensorflow.python import pywrap_tensorflow
  File "/usr/local/python27/lib/python2.7/site-packages/tensorflow/python/pywrap_tensorflow.py", line 28, in <module>
    _pywrap_tensorflow = swig_import_helper()
  File "/usr/local/python27/lib/python2.7/site-packages/tensorflow/python/pywrap_tensorflow.py", line 24, in swig_import_helper
    _mod = imp.load_module('_pywrap_tensorflow', fp, pathname, description)
ImportError: /lib64/libc.so.6: version `GLIBC_2.15' not found (required by /usr/local/python27/lib/python2.7/site-packages/tensorflow/python/_pywrap_tensorflow.so)


tensorflow0.80版本編譯的時候使用GLIBC_2.15,系統自帶的是GLIBC_2.12,所以報錯了。

安裝新版glibc

1.glibc下載
從http://www.gnu.org/software/libc/ 下載原始碼。我下載的版本是2.20,連結地址是http://ftp.gnu.org/gnu/glibc/glibc-2.20.tar.gz

2.安裝
因為glibc庫使用廣泛,為了避免汙染當前系統環境,最好自定義安裝目錄,使用時定義一下環境變數就行了。具體步驟如下:

------------------------------------------------------------------------------------------------

------------------------------------------------------------------------------------------------

網上使用這個方法(不修改原系統環境glibc,新建個目錄安裝呼叫新版glibc),但是我沒有成功,不要使用這個方法,參考後邊的安裝方法,因為$LD_LIBRARY_PATH沒有預設的內容
# tar xvf glibc-2.20.tar.gz
# cd glibc-2.20
[[email protected] glibc-2.20]# mkdir build
[[email protected]
glibc-2.20]# cd ./build
[[email protected] build]# ../configure --prefix=/opt/glibc-2.20
[[email protected] build]# make -j4
[[email protected] build]# make install

3.測試
[[email protected] nvEncodeApp]# export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/lib64:/opt/glibc-2.20/lib
[[email protected]
nvEncodeApp]# ./nvEncoder
export LD_LIBRARY_PATH=/usr/local/lib:/opt/glibc-2.20/lib:$LD_LIBRARY_PATH
-----------------------------------------------------------------------------------------------------

嚴重問題處理:
安裝過程中,因為修改/etc/ld.so.conf檔案,ldconfig後導致輸入命令後,報錯:
#ls
ls: error while loading shared libraries: __vdso_time: invalid mode for dlopen(): Invalid argument

可以使用的命令:
echo "include ld.so.conf.d/*.conf" > /etc/ld.so.conf

臨時解決可以使用ls、mv、cp、cat等命令的方法:
export LD_LIBRARY_PATH=/usr/lib:/usr/lib64:/lib:/lib64:/usr/local/lib:/usr/local/lib64:$LD_LIBRARY_PATH
修改/etc/ld.so.conf為原來的內容
執行ldconfig,恢復正常

(ldconfig 命令的用途,主要是在預設搜尋目錄(/lib和/usr/lib)以及動態庫配置檔案/etc/ld.so.conf內所列的目錄下,
搜尋出可共享的動態連結庫(格式如前介紹,lib*.so*),進而創建出動態裝入程式(ld.so)所需的連線和快取檔案.

快取檔案預設為/etc/ld.so.cache,此檔案儲存已排好序的動態連結庫名字列表.)

configure: error:
LD_LIBRARY_PATH shouldn't contain the current directory when building glibc. Please change the environment variable and run configure again.

很多命令不好用了
千萬不要斷開ssh,不然就遠端不上去了
vi /etc/profile 加入
export LD_LIBRARY_PATH=/usr/lib:/usr/lib64:/lib:/lib64:/usr/local/lib:/usr/local/lib64

# echo $LD_LIBRARY_PATH
繼續重新安裝glibc-2.17(參考下邊的安裝)

-----------------------------------------------------------------------------------------------------------

------------------------------------------------------------------------------------------------


我使用的方法(直接升級glibc):

yum install gcc

wget http://ftp.gnu.org/pub/gnu/glibc/glibc-2.17.tar.xz
xz -d glibc-2.17.tar.xz
tar -xvf glibc-2.17.tar
cd glibc-2.17
mkdir build
cd build
../configure --prefix=/usr --disable-profile --enable-add-ons --with-headers=/usr/include --with-binutils=/usr/bin  
make && make install
需要等大概10分鐘。

輸入strings /lib64/libc.so.6|grep GLIBC發現已經更新
GLIBC_2.2.5
GLIBC_2.2.6
GLIBC_2.3
GLIBC_2.3.2
GLIBC_2.3.3
GLIBC_2.3.4
GLIBC_2.4
GLIBC_2.5
GLIBC_2.6
GLIBC_2.7
GLIBC_2.8
GLIBC_2.9
GLIBC_2.10
GLIBC_2.11
GLIBC_2.12
GLIBC_2.13
GLIBC_2.14
GLIBC_2.15
GLIBC_2.16
GLIBC_2.17

GLIBC_PRIVATE

Python 2.7.3 (default, Apr 26 2016, 11:18:30)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/python27/lib/python2.7/site-packages/tensorflow/__init__.py", line 23, in <module>
    from tensorflow.python import *
  File "/usr/local/python27/lib/python2.7/site-packages/tensorflow/python/__init__.py", line 45, in <module>
    from tensorflow.python import pywrap_tensorflow
  File "/usr/local/python27/lib/python2.7/site-packages/tensorflow/python/pywrap_tensorflow.py", line 28, in <module>
    _pywrap_tensorflow = swig_import_helper()
  File "/usr/local/python27/lib/python2.7/site-packages/tensorflow/python/pywrap_tensorflow.py", line 24, in swig_import_helper
    _mod = imp.load_module('_pywrap_tensorflow', fp, pathname, description)
ImportError: /usr/lib64/libstdc++.so.6: version `GLIBCXX_3.4.14' not found (required by /usr/local/python27/lib/python2.7/site-packages/tensorflow/python/_pywrap_tensorflow.so)




ImportError: /usr/lib64/libstdc++.so.6: version `GLIBCXX_3.4.14' not found

#strings /usr/lib64/libstdc++.so.6 | grep GLIBCXX
GLIBCXX_3.4
GLIBCXX_3.4.1
GLIBCXX_3.4.2
GLIBCXX_3.4.3
GLIBCXX_3.4.4
GLIBCXX_3.4.5
GLIBCXX_3.4.6
GLIBCXX_3.4.7
GLIBCXX_3.4.8
GLIBCXX_3.4.9
GLIBCXX_3.4.10
GLIBCXX_3.4.11
GLIBCXX_3.4.12
GLIBCXX_3.4.13
GLIBCXX_FORCE_NEW
GLIBCXX_DEBUG_MESSAGE_LENGTH

沒有GLIBCXX_3.4.14版本支援,繼續安裝(注意:libstdc++6_4.7.2-5_amd64.deb這是64位,libstdc++6_4.7.2-5_i386.deb這個是32位)

下載新版本,地址:http://ftp.de.debian.org/debian/pool/main/g/gcc-4.7/libstdc++6_4.7.2-5_amd64.deb
ar -x libstdc++6_4.7.2-5_amd64.deb&&tar xvf data.tar.gz  

#cd /root/usr/lib/x86_64-linux-gnu (進入解壓檔案的目錄中,我這裡是下/root目錄下解壓的)
#ll
lrwxrwxrwx 1 root root     19 Apr 26 15:21 libstdc++.so.6 -> libstdc++.so.6.0.17
-rw-r--r-- 1 root root 991600 Jan  7  2013 libstdc++.so.6.0.17

# find / -name libstdc++.so.6
/usr/lib64/libstdc++.so.6
/root/usr/lib/x86_64-linux-gnu/libstdc++.so.6

#mv /usr/lib64/libstdc++.so.6 /usr/lib64/libstdc++.so.6.bak
#cp libstdc++.so.6.0.17 /usr/lib64/
#cd /usr/lib64/

#chmod +x libstdc++.so.6.0.17
#ll libstdc++.so.6.0.17
-rwxr-xr-x 1 root root 991600 Apr 26 15:30 libstdc++.so.6.0.17
#ln -s libstdc++.so.6.0.17 libstdc++.so.6

#strings /usr/lib64/libstdc++.so.6 | grep GLIBCXX
GLIBCXX_3.4
GLIBCXX_3.4.1
GLIBCXX_3.4.2
GLIBCXX_3.4.3
GLIBCXX_3.4.4
GLIBCXX_3.4.5
GLIBCXX_3.4.6
GLIBCXX_3.4.7
GLIBCXX_3.4.8
GLIBCXX_3.4.9
GLIBCXX_3.4.10
GLIBCXX_3.4.11
GLIBCXX_3.4.12
GLIBCXX_3.4.13
GLIBCXX_3.4.14
GLIBCXX_3.4.15
GLIBCXX_3.4.16
GLIBCXX_3.4.17
GLIBCXX_DEBUG_MESSAGE_LENGTH





>>> import tensorflow as tf
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/python27/lib/python2.7/site-packages/tensorflow/__init__.py", line 23, in <module>
    from tensorflow.python import *
  File "/usr/local/python27/lib/python2.7/site-packages/tensorflow/python/__init__.py", line 45, in <module>
    from tensorflow.python import pywrap_tensorflow
  File "/usr/local/python27/lib/python2.7/site-packages/tensorflow/python/pywrap_tensorflow.py", line 28, in <module>
    _pywrap_tensorflow = swig_import_helper()
  File "/usr/local/python27/lib/python2.7/site-packages/tensorflow/python/pywrap_tensorflow.py", line 24, in swig_import_helper
    _mod = imp.load_module('_pywrap_tensorflow', fp, pathname, description)
ImportError: /usr/lib64/libstdc++.so.6: version `GLIBCXX_3.4.19' not found (required by /usr/local/python27/lib/python2.7/site-packages/tensorflow/python/_pywrap_tensorflow.so)



從網上下載libstdc++.so.6.0.20

http://ftp.de.debian.org/debian/pool/main/g/gcc-4.8/
或者
http://download.csdn.net/detail/pomelover/7524227


放到/usr/lib64/下
#chmod +x libstdc++.so.6.0.20
#rm libstdc++.so.6
#ln -s libstdc++.so.6.0.20 libstdc++.so.6
#strings /usr/lib64/libstdc++.so.6 | grep GLIBCXX

[[email protected] lib64]# strings /usr/lib64/libstdc++.so.6 | grep GLIBCXX
GLIBCXX_3.4
GLIBCXX_3.4.1
GLIBCXX_3.4.2
GLIBCXX_3.4.3
GLIBCXX_3.4.4
GLIBCXX_3.4.5
GLIBCXX_3.4.6
GLIBCXX_3.4.7
GLIBCXX_3.4.8
GLIBCXX_3.4.9
GLIBCXX_3.4.10
GLIBCXX_3.4.11
GLIBCXX_3.4.12
GLIBCXX_3.4.13
GLIBCXX_3.4.14
GLIBCXX_3.4.15
GLIBCXX_3.4.16
GLIBCXX_3.4.17
GLIBCXX_3.4.18
GLIBCXX_3.4.19
GLIBCXX_3.4.20
GLIBCXX_FORCE_NEW
GLIBCXX_DEBUG_MESSAGE_LENGTH



終於成功了。
[[email protected] lib64]# python
Python 2.7.3 (default, Apr 26 2016, 11:18:30)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>> matrix1 = tf.constant([[3., 3.]])
>>> matrix2 = tf.constant([[2.],[2.]])
>>> product = tf.matmul(matrix1, matrix2)
>>> print product
Tensor("MatMul:0", shape=(1, 1), dtype=float32)
>>> sess = tf.Session()
>>> result = sess.run(product)
>>> print result
[[ 12.]]
>>> sess.close()
>>> exit()


以上內容記錄下,幫助需要的朋友。

參考:
http://stackoverflow.com/questions/7446187/no-module-named-pkg-resources
http://blog.chinaunix.net/uid-25691489-id-5577387.html
http://my.oschina.net/zhangxu0512/blog/262275
http://blog.csdn.net/bboxhe/article/details/46849167
http://stackoverflow.com/questions/33655731/error-while-importing-tensorflow-in-python2-7-in-ubuntu-12-04-glibc-2-17-not-f
http://blog.csdn.net/officercat/article/details/39520227
http://blog.chinaunix.net/uid-354915-id-3568853.html
http://blog.csdn.net/lib0129/article/details/23345791
http://blog.csdn.net/testcs_dn/article/details/45456815
http://blog.csdn.net/abcd1f2/article/details/49777715