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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42244198/
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 21:28:16  来源:igfitidea点击:

ImportError: No module named tensorflow

pythonmoduleinstallationtensorflowpip

提问by Abhishek Gangwar

Please help me with this error

请帮我解决这个错误

I have installed the tensorflow module on my server and below is it's information

我已经在我的服务器上安装了 tensorflow 模块,下面是它的信息

15IT60R19@cpusrv-gpu-109:~$ pip show tensorflow
Name: tensorflow
Version: 1.0.0
Summary: TensorFlow helps the tensors flow
Home-page: http://tensorflow.org/
Author: Google Inc.
Author-email: [email protected]
License: Apache 2.0
Location: /home/other/15IT60R19/anaconda2/lib/python2.7/site-packages
Requires: mock, numpy, protobuf, wheel, six

But when I try to import tensorflow I get following error

但是当我尝试导入 tensorflow 时出现以下错误

>>> import tensorflow as tf
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named tensorflow

My python version is as following

我的python版本如下

Python 2.7.12 |Anaconda 2.5.0 (64-bit)| (default, Jul  2 2016, 17:42:40) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org

I have tried the solutions given in sol1

我已经尝试了sol1 中给出的解决方案

Sol2

溶胶2

I do not have sudo access to the server I can only use pip to install any module

我没有对服务器的 sudo 访问权限 我只能使用 pip 安装任何模块

回答by Dharma

Try installing tensorflow again with the whatever version you want and with option --ignore-installed like:

尝试使用您想要的任何版本和选项 --ignore-installed 再次安装 tensorflow,例如:

pip install tensorflow==1.2.0 --ignore-installed

I solved same issue using this command.

我使用这个命令解决了同样的问题。

回答by Cryptc

I had a more basic problem when I received this error.

当我收到这个错误时,我遇到了一个更基本的问题。

The "Validate your installation" instructions say to type: python

“验证您的安装”说明说要输入:python

However, I have both 2.7 and 3.6 installed. Because I used pip3to install tensorflow, I needed to type: python3

但是,我同时安装了 2.7 和 3.6。因为我用pip3安装tensorflow,所以需要输入:python3

Using the correct version, I could import the "tensorflow" module.

使用正确的版本,我可以导入“tensorflow”模块。

回答by Alpa8

Check if Tensorflow was installed successfully using:

使用以下命令检查 Tensorflow 是否已成功安装:

 pip3 show tensorflow

If you get something like

如果你得到类似的东西

Name: tensorflow
Version: 1.2.1
Summary: TensorFlow helps the tensors flow
Home-page: http://tensorflow.org/
Author: Google Inc.
Author-email: [email protected]
License: Apache 2.0
Location: /usr/local/lib/python3.5/dist-packages
Requires: bleach, markdown, html5lib, backports.weakref, werkzeug, numpy, protobuf, wheel, six

You may try adding the path of your tensorflow location by:

您可以尝试通过以下方式添加 tensorflow 位置的路径:

export PYTHONPATH=/your/tensorflow/path:$PYTHONPATH.

回答by Raymond Yip

For Anaconda3, simply install in Anaconda Navigator: enter image description here

对于 Anaconda3,只需在 Anaconda Navigator 中安装: 在此处输入图片说明

回答by Vijay

Try installing tensorflow in the user site - This installation only works for you.

尝试在用户站点安装 tensorflow - 此安装仅适用于您。

pip install tensorflow --user

pip 安装 tensorflow --user

回答by Pallavi Kalambe

you might wanna try this:

你可能想试试这个:

$conda install -c conda-forge tensorflow

回答by parlad

You may need this since first one may not work.

您可能需要这个,因为第一个可能不起作用。

python3 -m pip install --upgrade https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-0.12.0-py3-none-any.whl

python3 -m pip install --upgrade https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-0.12.0-py3-none-any.whl

回答by KunMing Xie

with python2

使用 python2

pip show tensorflowto check install

pip show tensorflow检查安装

python test.pyto run test

python test.py运行测试

with python3

使用 python3

pip3 show tensorflowto check install

pip3 show tensorflow检查安装

python3 test.pyto run test

python3 test.py运行测试

test.py

测试文件

import tensorflow as tf
import numpy as np

c = np.array([[3.,4], [5.,6], [6.,7]])
step = tf.reduce_mean(c, 1)                                                                                 
with tf.Session() as sess:
    print(sess.run(step))

Or, if you haven't install tensorflow yet, try the offical document

或者,如果你还没有安装tensorflow,试试官方文档

回答by Hubert Pham

I was trying to install tensorflow GPU for a Windows 7 with pip3 for python3.5.x. Instead of doing pip3 install --upgrade tensorflowI just did pip install tensorflowand after it completed i was finally able to import tensorflow in python3.5.x.

我试图用 pip3 for python3.5.x 为 Windows 7 安装 tensorflow GPU。而不是pip3 install --upgrade tensorflow我刚刚做的pip install tensorflow,完成后我终于能够在python3.5.x中导入tensorflow。

回答by Mike N

Instead of using the doc's command (conda create -n tensorflow pip python=2.7 # or python=3.3, etc.) which wanted to install python2.7 in the conda environment, and kept erroring out saying the module can't be found when following the installation validation steps, I used conda create -n tensorflow pip python=3to make sure python3 was installed in the environment.

我没有使用conda create -n tensorflow pip python=2.7 # or python=3.3, etc.想要在 conda 环境中安装 python2.7的文档命令 ( ),并且在执行安装验证步骤时一直报错说找不到模块,我曾经conda create -n tensorflow pip python=3确保在环境中安装了 python3 .

Doing this, I only had to type pythoninstead of python3when validating the installation and the error went away.

这样做,我只需要在验证安装时输入python而不是输入python3,错误就消失了。