Python3.8安装可以参考博文https://janus.blog.csdn.net/article/details/55274849 进行安装即可。

【1】Anaconda

清华的开源软件镜像站:https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/下载,这里选择的是5.3.1版本。

然后正常安装就可以,安装路径这里选择的是E:\softinstall\Anaconda 。

安装后在Windows的开始菜单找到Anaconda Prompt并打开可以检测版本与环境信息。

base) C:\Users\Janus>conda --version

conda 4.5.11

(base) C:\Users\Janus>conda info --envs

# conda environments:

#

base * E:\softinstall\Anaconda

也可以选择从官网下载最新版本,但是可能比较慢:https://www.anaconda.com/download/

这个官网下载是真的慢,花了蛮久下载了Anaconda3-2023.09-0-Windows-x86_64.exe,下面是百度云链接:

链接:https://pan.baidu.com/s/1IIyl5qVjCcUNrKzN8bZVvg?pwd=yyds

提取码:yyds

【2】TensorFlow2.13.0CPU版本

TensorFlow 2.13.0版本需要Python版本: Requires: Python >=3.8

① 切换源

因为国外的网站下载速度很慢,所以先把anaconda的源换成清华镜像:

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

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

conda config --set show_channel_urls yes

注意哦,这里是http且最后加了win-64。

当然也可以直接修改anaconda的配置文件.condarc,改文件在家目录下我这里是C:\Users\Janus。

② 创建并激活环境

命令如下所示,当询问是否执行时,输入y(yes) 。

#创建环境

conda create --name python38 python=3.8

#激活环境

conda activate python38

#退出环境

conda deactivate

③ 在环境“python38”下安装TensorFlow

pip install tensorflow

#或者直接指定版本

pip install tensorflow==2.13.0

④ 查看TensorFlow版本

① 使用TensorFlow自带的版本查看功能

(python38) C:\Users\Janus>python

Python 3.8.18 (default, Sep 11 2023, 13:39:12) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32

Type "help", "copyright", "credits" or "license" for more information.

>>> import tensorflow as tf

>>> print(tf.version.VERSION)

2.13.0

>>> print(tf.__version__)

2.13.0

>>> exit()

这里表示安装的TensorFlow是2.13.0。

② 检查TensorFlow使用的Python版本

可以使用如下代码来确定正在使用哪个版本的Python:

import tensorflow as tf

print(tf.version.PYTHON_VERSION)

③ 使用命令行查看TensorFlow版本

可以使用pip show命令查看当前安装的TensorFlow版本:

(python38) E:\softinstall\Jupyter>pip show tensorflow

Name: tensorflow

Version: 2.13.0

Summary: TensorFlow is an open source machine learning framework for everyone.

Home-page: https://www.tensorflow.org/

Author: Google Inc.

Author-email: packages@tensorflow.org

License: Apache 2.0

Location: e:\softinstall\anaconda\envs\python38\lib\site-packages

Requires: tensorflow-intel

Required-by:

⑤ 测试Tensorflow

如下代码成功打印hello,tensorflow即说明成功。

import tensorflow as tf

tf.compat.v1.disable_eager_execution() 

hello = tf.constant('hello,tensorflow')

sess= tf.compat.v1.Session()

print(sess.run(hello))

【3】使用jupyter notebook

① 首先激活对应的conda环境

conda activate python38

② 安装ipykernel

在Anaconda 环境下优先使用conda install XXXX来安装包,如果不可以或者有特殊需求可采用pip install 。

(python38) C:\Users\Janus>conda install ipykernel

Solving environment: done

==> WARNING: A newer version of conda exists. <==

current version: 4.5.11

latest version: 23.10.0

Please update conda by running

$ conda update -n base -c defaults conda

# All requested packages already installed.

③ 切换到编程文件保存的目录

默认在C盘,我这里是:E:\softinstall\Jupyter

(python38) C:\Users\Janus>E:

(python38) E:\>cd softinstall

(python38) E:\softinstall>cd Jupyter

④ 将环境写入notebook的kernel中

(python38) E:\softinstall\Jupyter>python -m ipykernel install --user --name python38 --display-name "Python (python38)"

Installed kernelspec python38 in C:\Users\Janus\AppData\Roaming\jupyter\kernels\python38

⑤ 打开jupyter

安装Anaconda后自带Jupyter Notebook。在anaconda命令行中执行 jupyter notebook 就可以。

(python38) E:\softinstall\Jupyter>jupyter notebook

浏览器页面选择Python(python38)的Kernel即可

【4】遇到的问题总结

① 执行脚本提示 No module named ‘tensorflow’

这里查看jupyter kernelspec list ,其地址指向一个包含kernel.json的文件夹,打开这个json文件,发现里面指向的python.exe仍然是python3.7。

(python38) C:\Users\Janus>jupyter kernelspec list

Available kernels:

python3 E:\softinstall\Anaconda\share\jupyter\kernels\python3

重新安装Jupyter

conda install jupyter notebook

此外如果还需要什么环境比如pandas,那么在python38环境下安装即可,如canda install pandas 。

② No module named ‘matplotlib’

conda install matplotlib

③ No module named ‘sklearn’

conda install scikit-learn

④ partially initialized module ‘charset_normalizer’ has no attribute ‘md__mypyc’ (most likely due to a circular import)

conda install chardet

#或者

pip install chardet

好文阅读

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: