Mac上pycharm找不到tensorflow这个模块
【解决办法】
我指的彻底的解决办法。
参考的相关链接可以直接点击进去查看。
解决问题的关键是配置Pycharm的interpreter时要在virtualenv中进行设置。并且最好是在项目最开始新建的时候。
1,卸载tensorflow(比如在终端输入sudo pip uninstall tensorflow ?), anaconda3
2,删除所有anaconda3文件夹
【反正上面两部就是把你之前的尝试都清理干净】
3,删除所有包含python3.x的文件夹(mac自带的是python2.7不用删)
4.1 打开终端terminal
4.2 安装xcode:输入 xcode-select --install
4.3 安装homebrew:输入ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
4.4 通过brew安装python3:brew install python3
4.5 安装完成后,在终端输入python3能够正确打开。显示如下:
$ python3
Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 05:52:31)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
表示python3安装完成。看一下python的安装位置:
$ which python3
我的显示:/Library/Frameworks/Python.framework/Versions/3.6/bin/python3
4,更新一下pip和six,做好安装tensorflow之前的准备:
$ sudo easy_install --upgrade pip
$ sudo easy_install --upgrade six
$ pip install tensorflow # Python 2.7; CPU support
$ pip3 install tensorflow # Python 3.n; CPU support
打开终端,输入:
$ python3
Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 05:52:31)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
如果这一步没有报错,说明安装成功。
7,继续看一下tensorflow的安装位置,做到心里有数
$ python3
Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 05:52:31)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>> tf.__path__
我的显示:
['/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow', '/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/estimator/api', '/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/_api/v1']
8,打开pycharm,新建一个project
file -> newproject -> 在命名处输入文件命名
展开下方 project interpreter选项,选择new environment using 。。。, 注意选择virtualenv,并勾下面两个选项,location和baseinterpreter都选为上面python 3.6的安装路径。点击create
9,此时创建了一个项目,在项目名称上右键,选择new -> python文件 -> 输入下面的测试代码:
import tensorflow as tf
hello = tf.constant('Hello, Tensorflow!')
sess = tf.Session()
print(sess.run(hello))
a = tf.constant(66)
b = tf.constant(88)
print(sess.run(a + b))
结果显示以下内容表示pycharm导入tensorflow成功:
2019-01-16 21:58:49.790498: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
b'Hello, Tensorflow!'
154
Process finished with exit code 0