Jupyter notebook 中用 pip 安装 tensorflow

环境说明

Mac + virtualenv + jupyterNotebook

查看版本

$ python3 --version
$ pip3 --version
$ virtualenv --version

创建环境

在项目主目录下创建环境在venv目录,并激活环境:

$ virtualenv --system-site-packages -p python3 ./venv
$ source ./venv/bin/activate # sh, bash, ksh, or zsh

如果是在fish下运行以上的source命令,末尾加上.fish,如:source ./env/bin/acticate.fish

退出环境命令:

(venv) $ deactivate

前面的(venv)表示当前的命令行在venv环境下。

打开 Jupyter nootbook:

(venv) $ jupyter notebook

这时候浏览器会进入localhost:8888,在 jupyter notebook 中输入import tensorflow as tf,运行后报错:ModuleNotFoundError: No module named 'tensorflow'
现在我们在 jupyter notebook 内新增一个input代码块(快捷键A)输入:

!pip3 list

在 Jupyter notebook 代码块中的代码前面加!表示输入的是命令。

我们会看到pip3命令的可安装包列表,如果里面没有tensorflow我们就需要更新一下本地pip3安装包库,新增代码块并输入:

!pip3 install -U -i https://pypi.tuna.tsinghua.edu.cn/simple/ tensorflow

其中https://pypi.tuna.tsinghua.edu.cn/simple/表示清华源,因为使用pip3原装的国外源太慢了。
这时候可以看到下载速度飞快,眼看就要成功了,但要注意的是这时候可能会跳出报错:

ERROR: Cannot uninstall 'wrapt'. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall.

碰到这样的问题我们就执行以下命令:

!pip3 install -U -i https://pypi.tuna.tsinghua.edu.cn/simple/ --ignore-installed wrapt enum34 simplejson netaddr

可能还会遇到一个问题:

ERROR: tensorboard 1.14.0 has requirement setuptools>=41.0.0, but you'll have setuptools 39.1.0 which is incompatible.

原因:setuptools版本太低
办法:更新setuptools版本 输入!pip install --upgrade setuptools(记得用国内源)
现在我们就可以顺利的装 Tensorflow 了,再输入一遍从清华pip3源获取tensorflow包的命令:

!pip3 install -U -i https://pypi.tuna.tsinghua.edu.cn/simple/ tensorflow

检查下!pip3 list可以发现安装包已到手,现在我们执行安装命令:

!pip3 install tensorflow

愉快的安装成功!,不过如果以前电脑里装了百度的库paddlepaddle可能会在这有一条报错,不碍安装成功的事:

ERROR: paddlepaddle 1.3.2 has requirement requests==2.9.2, but you'll have requests 2.23.0 which is incompatible.

看起来没问题了,可以说是装好了。现在运行一下import tensorflow as tf运行成功,但可能会有一个小小的提示:

.../python3.6/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
  from ._conv import register_converters as _register_converters

这不妨碍我们,不过想要解决根本问题,那就执行两条命令:

!pip3 install -U -i https://pypi.tuna.tsinghua.edu.cn/simple/ h5py
!pip3 install h5py

现在再来看import tensorflow as tf已经没问题了!
附上代码:

import matplotlib as mpl
import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
import sklearn
import pandas as pd
import os
import sys
import time
import tensorflow as tf

from tensorflow import keras
print(tf.__version__)
print(sys.version_info)
for module in mpl, np, pd, sklearn, tf, keras:
    print(module.__name__, module.__version__)

输出:

2.1.0
sys.version_info(major=3, minor=6, micro=4, releaselevel='final', serial=0)
matplotlib 2.2.3
numpy 1.18.1
pandas 0.22.0
sklearn 0.19.1
tensorflow 2.1.0
tensorflow_core.python.keras.api._v2.keras 2.2.4-tf

Jupyter kernel error 解决方法

在 Anaconda 命令行环境中运行!jupyter kernelspec list命令查看 kernel 的位置,再进入安装的 kernel 目录,打开 kernel.json 文件,查看 Python 解释器的路径是否正确。

pip3

我最不喜欢的就是 pippip3 两个 python 版本的 pip 实在是让人不舒服,我只关心 pip3
还是看看国内的「pip镜像」吧:

更换 tensorflow 版本

卸载原有 tensorflow 版本:

!pip3 uninstall tensorflow --yes

安装指定 tensorflow 版本

!pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple/ tensorflow==1.13.1
本作品采用《CC 协议》,转载必须注明作者和本文链接
不要试图用百米冲刺的方法完成马拉松比赛。
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!