为什么python3.4试图在RedHat7上加载python2.7 TensorFlow包?

时间:2016-07-28 18:30:22

标签: python python-2.7 python-3.x tensorflow

我的RedHat7机器上安装了python2.7和python3.4。我为这两个版本安装了tensorflow

P2.7似乎工作正常:

Python 2.7.5 (default, Oct 11 2015, 17:47:16) 
[GCC 4.8.3 20140911 (Red Hat 4.8.3-9)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import site; site.getsitepackages()
['/usr/lib64/python2.7/site-packages', '/usr/lib/python2.7/site-packages', '/usr/lib/site-python']
>>> import tensorflow
>>> 

对于p3.4我得到了:

Python 3.4.3 (default, Jan 26 2016, 02:25:35) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import site; site.getsitepackages()
['/usr/lib64/python3.4/site-packages', '/usr/lib/python3.4/site-packages', '/usr/lib/site-python']
>>> import tensorflow
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/site-packages/tensorflow/__init__.py", line 23, in <module>
    from tensorflow.python import *
  File "/usr/lib/python2.7/site-packages/tensorflow/python/__init__.py", line 48, in <module>
    from tensorflow.python import pywrap_tensorflow
  File "/usr/lib/python2.7/site-packages/tensorflow/python/pywrap_tensorflow.py", line 28, in <module>
    _pywrap_tensorflow = swig_import_helper()
  File "/usr/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)
  File "/usr/lib64/python3.4/imp.py", line 243, in load_module
    return load_dynamic(name, filename, file)
ImportError: /usr/lib/python2.7/site-packages/tensorflow/python/_pywrap_tensorflow.so: undefined symbol: PyClass_Type
>>> 

为什么p3试图抓住p2包?

更新#1:

  • sys.path制作了['', '/usr/lib64/python34.zip', '/usr/lib64/python3.4', '/usr/lib64/python3.4/plat-linux', '/usr/lib64/python3.4/lib-dynload', '/usr/lib64/python3.4/site-packages', '/usr/lib/python3.4/site-packages']
  • PYTHONPATH未定义

更新#2 :正如Martijn建议我跑

python3 -v -c 'import tensorflow' > ~/log.txt 2>&1

并看到以下块

...
import 'types' # <_frozen_importlib.SourceFileLoader object at 0x7fd5b3e7e8d0>
import 'site' # <_frozen_importlib.SourceFileLoader object at 0x7fd5b3f4bba8>
# code object from /usr/lib/python2.7/site-packages/tensorflow/__init__.py
# could not create '/usr/lib/python2.7/site-packages/tensorflow/__pycache__/__init__.cpython-34.pyc': PermissionError(13, 'Permission denied')
# wrote '/usr/lib/python2.7/site-packages/tensorflow/__pycache__/__init__.cpython-34.pyc'
# /usr/lib64/python3.4/__pycache__/__future__.cpython-34.pyc matches /usr/lib64/python3.4/__future__.py
# code object from '/usr/lib64/python3.4/__pycache__/__future__.cpython-34.pyc'
import '__future__' # <_frozen_importlib.SourceFileLoader object at 0x7fd5b3ea04e0>
# code object from /usr/lib/python2.7/site-packages/tensorflow/python/__init__.py
# could not create '/usr/lib/python2.7/site-packages/tensorflow/python/__pycache__/__init__.cpython-34.pyc': PermissionError(13, 'Permission denied')
# wrote '/usr/lib/python2.7/site-packages/tensorflow/python/__pycache__/__init__.cpython-34.pyc'
...

我运行了以下命令来消除这些错误:

sudo chmod -R ugo+rX /usr/lib/python2.7/site-packages/tensorflow/__pycache__
sudo chmod -R ugo+rX /usr/lib/python2.7/site-packages/tensorflow/python/__pycache__/

但我仍然得到以下内容:

import 'imp' # <_frozen_importlib.SourceFileLoader object at 0x7fb372a1f320>
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
  File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 1129, in _exec
  File "<frozen importlib._bootstrap>", line 1471, in exec_module
  File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
  File "/usr/lib/python2.7/site-packages/tensorflow/__init__.py", line 23, in <module>
    from tensorflow.python import *
  File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
  File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 1129, in _exec
  File "<frozen importlib._bootstrap>", line 1471, in exec_module
  File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
  File "/usr/lib/python2.7/site-packages/tensorflow/python/__init__.py", line 48, in <module>
    from tensorflow.python import pywrap_tensorflow
  File "<frozen importlib._bootstrap>", line 2284, in _handle_fromlist
  File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
  File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 1129, in _exec
  File "<frozen importlib._bootstrap>", line 1471, in exec_module
  File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
  File "/usr/lib/python2.7/site-packages/tensorflow/python/pywrap_tensorflow.py", line 28, in <module>
    _pywrap_tensorflow = swig_import_helper()
  File "/usr/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)
  File "/usr/lib64/python3.4/imp.py", line 243, in load_module
    return load_dynamic(name, filename, file)
ImportError: /usr/lib/python2.7/site-packages/tensorflow/python/_pywrap_tensorflow.so: undefined symbol: PyClass_Type
# clear builtins._
# clear sys.path

更新#3:我要求我的系统管理员运行以安装TF for p3.4的命令是直接从TF安装页面提取的:

# Ubuntu/Linux 64-bit, CPU only, Python 3.4
export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.9.0-cp34-cp34m-linux_x86_64.whl
sudo pip3 install --upgrade $TF_BINARY_URL

pip3是否可能以某种方式失控并指向p2.7而不是p3.4?也就是说,我认为TF安装的一部分是安装numpy,并且在将其导入p3.4时没有出现任何错误,因此可能还有其他事情发生。

更新#4:我已经问过我们的系统管理员(最初为我安装了TF)在他身边运行python3 -v -c 'import tensorflow'命令。他的结果与我的不同:python3拉出正确的库,TF加载很好。这是我日志的摘录:

# /usr/lib/python2.7/site-packages/tensorflow/__pycache__/__init__.cpython-34.pyc matches /usr/lib/python2.7/site-packages/tensorflow/__init__.py
# code object from '/usr/lib/python2.7/site-packages/tensorflow/__pycache__/__init__.cpython-34.pyc'
# /usr/lib/python2.7/site-packages/tensorflow/python/__pycache__/__init__.cpython-34.pyc matches /usr/lib/python2.7/site-packages/tensorflow/python/__init__.py
# code object from '/usr/lib/python2.7/site-packages/tensorflow/python/__pycache__/__init__.cpython-34.pyc'

这是来自他的:

# /usr/lib/python3.4/site-packages/tensorflow/__pycache__/__init__.cpython-34.pyc matches /usr/lib/python3.4/site-packages/tensorflow/__init__.py
# code object from '/usr/lib/python3.4/site-packages/tensorflow/__pycache__/__init__.cpython-34.pyc'
# /usr/lib/python3.4/site-packages/tensorflow/python/__pycache__/__init__.cpython-34.pyc matches /usr/lib/python3.4/site-packages/tensorflow/python/__init__.py
# code object from '/usr/lib/python3.4/site-packages/tensorflow/python/__pycache__/__init__.cpython-34.pyc'

PYTHONPATH既不是为他定义,也不是为我定义。那么为什么python对我们的工作方式不同呢?

0 个答案:

没有答案