问题描述:
#在Anaconda中安装CPU版本的Tensorflow1.15.0.
(base) C:\Users\Queena>conda create -n TF1.15 python=3.7
(TF1.15) C:\Users\Queena>pip install tensorflow==1.15.0
(TF1.15) C:\Users\Queena>python
Python 3.7.15 (default, Nov 24 2022, 18:44:54) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
...
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).
#出现如上TypeError报错.
解决方法:
降低protobuf的版本,得到3.20.1版本的protobuf.
#查看当前TF1.15环境pip安装的所有插件.
(TF1.15) C:\Users\Queena>pip list
Package Version
-------------------- ---------
absl-py 1.4.0
astor 0.8.1
certifi 2022.12.7
gast 0.2.2
google-pasta 0.2.0
grpcio 1.51.1
h5py 3.7.0
importlib-metadata 6.0.0
Keras-Applications 1.0.8
Keras-Preprocessing 1.1.2
Markdown 3.4.1
MarkupSafe 2.1.1
numpy 1.21.6
opt-einsum 3.3.0
pip 22.3.1
protobuf 4.21.12
setuptools 65.6.3
six 1.16.0
tensorboard 1.15.0
tensorflow 1.15.0
tensorflow-estimator 1.15.1
termcolor 2.2.0
typing_extensions 4.4.0
Werkzeug 2.2.2
wheel 0.37.1
wincertstore 0.2
wrapt 1.14.1
zipp 3.11.0
#需要降低protobuf的版本.
#卸载现有的protobuf.
(TF1.15) C:\Users\Queena>pip uninstall protobuf
Found existing installation: protobuf 4.21.12
Uninstalling protobuf-4.21.12:
Would remove:
e:\anaconda\anaconda3_install\envs\tf1.15\lib\site-packages\google\_upb\_message.cp37-win_amd64.pyd
e:\anaconda\anaconda3_install\envs\tf1.15\lib\site-packages\google\protobuf\*
e:\anaconda\anaconda3_install\envs\tf1.15\lib\site-packages\protobuf-4.21.12.dist-info\*
Proceed (Y/n)? y
Successfully uninstalled protobuf-4.21.12
#安装3.20.1版本的protobuf.
(TF1.15) C:\Users\Queena>pip install protobuf==3.20.1
Collecting protobuf==3.20.1
Downloading protobuf-3.20.1-cp37-cp37m-win_amd64.whl (905 kB)
---------------------------------------- 905.1/905.1 kB 773.5 kB/s eta 0:00:00
Installing collected packages: protobuf
Successfully installed protobuf-3.20.1
#再次执行python的import tensorflow as tf代码,不再报错.
(TF1.15) C:\Users\Queena>python
Python 3.7.15 (default, Nov 24 2022, 18:44:54) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>> tf.__version__
'1.15.0'
#也可使用如下代码验证.
>>> hello = tf.constant('Hello, TensorFlow!')
>>> sess = tf.Session()
>>> print(sess.run(hello))
#注意:Tensorflow1.0版本有Session,
# Tensorflow2.0版本已经没有Session.
>>> exit()
#此时的pip list
(TF1.15) C:\Users\Queena>pip list
Package Version
-------------------- ---------
absl-py 1.4.0
astor 0.8.1
certifi 2022.12.7
gast 0.2.2
google-pasta 0.2.0
grpcio 1.51.1
h5py 3.7.0
importlib-metadata 6.0.0
Keras-Applications 1.0.8
Keras-Preprocessing 1.1.2
Markdown 3.4.1
MarkupSafe 2.1.1
numpy 1.21.6
opt-einsum 3.3.0
pip 22.3.1
protobuf 3.20.1
setuptools 65.6.3
six 1.16.0
tensorboard 1.15.0
tensorflow 1.15.0
tensorflow-estimator 1.15.1
termcolor 2.2.0
typing_extensions 4.4.0
Werkzeug 2.2.2
wheel 0.37.1
wincertstore 0.2
wrapt 1.14.1
zipp 3.11.0
至此,问题得以解决!