MACOS BREW安装指定版本软件包 安装python3.6.5并使用pip安装tensorflow

一、系统环境

MACOS 10.12.6 Sierra

python3.6.5

tensorflow1.12.0

二、背景

tensorflow官方显示【2018.12.02 https://tensorflow.google.cn/install/pip】,目前只支持到python3.6。

但是brew安装python默认安装的是最新版,即使用brew install python命令安装的是3.7.1版本。如果MACOS安装了3.7.1版本的python,那么在使用pip3命令安装tensorflow时会报错:

(tensorflow) ➜  ~ pip3 install tensorflow
Collecting tensorflow
  Could not find a version that satisfies the requirement tensorflow (from versions: )
No matching distribution found for tensorflow
(tensorflow) ➜  ~ 

三、解决方法:指定使用brew安装python时的版本

1、第一步:关闭brew的自动更新,不然即使下面环节指定了3.6.5版本,也会在开始安装时进行自动更新,继而安装成3.7.1版本。

export HOMEBREW_NO_AUTO_UPDATE=true

2、第二步:根据python.rb文件查找到3.6.5版本对应的commit的ID。

我这里是先手动更新一次brew,执行命令:brew update

查找python.rb的文件位置

zzhu:~ root# find / -name python.rb

/usr/local/Cellar/python/3.6.5_1/.brew/python.rb

/usr/local/Homebrew/Library/Homebrew/language/python.rb

/usr/local/Homebrew/Library/Taps/homebrew/homebrew-core/Formula/python.rb

zzhu:~ root#

然后打开/usr/local/Homebrew/Library/Taps/homebrew/homebrew-core/Formula/python.rb

➜  Formula git:(master)

➜  Formula git:(master) git log python.rb

➜  Formula git:(master)

找到3.6.5_1版本对应的commit ID是f2a764ef94

3、第三步

执行git checkout f2a764ef94切换到3.6.5._1版本

➜  Formula git:(master)

➜  Formula git:(master) git checkout f2a764ef94

Note: checking out 'f2a764ef94'.

You are in 'detached HEAD' state. You can look around, make experimental

changes and commit them, and you can discard any commits you make in this

state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may

do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b <new-branch-name>

HEAD is now at f2a764ef94... python: update 3.6.5_1 bottle.

➜  Formula git:(f2a764ef94)

➜  Formula git:(f2a764ef94)

4、第四步

执行brew install python即可正常安装python3.6.5

安装完成后将git repo还原

➜  Formula git:(f2a764ef94) git checkout master

Previous HEAD position was f2a764ef94... python: update 3.6.5_1 bottle.

Switched to branch 'master'

Your branch is behind 'origin/master' by 12 commits, and can be fast-forwarded.

  (use "git pull" to update your local branch)

➜  Formula git:(master)

四、解决python版本问题后,即可正常使用pip安装tensorflow

我们这里将tensorflow安装在一个虚拟环境中,通过virtualenv来进行管理(如果直接安装在本机系统中,可能与其他应用存在冲突,建议采用虚拟环境安装)

1、安装virtualenv

pip3 install virtualenv

cd ~

mkdir envs

2、创建一个名为tensorflow的虚拟环境,保存在~/envs/tensorflow文件夹中。

virtualenv ~/envs/tensorflow

3、激活这个虚拟环境

source ~/envs/tensorflow/bin/activate

4、安装tensorflow

pip install --upgrade tensorflow

五、执行第一个tensorflow程序

1、程序内容

cat hello_world.py

import tensorflow as tf

h = tf.constant("Hello")

w = tf.constant(" World!")

hw = h + w

with tf.Session() as sess:

    ans = sess.run(hw)

print(ans)

2、在激活的虚拟环境中执行

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值