1.打开Anaconda Prompt
输入
conda create -n TF2.1 python=3.7
选择Y
电脑如果有英伟达GPU,则输入如下命令,选择Y,同意安装相关软件包
conda activate TF2.1
conda install cudatoolkit=10.1
安装完之后接着输入
conda activate TF2.1
conda install cudnn=7.6
安装tensorflow
conda activate TF=2.1
pip install tensorflow==2.1
若报错
ValueError: check_hostname requires server_hostname
解决:
pip install -i 镜像网址 模块名
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple tensorflow==2.1
镜像源网址:
1)http://mirrors.aliyun.com/pypi/simple/ 阿里云
2)https://pypi.mirrors.ustc.edu.cn/simple/ 中国科技大学
3) http://pypi.douban.com/simple/ 豆瓣
4) https://pypi.tuna.tsinghua.edu.cn/simple/ 清华大学
5) http://pypi.mirrors.ustc.edu.cn/simple/ 中国科学技术大学
基于以上,输入以下命令行进行安装
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple tensorflow==2.1
(换源下载后 如果仍然报错则可能是网络代理服务器打开了的问题,
在设置中关闭网络代理服务器即可成功下载安装扩展包。)
报错
TypeError: Descriptors cannot not be created directly.
If this call came from a _pb2.py file, your generated code is out of date and must be regenerated with protoc >= 3.19.0.
If you cannot immediately regenerate your protos, some other possible workarounds are:
1. Downgrade the protobuf package to 3.20.x or lower.
2. Set PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python (but this will use pure-Python parsing and will be much slower).
More information: https://developers.google.com/protocol-buffers/docs/news/2022-05-06#python-updates
可以用镜像加快下载速度
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple protobuf==3.19.0
进入验证
conda activate TF=2.1
(TF2.1) C:\Users\WJ>python
Python 3.7.13 (default, Mar 28 2022, 08:03:21) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
2022-07-03 21:15:52.040725: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudart64_101.dll
>>> tf._version_
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'tensorflow' has no attribute '_version_'
>>> tf.__version__
'2.1.0'
>>>
安装成功