Python Tensorflow 导入错误:没有名为“tensorflow”的模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46568913/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 17:42:15  来源:igfitidea点击:

Tensorflow import error: No module named 'tensorflow'

pythonwindowstensorflowinstallanaconda

提问by Lcat

I installed TensorFlow on my Windows Python 3.5 Anaconda environment The validation was successful (with a warning)

我在我的 Windows Python 3.5 Anaconda 环境上安装了 TensorFlow 验证成功(有警告)

(tensorflow) C:\>python

Python 3.5.3 |Intel Corporation| (default, Apr 27 2017, 17:03:30) [MSC v.1900 64 bit (AMD64)] on win32

Python 3.5.3 |英特尔公司| (默认,2017 年 4 月 27 日,17:03:30)[MSC v.1900 64 位 (AMD64)] 在 win32 上

Type "help", "copyright", "credits" or "license" for more information. Intel(R) Distribution for Python is brought to you by Intel Corporation. Please check out: https://software.intel.com/en-us/python-distribution

输入“帮助”、“版权”、“信用”或“许可”以获取更多信息。适用于 Python 的英特尔(R) 分发版由英特尔公司提供。请查看:https: //software.intel.com/en-us/python-distribution

>>> import tensorflow as tf
>>> hello = tf.constant('Hello, TensorFlow!')
>>> sess = tf.Session()

2017-10-04 11:06:13.569696: W C:\tf_jenkins\home\workspace\rel-win\M\windows\PY\35\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.

2017-10-04 11:06:13.569696: WC:\tf_jenkins\home\workspace\rel-win\M\windows\PY\35\tensorflow\core\platform\cpu_feature_guard.cc:45] TensorFlow 库不是编译为使用 AVX 指令,但这些在您的机器上可用,可以加快 CPU 计算。

>>> print(sess.run(hello))

b'Hello, TensorFlow!'

b'你好,TensorFlow!'

However, when I attempt to import it into my python code

但是,当我尝试将它导入我的 python 代码时

from __future__ import print_function, division
import numpy as np
import os
import matplotlib
import tensorflow as tf

I get this error

我收到这个错误

ImportError: No module named 'tensorflow'

导入错误:没有名为“tensorflow”的模块

This is the location of the tensorflow package on my C drive

这是我C盘上tensorflow包的位置

C:\Users\myname\Anaconda2\envs\tensorflow\Lib\site-packages\tensorflow

When I go to Anaconda Navigator, it seems I have to choose either root, Python35, or Tensorflow. It looks like the Tensorflow environment includes Python35.

当我去 Anaconda Navigator 时,似乎我必须选择 root、Python35 或 Tensorflow。看起来 Tensorflow 环境包括 Python35。

Anaconda Navigator launcher had to be reinstalled recently, possibly due to the Tensorflow installation. Maybe if there were another way to set the environment to Tensorflow within Anaconda /Spyder IDE other than the Navigator it might help

最近必须重新安装 Anaconda Navigator 启动器,可能是因为安装了 Tensorflow。也许如果有另一种方法可以在 Anaconda /Spyder IDE 中将环境设置为 Tensorflow,而不是 Navigator,它可能会有所帮助

Method of installing tensorflow

tensorflow的安装方法

conda create --name tensorflow python=3.5; 
pip install --ignore-installed --upgrade tensorflow 

I did try: uninstalling and reinstalling protobuf, as suggesed by some blogs

我确实尝试过:按照一些博客的建议卸载并重新安装 protobuf

I see another SO user asked the same questionin March, received no reply

我看到另一个 SO 用户在三月份问了同样的问题,但没有收到回复

采纳答案by Lcat

The reason Python 3.5 environment is unable to import Tensorflow is that Anaconda does not store the tensorflow package in the same environment.

Python 3.5 环境无法导入 Tensorflow 的原因是 Anaconda 没有将 tensorflow 包存储在同一环境中。

One solution is to create a new separate environment in Anaconda dedicated to TensorFlow with its own Spyder

一种解决方案是在 Anaconda 中创建一个新的独立环境,专门用于 TensorFlow 及其自己的 Spyder

conda create -n newenvt anaconda python=3.5
activate newenvt

and then install tensorflow into newenvt

然后将 tensorflow 安装到newenvt

I found this primerhelpful

我发现这本入门书很有帮助

回答by Rubens_Zimbres

In Windows 64, if you did this sequence correctly:

在 Windows 64 中,如果您正确执行此序列:

Anaconda prompt:

蟒蛇提示:

conda create -n tensorflow python=3.5
activate tensorflow
pip install --ignore-installed --upgrade tensorflow

Be sure you still are in tensorflow environment. The best way to make Spyder recognize your tensorflow environment is to do this:

确保您仍然处于 tensorflow 环境中。让 Spyder 识别你的 tensorflow 环境的最好方法是这样做:

conda install spyder

This will install a new instance of Spyder inside Tensorflow environment. Then you must install scipy, matplotlib, pandas, sklearn and other libraries. Also works for OpenCV.

这将在 Tensorflow 环境中安装一个新的 Spyder 实例。然后必须安装scipy、matplotlib、pandas、sklearn等库。也适用于 OpenCV。

Always prefer to install these libraries with "conda install" instead of "pip".

总是喜欢用“conda install”而不是“pip”来安装这些库。

回答by imtithal

The reason why Python base environment is unable to import Tensorflow is that Anaconda does not store the tensorflow package in the base environment.

Python基础环境无法导入Tensorflow的原因是Anaconda没有在基础环境中存储tensorflow包。

create a new separate environment in Anacondadedicated to TensorFlow as follows:

Anaconda 中创建一个专门用于 TensorFlow的新独立环境,如下所示:

conda create -n newenvt anaconda python=python_version

replace python_version by your python version

用你的 python 版本替换 python_version

activate the new environment as follows:

激活新环境如下:

activate newenvt

Then install tensorflow into the new environment (newenvt) as follows:

然后将tensorflow安装到新环境(newenvt)中,如下:

conda install tensorflow

Now you can check it by issuing the following python code and it will work fine.

现在您可以通过发出以下 python 代码来检查它,它会正常工作。

import tensorflow

回答by Imran Ahmad Ghazali

I think your tensorflow is not installed for local environment.The best way of installing tensorflow is to create virtualenv as describe in the tensorflow installation guide Tensorflow Installation.After installing you can activate the invironment and can run anypython script under that environment.

我认为您的 tensorflow 没有为本地环境安装。安装 tensorflow 的最佳方法是按照 tensorflow 安装指南Tensorflow 安装中的描述创建 virtualenv 。安装后,您可以激活环境并可以在该环境下运行任何 python 脚本。

回答by krishna kakade

deleting tensorflow from cDrive/users/envs/tensorflow and after that

从 cDrive/users/envs/tensorflow 和之后删除 tensorflow

conda create -n tensorflow python=3.6
 activate tensorflow
 pip install --ignore-installed --upgrade tensorflow

now its working for newer versions of python thank you

现在它适用于较新版本的python,谢谢

回答by user12667152

for python 3.8 version go for anaconda navigator then go for environments --> then go for base(root)----> not installed from drop box--->then search for tensorflow then install it then run the program.......hope it may helpful

对于 python 3.8 版本,选择 anaconda navigator 然后选择环境 --> 然后选择 base(root)----> 未从下拉框安装 ---> 然后搜索 tensorflow 然后安装它然后运行程序... ....希望它可能有帮助

回答by Koke Cacao

Since none of the above solve my issue, I will post my solution

由于以上都没有解决我的问题,我将发布我的解决方案

WARNING: if you just installed TensorFlow using conda, you have to restart your command prompt!

警告:如果您刚刚使用 conda 安装了 TensorFlow,则必须重新启动命令提示符!

Solution: restart terminal ENTIRELYand restart conda environment

解决方案:完全重启终端并重启conda环境

回答by Hamza ?zkan

Visual Studio in left panel is Python "interactive Select karnel"

左侧面板中的 Visual Studio 是 Python“交互式选择 karnel”

Pyton 3.7.x anaconda3/python.exe ('base':conda) I'm this fixing

Pyton 3.7.x anaconda3/python.exe ('base':conda) 我正在修复

回答by Sasha N

I had same issues on Windows 64-bit processor but manage to solve them. Check if your Python is for 32- or 64-bit installation. If it is for 32-bit, then you should download the executable installer (for e.g. you can choose latest Python version - for me is 3.7.3) https://www.python.org/downloads/release/python-373/-> Scroll to the bottom in Files section and select “Windows x86-64 executable installer”. Download and install it.

我在 Windows 64 位处理器上遇到了同样的问题,但设法解决了这些问题。检查您的 Python 是用于 32 位还是 64 位安装。如果是 32 位,那么您应该下载可执行安装程序(例如,您可以选择最新的 Python 版本 - 对我来说是 3.7.3) https://www.python.org/downloads/release/python-373/-> 滚动到文件部分的底部,然后选择“Windows x86-64 可执行安装程序”。下载并安装它。

The tensorflow installation steps check here : https://www.tensorflow.org/install/pip. I hope this helps somehow ...

tensorflow 安装步骤在这里检查:https://www.tensorflow.org/install/pip 。我希望这会有所帮助......