【问题标题】:can not import TensorFlow in Spyder or Python (ModuleNotFoundError: No module named 'tensorflow')无法在 Spyder 或 Python 中导入 TensorFlow(ModuleNotFoundError: No module named 'tensorflow')
【发布时间】:2020-07-26 13:43:45
【问题描述】:

我已尝试根据此处https://docs.anaconda.com/anaconda/user-guide/tasks/tensorflow/ 的手册安装 TensorFlow 的 CPU 和 GPU 版本,一切正常:

(tf-gpu) C:\Users\Kosh>conda create -n tf tensorflow 收集 包元数据(current_repodata.json):完成解决环境: 完成

==> 警告:存在较新版本的 conda。

请通过运行更新 conda

$ conda update -n base -c defaults conda

套餐计划

环境位置:C:\Anaconda3\envs\tf

添加/更新规格: -张量流

将下载以下包:

package                    |            build
---------------------------|-----------------
_tflow_select-2.2.0        |            eigen           3 KB
tensorflow-2.1.0           |eigen_py37hd727fc0_0           4 KB
tensorflow-base-2.1.0      |eigen_py37h49b2757_0        35.4 MB
------------------------------------------------------------
                                       Total:        35.4 MB

将安装以下新包:

_tflow_select pkgs/main/win-64::_tflow_select-2.2.0-eigen
absl-py pkgs/main/win-64::absl-py-0.9.0-py37_0 asn1crypto pkgs/main/win-64::asn1crypto-1.3.0-py37_0 astor
pkgs/main/win-64::astor-0.8.0-py37_0 blas
pkgs/main/win-64::blas-1.0-mkl 闪光灯
pkgs/main/win-64::blinker-1.4-py37_0 ca-certificates
pkgs/main/win-64::ca-certificates-2020.1.1-0 缓存工具
pkgs/main/noarch::cachetools-3.1.1-py_0 证书
pkgs/main/win-64::certifi-2020.4.5.1-py37_0 cffi
pkgs/main/win-64::cffi-1.14.0-py37h7a1dbc1_0 chardet
pkgs/main/win-64::chardet-3.0.4-py37_1003 点击
pkgs/main/noarch::click-7.1.1-py_0 密码学
pkgs/main/win-64::cryptography-2.8-py37h7a1dbc1_0 gast
pkgs/main/win-64::gast-0.2.2-py37_0 google-auth
pkgs/main/noarch::google-auth-1.13.1-py_0 google-auth-oauth~ pkgs/main/noarch::google-auth-oauthlib-0.4.1-py_2 google-pasta
pkgs/main/noarch::google-pasta-0.2.0-py_0 grpcio
pkgs/main/win-64::grpcio-1.27.2-py37h351948d_0 h5py
pkgs/main/win-64::h5py-2.10.0-py37h5e291fa_0 hdf5
pkgs/main/win-64::hdf5-1.10.4-h7ebc959_0 icc_rt
pkgs/main/win-64::icc_rt-2019.0.0-h0cc432a_1 idna
pkgs/main/noarch::idna-2.9-py_1 intel-openmp
pkgs/main/win-64::intel-openmp-2020.0-166 keras-applications pkgs/main/noarch::keras-applications-1.0.8-py_0 keras-preprocessi~ pkgs/main/noarch::keras-preprocessing-1.1.0-py_1 libprotobuf
pkgs/main/win-64::libprotobuf-3.11.4-h7bd577a_0 降价
pkgs/main/win-64::markdown-3.1.1-py37_0 mkl
pkgs/main/win-64::mkl-2020.0-166 mkl-service
pkgs/main/win-64::mkl-service-2.3.0-py37hb782905_0 mkl_fft
pkgs/main/win-64::mkl_fft-1.0.15-py37h14836fe_0 mkl_random
pkgs/main/win-64::mkl_random-1.1.0-py37h675688f_0 numpy
pkgs/main/win-64::numpy-1.18.1-py37h93ca92e_0 numpy-base
pkgs/main/win-64::numpy-base-1.18.1-py37hc3f5095_1 oauthlib
pkgs/main/noarch::oauthlib-3.1.0-py_0 openssl
pkgs/main/win-64::openssl-1.1.1f-he774522_0 opt_einsum
pkgs/main/noarch::opt_einsum-3.1.0-py_0 pip
pkgs/main/win-64::pip-20.0.2-py37_1 protobuf
pkgs/main/win-64::protobuf-3.11.4-py37h33f27b4_0 pyasn1
pkgs/main/noarch::pyasn1-0.4.8-py_0 pyasn1-modules
pkgs/main/noarch::pyasn1-modules-0.2.7-py_0 pycparser
pkgs/main/noarch::pycparser-2.20-py_0 pyjwt
pkgs/main/win-64::pyjwt-1.7.1-py37_0 pyopenssl
pkgs/main/win-64::pyopenssl-19.1.0-py37_0 pyreadline
pkgs/main/win-64::pyreadline-2.1-py37_1 pysocks
pkgs/main/win-64::pysocks-1.7.1-py37_0 python
pkgs/main/win-64::python-3.7.7-h60c2a47_0_cpython 请求
pkgs/main/win-64::requests-2.23.0-py37_0 requests-oauthlib pkgs/main/noarch::requests-oauthlib-1.3.0-py_0 rsa
pkgs/main/noarch::rsa-4.0-py_0 scipy
pkgs/main/win-64::scipy-1.4.1-py37h9439919_0 setuptools
pkgs/main/win-64::setuptools-46.1.3-py37_0 六
pkgs/main/win-64::six-1.14.0-py37_0 sqlite
pkgs/main/win-64::sqlite-3.31.1-he774522_0 张量板
pkgs/main/noarch::tensorboard-2.1.0-py3_0 tensorflow
pkgs/main/win-64::tensorflow-2.1.0-eigen_py37hd727fc0_0
张量流基础
pkgs/main/win-64::tensorflow-base-2.1.0-eigen_py37h49b2757_0
张量流估计~ pkgs/main/noarch::tensorflow-estimator-2.1.0-pyhd54b08b_0 termcolor pkgs/main/win-64::termcolor-1.1.0-py37_1 urllib3
pkgs/main/win-64::urllib3-1.25.8-py37_0 vc
pkgs/main/win-64::vc-14.1-h0510ff6_4 vs2015_runtime
pkgs/main/win-64::vs2015_runtime-14.16.27012-hf0eaf9b_1 werkzeug
pkgs/main/win-64::werkzeug-0.14.1-py37_0 轮
pkgs/main/win-64::wheel-0.34.2-py37_0 win_inet_pton
pkgs/main/win-64::win_inet_pton-1.1.0-py37_0 wincertstore
pkgs/main/win-64::wincertstore-0.2-py37_0 wrapt
pkgs/main/win-64::wrapt-1.12.1-py37he774522_1 zlib
pkgs/main/win-64::zlib-1.2.11-h62dcd97_3

继续([y]/n)?是的

下载和解压包 _tflow_select-2.2.0 | 3 KB | ################################################# ##########################| 100% 张量流-2.1.0 | 4 KB |

################################################# #################### | 100% 张量流基础 2.1。 | 35.4 MB | ################################################# #################### | 100% 准备交易:完成 验证交易:完成

执行事务:完成 #

要激活此环境,请使用

#

$ conda 激活 tf

#

要停用活动环境,请使用

#

$ conda 停用

(tf-gpu) C:\Users\Kosh> (tf-gpu) C:\Users\Kosh> (tf-gpu) C:\Users\Kosh>conda activate tf-2 找不到 conda 环境: tf-2 您可以使用conda info --envs 列出所有可发现的环境。

(tf-gpu) C:\Users\Kosh>conda 激活 tf

但是当我尝试在 Spyder 或 Python 中导入它时,我得到了相同的结果:

import tensorflow as tf
Traceback (most recent call last):

  File "<ipython-input-1-64156d691fe5>", line 1, in <module>
    import tensorflow as tf

ModuleNotFoundError: No module named 'tensorflow'

有人可以帮我吗?

【问题讨论】:

    标签: python-3.x tensorflow spyder


    【解决方案1】:

    它对我有用:

    1. 以管理员身份运行anaconda提示符。(右键->以管理员身份运行)。

    2. pip 卸载 tensorflow。

    3. 关闭 anaconda 提示符。

    4. 再次以管理员身份运行 anaconda 提示符。

    5. 类型: 康达安装张量流 它会要求一些 y / n 输入 y。

    现在完成后,将 python 解释器的环境更改为安装 tensorflow 的 anaconda 环境。

    之后tensorflow导入成功!

    【讨论】:

      【解决方案2】:

      我在使用 Python 3.7 和 Tensorflow 2.1 时遇到了同样的问题。

      我使用 conda create -n tf-2 tensorflow pandas 创建了环境 tf-2。

      使用命令提示符没有错误,但是 Spyder 没有找到 Tensorflow 库。

      我确认 IPython 窗口显示的是 Python 3.6.9(基本版本),而命令提示符将 3.7 返回到此命令:

      python -c "import platform; print(platform.python_version())"
      

      原因是:

      • 新环境没有安装 Spyder;
      • conda 默认安装 Tensorflow 1.13。

      然后,我删除了环境 tf2 并重新创建了它,包括 Python 版本和 Spyder:

      conda create -n tf2 python=3.6 spyder
      

      然后,我通过 pip 安装了 tensorflow。

      conda activate tf2
      pip install tensorflow
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-07-17
        • 2016-04-27
        • 2020-10-29
        • 2018-10-28
        • 2022-12-19
        • 2020-10-03
        • 1970-01-01
        • 2022-11-18
        相关资源
        最近更新 更多