一、安装速度慢
解决这个问题很简单,因为pip内置的镜像网站是国外的,所以下载起来很慢,我们可以使用国内的镜像网站。
清华:https://pypi.tuna.tsinghua.edu.cn/simple
阿里云:http://mirrors.aliyun.com/pypi/simple/
中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/
华中理工大学:http://pypi.hustunique.com/
山东理工大学:http://pypi.sdutlinux.org/
豆瓣:http://pypi.douban.com/simple/
实测,豆瓣速度最快。
使用方法
可以在使用pip的时候加参数-i
https://pypi.tuna.tsinghua.edu.cn/simple
例如:pip install -i https://pypi.tuna.tsinghua.edu.cn/simple
pyspider,这样就会从清华这边的镜像去安装pyspider库。
报错
出现如下错误
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.
解决方案
输入 pip install -U --ignore-installed wrapt enum34 simplejson netaddr
最后使用
pip -install tensorflow -i https://pypi.tuna.tsinghua.edu.cn/simple
即可安装成功
安装成功,但是import却报错
通常是因为tensorflow依赖的组件在你当前的环境下其版本太高或者太低,使用如下命令,即可解决
pip install h5py==2.8.0rc1 -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install -U numpy==1.16.4 -i https://pypi.tuna.tsinghua.edu.cn/simple
推荐方法
因为tensorflow对python版本有要求,如果我们当前用的是3.7的话,可能你还要把Anaconda卸载掉然后去下载个老版本,但是完全没必要,我们可以使用Anaconda来创建一个虚拟环境。感觉很实用,记录一下首先使用以下命令,在Anaconda中创建一个虚拟环境,其中python=3.6是指该环境中,python版本,你也可以指定成2.x,那么就是conda的名字了
conda create -n name python=3.6
创建完成之后,我们就直接在这个环境中搭建tensorflow,使用如下命令
pip install tensorflow==2.0.0 -i https://pypi.tuna.tsinghua.edu.cn/simple
所有东西都下载完成之后,如果不出意外应该就可以使用了。
但是现在还有两个问题
我们在pycharm中怎么切换到这个虚拟环境中进行工作?
我们怎么把Jupyter notebook中的工作环境切换到刚刚创建的tensorflow的环境中
我们必须为Jupyter Notebook创建一个kernel,才可以进行环境的切换,创建方式如下
首先使用命令,进入刚刚创建的conda环境中
activate tensorflow //注意,这里的tensorflow是指你创建的环境名称
然后为这个环境安装支持文件
conda install ipykernel
完成之后,就可以为jupyter notebook 添加核了,使用如下命令即可
python -m ipykernel install --name tensorflow //注意,这里的tensorflow就是指的你刚刚创建的环境名称
然后我们打开Jupyter notebook,进入到文件中
鼠标移动到Kernel,即可进行切换
关于GPU版本的安装
网络有很多,我按照这个帖子一步一步的来,一下子就安装成功了