【问题标题】:Tensorflow 2 showing showing error in Conda spyderTensorflow 2 在 Conda spyder 中显示错误
【发布时间】:2021-07-30 16:42:25
【问题描述】:

我之前在 python3_env 中使用 conda forge 安装了 tensorflow 1。然后我使用 pip 升级到 tensorflow 2,它安装正确,但没有在 spyder 中运行。然后我使用 conda remove 卸载了 tensorflow,然后在同一环境中安装了 conda install tensorflow gpu 版本。但是现在导入时显示以下错误:

runfile('E:/Machine Learning/Tensorflow/learning DL with tensorflow and keras.py', wdir='E:/Machine Learning/Tensorflow')
Traceback (most recent call last):

  File "E:\Machine Learning\Tensorflow\learning DL with tensorflow and keras.py", line 8, in <module>
    import tensorflow as tf

  File "C:\Users\snakk\AppData\Roaming\Python\Python36\site-packages\tensorflow\__init__.py", line 41, in <module>
    from tensorflow.python.tools import module_util as _module_util

  File "C:\Users\snakk\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\__init__.py", line 48, in <module>
    from tensorflow.python import keras

  File "C:\Users\snakk\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\keras\__init__.py", line 27, in <module>
    from tensorflow.python.keras import models

  File "C:\Users\snakk\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\keras\models.py", line 26, in <module>
    from tensorflow.python.keras.engine import functional

  File "C:\Users\snakk\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\keras\engine\functional.py", line 38, in <module>
    from tensorflow.python.keras.engine import training as training_lib

  File "C:\Users\snakk\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\keras\engine\training.py", line 56, in <module>
    from tensorflow.python.keras.saving import hdf5_format

  File "C:\Users\snakk\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\keras\saving\hdf5_format.py", line 30, in <module>
    from tensorflow.python.keras.saving import model_config as model_config_lib

  File "C:\Users\snakk\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\keras\saving\model_config.py", line 28, in <module>
    import yaml

  File "F:\Installed\Anaconda\envs\python3_env\lib\site-packages\yaml\__init__.py", line 13, in <module>
    from .cyaml import *

  File "F:\Installed\Anaconda\envs\python3_env\lib\site-packages\yaml\cyaml.py", line 7, in <module>
    from _yaml import CParser, CEmitter

  File "ext/_yaml.pyx", line 18, in init _yaml

AttributeError: module 'yaml' has no attribute 'error'

这是我的 conda 列表:

(python3_env) C:\WINDOWS\system32>conda list
# packages in environment at F:\Installed\Anaconda\envs\python3_env:
#
# Name                    Version                   Build  Channel
_anaconda_depends         2020.07                  py36_0
_r-mutex                  1.0.1               anacondar_1    conda-forge
_tflow_select             2.1.0                       gpu    anaconda
absl-py                   0.12.0                   pypi_0    pypi
alabaster                 0.7.12                   py36_0
anaconda                  custom                   py36_1
anaconda-client           1.7.2                    py36_0
anaconda-project          0.8.4                      py_0
argh                      0.26.2                   py36_0
asn1crypto                1.3.0                    py36_0
astor                     0.8.1              pyh9f0ad1d_0    conda-forge
astroid                   2.4.2                    py36_0
astropy                   4.0.1.post1      py36he774522_1
astunparse                1.6.3                    pypi_0    pypi
atomicwrites              1.4.0                      py_0
attrs                     19.3.0                     py_0
autopep8                  1.5.3                      py_0
babel                     2.8.0                      py_0
backcall                  0.2.0                      py_0
backports                 1.0                        py_2
backports.shutil_get_terminal_size 1.0.0                    py36_2
bcrypt                    3.1.7            py36he774522_1
beautifulsoup4            4.9.1                    py36_0
bitarray                  1.4.0            py36he774522_0
bkcharts                  0.2                      py36_0
blas                      1.0                         mkl
bleach                    3.1.5                      py_0
blosc                     1.21.0               h19a0ad4_0
bokeh                     2.1.1                    py36_0
boto                      2.49.0                   py36_0
bottleneck                1.3.2            py36h2a96729_1
brotlipy                  0.7.0           py36he774522_1000
bzip2                     1.0.8                he774522_0
ca-certificates           2020.10.14                    0    anaconda
cachetools                4.2.2                    pypi_0    pypi
certifi                   2020.6.20                py36_0    anaconda
cffi                      1.14.0           py36h7a1dbc1_0
chardet                   3.0.4                 py36_1003
click                     7.1.2                      py_0
cloudpickle               1.5.0                      py_0
clyent                    1.2.2                    py36_1
colorama                  0.4.3                      py_0
comtypes                  1.1.7                 py36_1001
console_shortcut          0.1.1                         4
contextlib2               0.6.0.post1                py_0
contextvars               2.4                        py_0
cryptography              2.9.2            py36h7a1dbc1_0
cudatoolkit               10.0.130                      0    anaconda
cudnn                     7.6.5                cuda10.0_0    anaconda
curl                      7.71.1               h2a8f88b_1
cycler                    0.10.0           py36h009560c_0
cython                    0.29.21          py36ha925a31_0
cytoolz                   0.10.1           py36he774522_0
dask                      2.20.0                     py_0
dask-core                 2.20.0                     py_0
decorator                 4.4.2                      py_0
defusedxml                0.6.0                      py_0
diff-match-patch          20200713                   py_0
distributed               2.20.0                   py36_0
docutils                  0.16                     py36_1
entrypoints               0.3                      py36_0
et_xmlfile                1.0.1                   py_1001
fastcache                 1.1.0            py36he774522_0
filelock                  3.0.12                     py_0
flake8                    3.8.3                      py_0
flask                     1.1.2                      py_0
flatbuffers               1.12                     pypi_0    pypi
freetype                  2.10.4               hd328e21_0
fsspec                    0.7.4                      py_0
future                    0.18.2                   py36_1
gast                      0.3.3                      py_0    conda-forge
get_terminal_size         1.0.0                h38e98db_0
gevent                    20.6.2           py36he774522_0
glob2                     0.7                        py_0
gmpy2                     2.0.8            py36h0964b28_3
google-auth               1.30.0                   pypi_0    pypi
google-auth-oauthlib      0.4.4                    pypi_0    pypi
google-pasta              0.2.0              pyh8c360ce_0    conda-forge
greenlet                  0.4.16           py36he774522_0
grpcio                    1.32.0                   pypi_0    pypi
h5py                      2.10.0           py36h5e291fa_0
hdf5                      1.10.4               h7ebc959_0
heapdict                  1.0.1                      py_0
html5lib                  1.1                        py_0
icc_rt                    2019.0.0             h0cc432a_1
icu                       67.1                 h33f27b4_0    conda-forge
idna                      2.10                       py_0
imageio                   2.9.0                      py_0
imagesize                 1.2.0                      py_0
immutables                0.14             py36he774522_0
importlib-metadata        1.7.0                    py36_0
importlib_metadata        1.7.0                         0
intel-openmp              2021.2.0           haa95532_616
intervaltree              3.0.2                      py_1
ipykernel                 5.3.2            py36h5ca1d4c_0
ipython                   7.16.1           py36h5ca1d4c_0
ipython_genutils          0.2.0                    py36_0
ipywidgets                7.5.1                      py_0
isort                     4.3.21                   py36_0
itsdangerous              1.1.0                    py36_0
jdcal                     1.4.1                      py_0
jedi                      0.17.1                   py36_0
jinja2                    2.11.2                     py_0
joblib                    0.16.0                     py_0
jpeg                      9d                   he774522_0    conda-forge
json5                     0.9.5                      py_0
jsonschema                3.2.0                    py36_0
jupyter                   1.0.0                    py36_7
jupyter_client            6.1.6                      py_0
jupyter_console           6.1.0                      py_0
jupyter_core              4.6.3                    py36_0
jupyterlab                2.1.5                      py_0
jupyterlab_server         1.2.0                      py_0
keras-applications        1.0.8                      py_1    conda-forge
keras-preprocessing       1.1.2                    pypi_0    pypi
keyring                   21.2.1                   py36_0
kiwisolver                1.2.0            py36h74a9793_0
krb5                      1.18.2               hc04afaa_0
lazy-object-proxy         1.4.3            py36he774522_0
libarchive                3.4.2                h5e25573_0
libblas                   3.8.0                    14_mkl    conda-forge
libcblas                  3.8.0                    14_mkl    conda-forge
libclang                  10.0.1          default_hf44288c_1    conda-forge
libcurl                   7.71.1               h2a8f88b_1
libiconv                  1.15                 h1df5818_7
liblapack                 3.8.0                    14_mkl    conda-forge
liblapacke                3.8.0                    14_mkl    conda-forge
liblief                   0.10.1               ha925a31_0
libllvm9                  9.0.1                h21ff451_0
libopencv                 4.4.0                    py36_3    conda-forge
libpng                    1.6.37               h2a8f88b_0
libprotobuf               3.12.4               h200bbdf_0
libsodium                 1.0.18               h62dcd97_0
libspatialindex           1.9.3                h6c2663c_0
libssh2                   1.9.0                h7a1dbc1_1
libtiff                   4.2.0                hd0e1b90_0
libwebp-base              1.2.0                h2bbff1b_0
libxml2                   2.9.10               hb89e7f3_3
libxslt                   1.1.34               he774522_0
llvmlite                  0.33.0           py36ha925a31_0
locket                    0.2.0            py36hfed976d_1
lxml                      4.5.2            py36h1350720_0
lz4-c                     1.9.3                h2bbff1b_0
lzo                       2.10                 he774522_2
m2w64-bwidget             1.9.10                        2    conda-forge
m2w64-bzip2               1.0.6                         6    conda-forge
m2w64-expat               2.1.1                         2    conda-forge
m2w64-fftw                3.3.4                         6    conda-forge
m2w64-flac                1.3.1                         3    conda-forge
m2w64-gcc-libgfortran     5.3.0                         6
m2w64-gcc-libs            5.3.0                         7
m2w64-gcc-libs-core       5.3.0                         7
m2w64-gettext             0.19.7                        2    conda-forge
m2w64-gmp                 6.1.0                         2
m2w64-gsl                 2.1                           2    conda-forge
m2w64-libiconv            1.14                          6    conda-forge
m2w64-libjpeg-turbo       1.4.2                         3    conda-forge
m2w64-libogg              1.3.2                         3    conda-forge
m2w64-libpng              1.6.21                        2    conda-forge
m2w64-libsndfile          1.0.26                        2    conda-forge
m2w64-libtiff             4.0.6                         2    conda-forge
m2w64-libvorbis           1.3.5                         2    conda-forge
m2w64-libwinpthread-git   5.0.0.4634.697f757               2
m2w64-libxml2             2.9.3                         3    conda-forge
m2w64-mpfr                3.1.4                         4    conda-forge
m2w64-pcre2               10.34                         0    conda-forge
m2w64-speex               1.2rc2                        3    conda-forge
m2w64-speexdsp            1.2rc3                        3    conda-forge
m2w64-tcl                 8.6.5                         3    conda-forge
m2w64-tk                  8.6.5                         3    conda-forge
m2w64-tktable             2.10                          5    conda-forge
m2w64-wineditline         2.101                         5    conda-forge
m2w64-xz                  5.2.2                         2    conda-forge
m2w64-zlib                1.2.8                        10    conda-forge
markdown                  3.2.2                      py_0    conda-forge
markupsafe                1.1.1            py36he774522_0
matplotlib                3.2.2                         0
matplotlib-base           3.2.2            py36h64f37c6_0
mccabe                    0.6.1                    py36_1
menuinst                  1.4.16           py36he774522_1
mistune                   0.8.4            py36he774522_0
mkl                       2019.4                      245
mkl-service               2.3.0            py36hb782905_0
mkl_fft                   1.0.14           py36h6288b17_0
mkl_random                1.0.4            py36h343c172_0
mock                      4.0.2                      py_0
more-itertools            8.4.0                      py_0
mpc                       1.1.0                h7edee0f_1
mpfr                      4.0.2                h62dcd97_1
mpir                      3.0.0                hec2e145_1
mpmath                    1.1.0                    py36_0
msgpack-python            1.0.0            py36h74a9793_1
msys2-conda-epoch         20160418                      1
multipledispatch          0.6.0                    py36_0
nbconvert                 5.6.1                    py36_0
nbformat                  5.0.7                      py_0
networkx                  2.4                        py_1
nltk                      3.5                        py_0
nose                      1.3.7                    py36_2
notebook                  6.0.3                    py36_0
numba                     0.50.1           py36h47e9c7a_0
numexpr                   2.7.1            py36h25d0782_0
numpy                     1.19.5                   pypi_0    pypi
numpy-base                1.17.0           py36hc3f5095_0
numpydoc                  1.1.0                      py_0
oauthlib                  3.1.0                    pypi_0    pypi
olefile                   0.46                     py36_0
opencv                    4.4.0                    py36_3    conda-forge
openpyxl                  3.0.4                      py_0
openssl                   1.1.1h               he774522_0    anaconda
opt-einsum                3.3.0                    pypi_0    pypi
packaging                 20.4                       py_0
pandas                    1.0.5            py36h47e9c7a_0    anaconda
pandoc                    2.12                 haa95532_0
pandocfilters             1.4.2                    py36_1
paramiko                  2.7.1                      py_0
parso                     0.7.0                      py_0
partd                     1.1.0                      py_0
path                      13.1.0                   py36_0
path.py                   12.4.0                        0
pathlib2                  2.3.5                    py36_0
pathtools                 0.1.2                      py_1
patsy                     0.5.1                    py36_0
pep8                      1.7.1                    py36_0
pexpect                   4.8.0                    py36_0
pickleshare               0.7.5                    py36_0
pillow                    7.2.0            py36hcc1f983_0
pip                       20.1.1                   pypi_0    pypi
pkginfo                   1.5.0.1                  py36_0
pluggy                    0.13.1                   py36_0
ply                       3.11                     py36_0
powershell_shortcut       0.0.1                         3
prometheus_client         0.8.0                      py_0
prompt-toolkit            3.0.5                      py_0
prompt_toolkit            3.0.5                         0
protobuf                  3.12.4           py36h003fed8_0    conda-forge
psutil                    5.7.0            py36he774522_0
py                        1.9.0                      py_0
py-lief                   0.10.1           py36ha925a31_0
py-opencv                 4.4.0            py36hac0dd68_3    conda-forge
pyasn1                    0.4.8                    pypi_0    pypi
pyasn1-modules            0.2.8                    pypi_0    pypi
pycodestyle               2.6.0                      py_0
pycosat                   0.6.3            py36he774522_0
pycparser                 2.20                       py_2
pycrypto                  2.6.1           py36he774522_10
pycurl                    7.43.0.5         py36h7a1dbc1_0
pydocstyle                5.0.2                      py_0
pyflakes                  2.2.0                      py_0
pygments                  2.6.1                      py_0
pylint                    2.5.3                    py36_0
pynacl                    1.4.0            py36h62dcd97_1
pyodbc                    4.0.30           py36ha925a31_0
pyopenssl                 19.1.0                     py_1
pyparsing                 2.4.7                      py_0
pyqt                      5.12.3           py36h003fed8_4    conda-forge
pyqt5-sip                 4.19.18                  pypi_0    pypi
pyqtchart                 5.12                     pypi_0    pypi
pyqtwebengine             5.12.1                   pypi_0    pypi
pyreadline                2.1                      py36_1
pyrsistent                0.16.0           py36he774522_0
pysocks                   1.7.1                    py36_0
pytables                  3.6.1            py36h1da0976_0
pytest                    5.4.3                    py36_0
python                    3.6.10               h9f7ef89_2
python-dateutil           2.8.1                      py_0
python-jsonrpc-server     0.3.4                      py_1
python-language-server    0.34.1                   py36_0
python-libarchive-c       2.9                        py_0
python-utils              2.4.0                      py_0    conda-forge
python_abi                3.6                     1_cp36m    conda-forge
pytz                      2020.1                     py_0
pywavelets                1.1.1            py36he774522_0
pywin32                   227              py36he774522_1
pywin32-ctypes            0.2.0                 py36_1000
pywinpty                  0.5.7                    py36_0
pyyaml                    5.3.1            py36he774522_1
pyzmq                     19.0.1           py36ha925a31_1
qdarkstyle                2.8.1                      py_0
qt                        5.12.9               hb2cf2c5_0    conda-forge
qtawesome                 0.7.2                      py_0
qtconsole                 4.7.5                      py_0
qtpy                      1.9.0                      py_0
r-base                    4.0.3                hddad469_6    conda-forge
r-collections             0.3.5             r40hda5aaf8_0    conda-forge
r-digest                  0.6.27            r40h096a1c2_0    conda-forge
regex                     2020.6.8         py36he774522_0
requests                  2.24.0                     py_0
requests-oauthlib         1.3.0                    pypi_0    pypi
rope                      0.17.0                     py_0
rsa                       4.7.2                    pypi_0    pypi
rtree                     0.9.4            py36h21ff451_1
ruamel_yaml               0.15.87          py36he774522_1
scikit-image              0.16.2           py36h47e9c7a_0
scikit-learn              0.23.1           py36h25d0782_0
scipy                     1.5.0            py36h9439919_0
seaborn                   0.10.1                     py_0
send2trash                1.5.0                    py36_0
setuptools                49.2.0                   py36_0
simplegeneric             0.8.1                    py36_2
singledispatch            3.4.0.3          py36h17d0c80_0
sip                       4.19.8           py36h6538335_0
six                       1.15.0                     py_0
snappy                    1.1.8                h33f27b4_0
snowballstemmer           2.0.0                      py_0
sortedcollections         1.2.1                      py_0
sortedcontainers          2.2.2                      py_0
soupsieve                 2.0.1                      py_0
sphinx                    3.1.2                      py_0
sphinxcontrib             1.0                      py36_1
sphinxcontrib-applehelp   1.0.2                      py_0
sphinxcontrib-devhelp     1.0.2                      py_0
sphinxcontrib-htmlhelp    1.0.3                      py_0
sphinxcontrib-jsmath      1.0.1                      py_0
sphinxcontrib-qthelp      1.0.3                      py_0
sphinxcontrib-serializinghtml 1.1.4                      py_0
sphinxcontrib-websupport  1.2.3                      py_0
spyder                    4.1.4                    py36_0
spyder-kernels            1.9.2                    py36_0
sqlalchemy                1.3.18           py36he774522_0
sqlite                    3.35.4               h2bbff1b_0
statsmodels               0.11.1           py36he774522_0
sympy                     1.6.1                    py36_0
tbb                       2021.2.0             h59b6b97_0
tblib                     1.6.0                      py_0
tensorboard               2.5.0                    pypi_0    pypi
tensorboard-data-server   0.6.1                    pypi_0    pypi
tensorboard-plugin-wit    1.8.0                    pypi_0    pypi
tensorflow                1.14.0          gpu_py36h305fd99_0    anaconda
tensorflow-base           1.14.0          gpu_py36h55fc52a_0    anaconda
tensorflow-estimator      2.4.0                    pypi_0    pypi
tensorflow-gpu            1.14.0               h0d30ee6_0    anaconda
termcolor                 1.1.0                      py_2    conda-forge
terminado                 0.8.3                    py36_0
testpath                  0.4.4                      py_0
threadpoolctl             2.1.0              pyh5ca1d4c_0
tk                        8.6.10               he774522_0
toml                      0.10.1                     py_0
toolz                     0.10.0                     py_0
tornado                   6.0.4            py36he774522_1
tqdm                      4.47.0                     py_0
traitlets                 4.3.3                    py36_0
typed-ast                 1.4.1            py36he774522_0
typing_extensions         3.7.4.2                    py_0
ujson                     1.35             py36hfa6e2cd_0
unicodecsv                0.14.1           py36h6450c06_0
urllib3                   1.25.9                     py_0
vc                        14.2                 h21ff451_1
vs2015_runtime            14.27.29016          h5e58377_2
watchdog                  0.10.3                   py36_0
wcwidth                   0.2.5                      py_0
webencodings              0.5.1                    py36_1
werkzeug                  1.0.1                      py_0
wheel                     0.36.2                   pypi_0    pypi
widgetsnbextension        3.5.1                    py36_0
win_inet_pton             1.1.0                    py36_0
win_unicode_console       0.5              py36hcdbd4b5_0
wincertstore              0.2              py36h7fe50ca_0
winpty                    0.4.3                         4
wrapt                     1.12.1                   pypi_0    pypi
xlrd                      1.2.0                    py36_0    anaconda
xlsxwriter                1.2.9                      py_0
xlwings                   0.19.5                   py36_0
xlwt                      1.3.0            py36h1a4751e_0
xz                        5.2.5                h62dcd97_0
yaml                      0.2.5                he774522_0
yapf                      0.30.0                     py_0
zeromq                    4.3.2                ha925a31_3
zict                      2.0.0                      py_0
zipp                      3.1.0                      py_0
zlib                      1.2.11               h62dcd97_4
zope                      1.0                      py36_1
zope.event                4.4                      py36_0
zope.interface            4.7.1            py36he774522_0
zstd                      1.4.5                h04227a9_0

【问题讨论】:

  • 嗨,Tensorflow 2 已正确安装在 pip 中,但我收到此错误:文件“E:\Machine Learning\Tensorflow\learning DL with tensorflow and keras.py”,第 8 行,在 import tensorflow as tf ModuleNotFoundError: No module named 'tensorflow'

标签: conda tensorflow2.0 spyder


【解决方案1】:

(这里是 Spyder 维护者) 这个问题的出现是因为你混合了 pip 和 conda 包。要修复它,请删除此目录

C:\Users\snakk\AppData\Roaming\Python

仅使用 Python 创建新环境:

conda create -n tf-env python

最后用 pip 安装 Tensorflow 和 Spyder,但没有 --user 标志以避免在所有环境之间共享它

pip install tensorflow

【讨论】:

  • 这会安装 tensorflow 2 还是 1 版本?我实际上需要 tensorflow 2
  • 顺便说一句,我安装了 yaml,protobuf 模块,这些模块一个一个地出现在错误消息中,然后它运行了,但是每次都显示一个很大的警告消息,并要求我使用装饰器是否要删除警告.有没有其他方法可以消除警告?
  • 基本上没有用 pip 安装的库在这里工作,我用 pip 安装了 tensorflow 和 keras。 Numpy 甚至没有被导入。
猜你喜欢
  • 2019-01-15
  • 1970-01-01
  • 2020-11-02
  • 2021-07-18
  • 1970-01-01
  • 2018-01-22
  • 2022-01-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多