无法导入tensorflow

tensorflow can not be imported

我最近新建了一个anaconda环境来安装最新版的Tensorflow。之后,我使用了以下命令 conda install tensorflow. 我使用此命令重新安装了 tensorflow:pip install --upgrade --no-deps --force-reinstall tensorflow 我在此环境中使用的 python 版本是: Python3.7.9. 当我想导入 tensorflow 时它抛出这个错误:

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-2-d6579f534729> in <module>
----> 1 import tensorflow

~\Anaconda3\lib\site-packages\tensorflow\__init__.py in <module>
     39 import sys as _sys
     40 
---> 41 from tensorflow.python.tools import module_util as _module_util
     42 from tensorflow.python.util.lazy_loader import LazyLoader as _LazyLoader
     43 

~\Anaconda3\lib\site-packages\tensorflow\python\__init__.py in <module>
     39 from tensorflow.python import pywrap_tensorflow as _pywrap_tensorflow
     40 
---> 41 from tensorflow.python.eager import context
     42 
     43 # pylint: enable=wildcard-import

~\Anaconda3\lib\site-packages\tensorflow\python\eager\context.py in <module>
     30 import six
     31 
---> 32 from tensorflow.core.framework import function_pb2
     33 from tensorflow.core.protobuf import config_pb2
     34 from tensorflow.core.protobuf import rewriter_config_pb2

~\Anaconda3\lib\site-packages\tensorflow\core\framework\function_pb2.py in <module>
      5 import sys
      6 _b=sys.version_info[0]<3 and (lambda x:x) or (lambda x:x.encode('latin1'))
----> 7 from google.protobuf import descriptor as _descriptor
      8 from google.protobuf import message as _message
      9 from google.protobuf import reflection as _reflection

ImportError: cannot import name 'descriptor' from 'google.protobuf' (unknown location)

这里是安装包的列表:

# Name                    Version                   Build  Channel
_tflow_select             2.3.0                     eigen
absl-py                   0.12.0           py38haa95532_0
aiohttp                   3.7.4            py38h2bbff1b_1
astunparse                1.6.3                      py_0
async-timeout             3.0.1            py38haa95532_0
attrs                     20.3.0             pyhd3eb1b0_0
blas                      1.0                         mkl
blinker                   1.4              py38haa95532_0
brotlipy                  0.7.0           py38h2bbff1b_1003
ca-certificates           2021.4.13            haa95532_1
cachetools                4.2.2              pyhd3eb1b0_0
certifi                   2020.12.5        py38haa95532_0
cffi                      1.14.5           py38hcd4344a_0
chardet                   3.0.4           py38haa95532_1003
click                     7.1.2              pyhd3eb1b0_0
coverage                  5.5              py38h2bbff1b_2
cryptography              3.4.7            py38h71e12ea_0
cython                    0.29.23          py38hd77b12b_0
gast                      0.4.0                      py_0
google-auth               1.30.0             pyhd3eb1b0_0
google-auth-oauthlib      0.4.4              pyhd3eb1b0_0
google-pasta              0.2.0                      py_0
grpcio                    1.36.1           py38hc60d5dd_1
h5py                      2.10.0           py38h5e291fa_0
hdf5                      1.10.4               h7ebc959_0
icc_rt                    2019.0.0             h0cc432a_1
idna                      2.10               pyhd3eb1b0_0
importlib-metadata        3.10.0           py38haa95532_0
intel-openmp              2021.2.0           haa95532_616
keras-applications        1.0.8                      py_1
keras-preprocessing       1.1.2              pyhd3eb1b0_0
libprotobuf               3.14.0               h23ce68f_0
markdown                  3.3.4            py38haa95532_0
mkl                       2021.2.0           haa95532_296
mkl-service               2.3.0            py38h2bbff1b_1
mkl_fft                   1.3.0            py38h277e83a_2
mkl_random                1.2.1            py38hf11a4ad_2
multidict                 5.1.0            py38h2bbff1b_2
numpy                     1.20.1           py38h34a8a5c_0
numpy-base                1.20.1           py38haf7ebc8_0
oauthlib                  3.1.0                      py_0
openssl                   1.1.1k               h2bbff1b_0
opt_einsum                3.1.0                      py_0
pip                       21.0.1           py38haa95532_0
protobuf                  3.14.0           py38hd77b12b_1
pyasn1                    0.4.8                      py_0
pyasn1-modules            0.2.8                      py_0
pycparser                 2.20                       py_2
pyjwt                     1.7.1                    py38_0
pyopenssl                 20.0.1             pyhd3eb1b0_1
pyreadline                2.1                      py38_1
pysocks                   1.7.1            py38haa95532_0
python                    3.8.8                hdbf39b2_5
requests                  2.25.1             pyhd3eb1b0_0
requests-oauthlib         1.3.0                      py_0
rsa                       4.7.2              pyhd3eb1b0_1
scipy                     1.6.2            py38h66253e8_1
setuptools                52.0.0           py38haa95532_0
six                       1.15.0           py38haa95532_0
sqlite                    3.35.4               h2bbff1b_0
tensorboard               2.4.0              pyhc547734_0
tensorboard-plugin-wit    1.6.0                      py_0
tensorflow                2.3.0           mkl_py38h8c0d9a2_0
tensorflow-addons         0.12.1                   pypi_0    pypi
tensorflow-base           2.3.0           eigen_py38h75a453f_0
tensorflow-estimator      2.3.0              pyheb71bc4_0
termcolor                 1.1.0            py38haa95532_1
typeguard                 2.12.0                   pypi_0    pypi
typing-extensions         3.7.4.3              hd3eb1b0_0
typing_extensions         3.7.4.3            pyh06a4308_0
urllib3                   1.26.4             pyhd3eb1b0_0
vc                        14.2                 h21ff451_1
vs2015_runtime            14.27.29016          h5e58377_2
werkzeug                  1.0.1              pyhd3eb1b0_0
wheel                     0.36.2             pyhd3eb1b0_0
win_inet_pton             1.1.0            py38haa95532_0
wincertstore              0.2                      py38_0
wrapt                     1.12.1           py38he774522_1
yarl                      1.6.3            py38h2bbff1b_0
zipp                      3.4.1              pyhd3eb1b0_0
zlib                      1.2.11               h62dcd97_4

谁能告诉我这有什么问题吗?

尽量不要混用 conda 和 pip 安装。

如果您不想删除环境并重新开始,可以:

  • 尝试 运行 conda remove tensorflow 从 conda 中删除 tensorflow(如果你想坚持使用 pip
  • 尝试 运行 pip uninstall tensorflow,然后 运行 conda install protobuf(如果你想坚持使用 conda

但最明智的做法可能是从一个新的 conda/virtualenv 环境开始,然后 运行 conda install tensorflow protobufpip install tensorflow protobuf