Mac搭建TensorFlow环境踩过的那些坑

安装tensorflow时,输入Pip install tensorflow后
出现错误提示,其中红色提示文字:
matplotlib 1.3.1 requires nose,which is not installed.
matplotlib 1.3.1 requires tornado,,which is not installed.
matplotlib 1.3.1 has requirement numpy>=1.5, but you'll have numpy 1.8.0rc1 which is incompatible.

大致意思是 matplotlib的1.3.1版本需要nose、tornado,另外numpy的版本1.8.0rc1不匹配。

但是 使用pip uninstall numpy卸载numpy,不成功
使用pip install -U numpy也无法升级成功

出现错误提示:

Collecting numpy

Using cached numpy-1.10.2-cp27-none-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whlInstalling
 collected packages: numpy
 Found existing installation: numpy 1.8.0rc1
 DEPRECATION: Uninstalling a distutils installed project (numpy) has been deprecated and will be removed in a future version. This is due to the fact that uninstalling a distutils project will only partially uninstall the project.
 Uninstalling numpy-1.8.0rc1:Exception:Traceback
 (most recent call last):
 File "/Library/Python/2.7/site-packages/pip-7.1.2-py2.7.egg/pip/basecommand.py", line 211, in main
 status = self.run(options, args)
 File "/Library/Python/2.7/site-packages/pip-7.1.2-py2.7.egg/pip/commands/install.py", line 311, in run
 root=options.root_path,
 File "/Library/Python/2.7/site-packages/pip-7.1.2-py2.7.egg/pip/req/req_set.py", line 640, in install
 requirement.uninstall(auto_confirm=True)
 File "/Library/Python/2.7/site-packages/pip-7.1.2-py2.7.egg/pip/req/req_install.py", line 716, in uninstall
 paths_to_remove.remove(auto_confirm)
 File "/Library/Python/2.7/site-packages/pip-7.1.2-py2.7.egg/pip/req/req_uninstall.py", line 125, in remove
 renames(path, new_path)
 File "/Library/Python/2.7/site-packages/pip-7.1.2-py2.7.egg/pip/utils/__init__.py",
 line 315, in renames
shutil.move(old, new)
 File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 302, in move
 copy2(src, real_dst)
 File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 131, in copy2
 copystat(src, dst)
 File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 103, in copystat
 os.chflags(dst, st.st_flags)OSError:
 [Errno 1] Operation not permitted: '/var/folders/5n/vbm997m56xg3kw67y6bccn2m0000gn/T/pip-4tcBsd-uninstall/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy-1.8.0rc1-py2.7.egg-info'

sudo  pip install -U numpy仍然提示错误。

$ sudo pip install -U numpy  
The directory '/Users/a6/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.  
The directory '/Users/a6/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.  
Collecting numpy  
  Downloading numpy-1.13.3-cp27-cp27m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (4.6MB)  
    100% |████████████████████████████████| 4.6MB 198kB/s  
Installing collected packages: numpy  
  Found existing installation: numpy 1.8.0rc1  
    DEPRECATION: Uninstalling a distutils installed project (numpy) has been deprecated and will be removed in a future version. This is due to the fact that uninstalling a distutils project will only partially uninstall the project.  
    Uninstalling numpy-1.8.0rc1:  
Exception:  
Traceback (most recent call last):  
  File "/Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg/pip/basecommand.py", line 215, in main  
    status = self.run(options, args)  
  File "/Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg/pip/commands/install.py", line 342, in run  
    prefix=options.prefix_path,  
  File "/Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg/pip/req/req_set.py", line 778, in install  
    requirement.uninstall(auto_confirm=True)  
  File "/Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg/pip/req/req_install.py", line 754, in uninstall  
    paths_to_remove.remove(auto_confirm)  
  File "/Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg/pip/req/req_uninstall.py", line 115, in remove  
    renames(path, new_path)  
  File "/Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg/pip/utils/__init__.py", line 267, in renames  
    shutil.move(old, new)  
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 302, in move  
    copy2(src, real_dst)  
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 131, in copy2  
    copystat(src, dst)  
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 103, in copystat  
    os.chflags(dst, st.st_flags)  
OSError: [Errno 1] Operation not permitted: '/tmp/pip-QissHG-uninstall/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy-1.8.0rc1-py2.7.egg-info'  

网上查了半天,这个Operation not permitted ,才找到解决方案:

大部分资料说是用了新的Mac OS版本是10~11时,使用homebrew/pip ,都会经常出现访问文件的OSError,大概原因是新的mac os开启了SIP(System Integrity Protection),导致很多安装(需要修改一些系统目录、文件)都无法进行。解决的办法是关闭保护SIP。具体操作如下:
重启电脑,按住Command+R(直到出现苹果标志)进入Recovery Mode(恢复模式)
左上角菜单里找到实用工具 -> 终端
命令行中输入 csrutil disable回车
重启Mac即可
如果想重新启动SIP机制重复上述步骤,命令行输入csrutil enable即可
查询当前系统的SIP状态时,可在命令行直接输入csrutil status

电脑重启之后,再重新 执行sudo pip install -U numpy,按网上的教程都应该是正常升级numpy了,但我仍然失败。

于是采用了另一个博主的方法:

sudo pip install --ignore-installed numpy

操作后结果如下,numpy版本总算升级成功了。

$ sudo pip install --ignore-installed numpy  
The directory '/Users/a6/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.  
The directory '/Users/a6/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.  
Collecting scipy  
  Downloading scipy-1.0.0-cp27-cp27m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (16.8MB)  
    100% |████████████████████████████████| 16.8MB 41kB/s  
Collecting numpy>=1.8.2 (from scipy)  
  Downloading numpy-1.13.3-cp27-cp27m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (4.6MB)  
    100% |████████████████████████████████| 4.6MB 89kB/s  
Installing collected packages: numpy, scipy  
Successfully installed numpy-1.13.3 scipy-1.0.0  
localhost:Applications a6$ pip install --upgrade pip  
Requirement already up-to-date: pip in /Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg  
localhost:Applications a6$ sudo pip install -U numpy  
The directory '/Users/a6/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.  
The directory '/Users/a6/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.  
Requirement already up-to-date: numpy in /Library/Python/2.7/site-packages 

Numpy安装成功,按照后续步骤继续进行tensorflow安装。tensorflow安装和激活可参考这个文章:

https://blog.csdn.net/alice_tl/article/details/79807770


有两台mac,一台是公司机,一台是个人机。公司机的环境搭建非常顺利,上述故障一次没出现过,但个人机折腾了好几天晚上(因为只有下班回家才有时间折腾个人机)。本来以为个人机也环境ok了,于是按照如下方法进入tensorflow:
alicedeMacBook-Pro:~ alice$ cd tensorflow
alicedeMacBook-Pro:tensorflow alice$ source bin/activate
(tensorflow) alicedeMacBook-Pro:tensorflow alice$ python
Python 2.7.10 (default, Jul 15 2017, 17:16:57) 
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)] on darwin
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 "/Users/alice/tensorflow/lib/python2.7/site-packages/tensorflow/__init__.py", line 24, in <module>
    from tensorflow.python import *
  File "/Users/alice/tensorflow/lib/python2.7/site-packages/tensorflow/python/__init__.py", line 52, in <module>
    from tensorflow.core.framework.graph_pb2 import *
  File "/Users/alice/tensorflow/lib/python2.7/site-packages/tensorflow/core/framework/graph_pb2.py", line 6, in <module>
    from google.protobuf import descriptor as _descriptor
ImportError: No module named protobuf

提示:ImportError:No module named protobuf

还在继续排查原因中。

猜你喜欢

转载自blog.csdn.net/alice_tl/article/details/79982625