开始Anaconda安装

Anaconda是一个安装、管理python相关包的软件,还自带python、Jupyter Notebook、Spyder,有管理包的conda工具,非常有用。
1:安装:https://repo.anaconda.com/archive
下载 Anaconda3-5.2.0-Windows-x86_64.exe 版本,5.3以后的版本包含的python版本都在3.7及以上。 

安装完成后需要设置Anaconda环境变量:
安装目录:D:\ProgramData\Anaconda3
设置以下三个环境变量
D:\ProgramData\Anaconda3
D:\ProgramData\Anaconda3\Scripts
D:\ProgramData\Anaconda3\Library\bin 

一:创建虚拟环境

D:\workfolder\git-source\anaconda>conda create -n test
Collecting package metadata (current_repodata.json): done
Solving environment: done

==> WARNING: A newer version of conda exists. <==
  current version: 4.8.3
  latest version: 4.11.0

Please update conda by running
    $ conda update -n base -c defaults conda
## Package Plan ##
  environment location: D:\ProgramData\Anaconda3\envs\test

新创建的环境在安装目录的envs中,查询一下当前已经创建好的的虚拟环境

D:\ProgramData\Anaconda3\envs\test>conda env list
# conda environments:
#
base                  *  D:\ProgramData\Anaconda3
test                     D:\ProgramData\Anaconda3\envs\test 

1.1:如果出现错误: CondaHTTPError: HTTP 000 CONNECTION FAILED for url <https://repo.anaconda.com/pkgs/main/win-64/curre
解决方法:

 把路径“Anaconda3/Library/bin ”下面的两个文件:libcrypto-1_1-x64.dll ;libssl-1_1-x64.dll,复制到路径“Anaconda3/DLLs”下。

1.2: 添加清华源

打开命令窗口,依次执行以下命令:【注意添加完整,不然会有部分不是镜像源】

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

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

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/

#设置搜索时显示通道地址
conda config --set show_channel_urls yes

执行完成会在用户目录下生成一个.condarc文件, 

.condarc文件内容如下,和我们设置的一致

channels:
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
  - defaults
show_channel_urls: true
ssl_verify: false

创建 conda 环境成功以后界面会显示:

 

1.3 操作命令

激活Anacond

C:\Users\Administrator>activate 

显示当前所有的虚拟环境,默认进入base环境 

(base) C:\Users\Administrator>conda env list
# conda environments:
#
base * D:\ProgramData\Anaconda3
myenv D:\ProgramData\Anaconda3\envs\myenv
test D:\ProgramData\Anaconda3\envs\test
myenv d:\ProgramData\Anaconda3\envs\myenv

进入test环境

(base) C:\Users\Administrator>conda activate test 

或者直接命令:

C:\Users\Administrator>activate myenv
(myenv) C:\Users\Administrator>

显示当前环境下安装的组件

(test) C:\Users\Administrator>conda list
# packages in environment at D:\ProgramData\Anaconda3\envs\test:
#
# Name Version Build Channel
appdirs 1.4.4 pyh9f0ad1d_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
bzip2 1.0.8 h8ffe710_4 https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
ca-certificates 2021.10.8 h5b45459_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
libffi 3.4.2 h8ffe710_5 https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
libzlib 1.2.11 h8ffe710_1013 https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
openssl 3.0.0 h8ffe710_2 https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
pip 21.3.1 pyhd8ed1ab_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
python 3.10.1 hcf16a7b_2_cpython https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
python_abi 3.10 2_cp310 https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
setuptools 60.1.1 py310h5588dad_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
sqlite 3.37.0 h8ffe710_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
tk 8.6.11 h8ffe710_1 https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
tzdata 2021e he74cb21_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
ucrt 10.0.20348.0 h57928b3_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
vc 14.2 hb210afc_5 https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
vs2015_runtime 14.29.30037 h902a5da_5 https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
wheel 0.37.1 pyhd8ed1ab_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
xz 5.2.5 h62dcd97_1 https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge

返回base环境

(test) C:\Users\Administrator>conda activate 

删除test环境

(base) C:\Users\Administrator>conda remove -n test --all

 在虚拟环境中安装组件:也可以不指定版本。

(myenv) C:\Users\Administrator>conda install appdirs==1.4.3

 安装组件时可以选着一个镜像:

pip install seaborn -i https://pypi.tuna.tsinghua.edu.cn/simple

 

conda常用的命令 

#查看指定虚拟环境下的package
conda list --name [虚拟环境名]   

#创建
conda create --name [虚拟环境名] [python的版本] [需要的包]

例如:
conda create --name python3.8
conda create --name python3.8 python=3.8
conda create --name python3.8 python=3.8 numpy scipy 

#克隆
conda create --name [虚拟环境名] -- clone [colne的环境]

例如:创建一个和原python环境一样的虚拟环境
conda create --name mybase --clone base

#删除
conda remove --name [虚拟环境名] --all

#清除所有缓存的包文件
pip cache purge

#激活取消(默认的环境是base)
activate [虚拟环境名]
deactivate [虚拟环境名]

 

Linux下安装conda

Linux下安装好进行全局初始化后,启动终端就会自动进入conda的base环境,可以使用以下命令取消。

conda config --set auto_activate_base false

 

 

安装Tensorflow

注意事项:根据Tensorflow的官方文档,可以得到安装tensorflow的一个命令是下面:

pip install --ignore-installed --upgrade https://storage.googleapis.com/tensorflow/windows/cpu/tensorflow-1.0.0-cp35-cp35m-win_x86_64.whl

但是,如果我们在cmd中,直接进行这样的话,有可能是不能够成功的,开始也不知道为什么,后面发现是跟电脑的cpu和显卡有点关系,所以,采取后面的方法进行安装;

通过命令:pip install --upgrade --ignore-installed tensorflow
剩下的就是慢慢的等待安装的过程啦

 

3: 打开pycharm , 打开菜单 File -> Settings -> Python interpreter 

  选择 “Show All...”

  添加左下角的 加号

 tensorflow安装:https://blog.csdn.net/FriendshipTang/article/details/113573114

 选着刚刚生成的 tensorflow目录中的python.exe

 在pycharm中添加一个tensorflowdemo.py文件,内容如下 

import tensorflow as tf
tf.compat.v1.disable_eager_execution() # 保证sess.run()能够正常运行
hello = tf.constant('hello,tensorf') 
sess = tf.compat.v1.Session() 
print(sess.run(hello)) 
print(tf.compat.v1.VERSION) 

运行结果如下:

b'hello,tensorf'
2.3.0
2021-12-13 00:12:18.510654: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN)to use the following CPU instructions in performance-critical operations:  AVX AVX2
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.

这里的输出结果是一个字节字符串。要删除字符串引号和“b”(表示字节,byte)只保留单引号内的内容,可以使用 decode() 方法。  

输出的最下面有一行是一个tensorflow的warning而已,在py中加入红色的代码,就不会输出

import tensorflow as tf
import os

os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
# 以上代码用于忽略级别 2 及以下的消息(级别 1 是提示,级别 2 是警告,级别 3 是错误)。
tf.compat.v1.disable_eager_execution() # 保证sess.run()能够正常运行
message = tf.constant('Welcome to the exciting world of Deep Neural Networks!')

with tf.compat.v1.Session() as sess:
    print(sess.run(message).decode())

print(tf.compat.v1.VERSION)

with作用 : 每个会话都需要使用 close() 来明确关闭,而 with 格式可以在运行结束时隐式关闭会话  

 

TensorFlow学习:https://zhuanlan.zhihu.com/p/528990671

TensorFlow学习: https://tensorflow.google.cn/tutorials/keras/classification?hl=zh-cn

 

posted @ 2021-12-12 23:08  南极山  阅读(144)  评论(0)    收藏  举报