事件背景:卸载重新安装anaconda,发现以前很多配置方法都忘记了,虽然能在百度搜索到,但有必要记录一下,以备下次参考。

1 安装anaconda

为了研究需要,当前安装的版本是Anaconda3-5.1.0-Windows-x86_64.exe。anaconda的各种版本可以在下面网站中找到:

https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/

2 配置下载镜像

配置下载镜像的主要目的是为了安装一些包的时候比较快,毕竟连到国外的速度是比较慢的。
a 打开anaconda prompt命令行,输入
conda config --show channels
,可以查看当前已经设置的镜像,因为我的还没设置,所以显示是defaults。
b 添加镜像
conda config --add channels [urls…]
下面是清华的镜像:

https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/

下面是我的添加的镜像:
image.png
注意:这个在后面会报错
输入下面命令(把https改成http):

conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/

c 删除镜像命令

conda config--remove-key channels ##删除所有
conda config --remove channels <URL>  ## 删除原来的旧镜像

3 创建虚拟环境 nlp_ner

a 查看当前虚拟环境
输入命令:
conda env list
image.png
b 输入命令:
conda create -n nlp_ner python=3.7
结果在创建虚拟环境时遇到了一个错误:
image.png

(base) C:\Users\Administrator>conda create -n nlp_ner python=3.7
Collecting package metadata: failed

CondaHTTPError: HTTP 000 CONNECTION FAILED for url <https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/win-64/repodata.json>
Elapsed: -

An HTTP error occurred when trying to retrieve this URL.
HTTP errors are often intermittent, and a simple retry will get you on your way.
SSLError(MaxRetryError('HTTPSConnectionPool(host=\'mirrors.tuna.tsinghua.edu.cn\', port=443): Max retries exceeded with url: /anaconda/cloud/pytorch/win-64/repodata.json (Caused by SSLError(SSLError("bad handshake: Error([(\'SSL routines\', \'ssl3_get_server_certificate\', \'certificate verify failed\')],)",),))',),)

这是由于前面设置镜像有问题,需要把镜像网址的https改成http
方法:删除所有镜像配置,然后在添加。

conda config--remove-key channels
conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/

再次输入
conda create -n nlp_ner python=3.7
一路顺畅!
中间要输入一个yes,创建的虚拟环境路径在:C:\ProgramData\Anaconda3\envs中。
image.png

4 安装TensorFlow1.15.0

a 进入虚拟环境
输入命令:
conda activate nlp_ner
image.png
注意前面括号中的名字,就是你的虚拟环境名字
b 安装tensorflow 1.15.0
在网上查到,安装tensorflow 1.15.0,可选择下载CPU版本或者GPU版本

conda install tensorflow-cpu==1.15.0
conda install tensorflow-gpu==1.15.0

因为我笔记本电脑显卡不行,于是就用的第一个命令。
结果又报错了,意思是说现在的镜像里面没有找到这个包,要我找另外的镜像替换一下。我记得没卸载之前用的是阿里的镜像,装这个包是没有问题的,换回阿里的镜像试一下看:
image.png

Solving environment: failed

PackagesNotFoundError: The following packages are not available from current channels:

  - tensorflow-cpu==1.15.0

Current channels:

  - http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/win-64
  - http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/noarch
  - http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/win-64
  - http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/noarch
  - https://repo.anaconda.com/pkgs/main/win-64
  - https://repo.anaconda.com/pkgs/main/noarch
  - https://repo.anaconda.com/pkgs/free/win-64
  - https://repo.anaconda.com/pkgs/free/noarch
  - https://repo.anaconda.com/pkgs/r/win-64
  - https://repo.anaconda.com/pkgs/r/noarch
  - https://repo.anaconda.com/pkgs/msys2/win-64
  - https://repo.anaconda.com/pkgs/msys2/noarch

To search for alternate channels that may provide the conda package you're
looking for, navigate to

    https://anaconda.org

and use the search bar at the top of the page.

后来,更换了阿里的镜像,发现还是这个错误。所以错误的根源应该不是镜像,后来在查资料,很多人直接用的这个命令:
conda install tensorflow==1.15.0
根本不管什么cpu还是gpu版本,然后就可以了
还有一个命令,是查看当前的TensorFlow可以安装的版本的,如下:
conda search --full --name tensorflow
安装过程有点久,需要输入一个yes,耐心等待。

c 查看已安装TensorFlow的版本
输入命令:

import tensorflow as tf
tf.__version__

下面是结果,显示的是1.15.0的版本 ,安装成功:
image.png

5 修改anaconda的默认存储地址

启动Jupyter notebook后,发现默认存储地址是在c盘的C:\Users\Administrator下面
image.png
我原来的文件放在D:\anaconda_workspace目录下,于是需要更改。
a 打开Anaconda Prompt

输入jupyter notebook --generate-config生成配置文件~\.jupyter\jupyter_notebook_config.py

b 修改配置文件
在文件夹C:\Users\Administrator.jupyter打开jupyter_notebook_config.py

修改 #c.NotebookApp.notebook_dir = ’ ’ 为下一行

c.NotebookApp.notebook_dir =‘D:\anaconda_workspace’

注意去掉注释#,完成路径的修改

c Jupyter notebook属性修改
从这里进去
image.png
进去后,
右键Jupyter notebook,点击属性,去掉目标一栏中的%USERPROFILE%即可。

d 打开Jupyter notebook
重新打开Jupyter notebook时,命令行界面闪退了,没有看到错误代码。
于是打开anaconda prompt,用命令的方式,输入jupyter notebook来启动Jupyter notebook,这时候可以看到错误
image.png
路径分隔符乱码了,于是重新打开~.jupyter\jupyter_notebook_config.py配置文件,用双斜杠替代反斜杠:

c.NotebookApp.notebook_dir = 'D://anaconda_workspace'

image.png
再启动,就没有问题了,熟悉的内容又回来了。
image.png

6 虚拟环境安装jupyter notebook

进入anaconda prompt ,进入虚拟目录,

(base) C:\Users\Administrator>activate nlp_ner
(nlp_ner) C:\Users\Administrator>conda list

在我们刚刚创建的虚拟环境下,安装ipykernel库,利用ipykernel库将该虚拟环境写入到jupyter中。
首先,在当前环境下安装ipykernel库,输入:

conda install ipykernel

然后,在当前环境下将该环境写入jupyter,输入如下:
注意:name后跟当前环境名

# 将虚拟环境添加到jupyter中,其中第二个“nlp_ner”为虚拟环境在jupyter中显示的名称,与虚拟环境名称可不一致,可自行设置
python -m ipykernel install --user --name nlp_ner --display-name nlp_ner

# 查看jupyter是否已添加虚拟环境
jupyter kernelspec list
 
# 删除相应的虚拟环境
jupyter kernelspec uninstall nlp_ner

image.png
返回base,输入jupyter notebook启动,可以看到已经添加到kernel中
image.png

7 总结

至此,就可以把项目放在D:\anaconda_workspace目录中,然后在kernel中选择合适的内核去运行项目。

由于以前跑这些项目的时候是去年11月份,当时没有留下整个过程的完整配置文档,结果今年又要跑项目的时候,发现很多配置方面的东西全部忘记了。网上虽然能找到资料解决问题,但有些资料似是而非,由此浪费了很多时间。所以,今后的每一步项目,都要形成文档记录,待下次有经验可循,而且不必重新去找资料而浪费太多时间。


1 声望0 粉丝

引用和评论

0 条评论