简体   繁体   English

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

[英]ImportError: No module named tensorflow

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: opensource@google.com
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 安装任何模块

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.我使用这个命令解决了同样的问题。

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.但是,我同时安装了 2.7 和 3.6。 Because I used pip3 to install tensorflow, I needed to type: python3因为我用pip3安装tensorflow,所以需要输入: python3

Using the correct version, I could import the "tensorflow" module.使用正确的版本,我可以导入“tensorflow”模块。

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: opensource@google.com
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.

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

Try installing tensorflow in the user site - This installation only works for you.尝试在用户站点安装 tensorflow - 此安装仅适用于您。

pip install tensorflow --user pip 安装 tensorflow --user

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

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

你可能想试试这个:

$conda install -c conda-forge tensorflow

with python2使用 python2

pip show tensorflow to check install pip show tensorflow来检查安装

python test.py to run test python test.py运行测试

with python3使用 python3

pip3 show tensorflow to check install pip3 show tensorflow来检查安装

python3 test.py to 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,试试官方文档

I was trying to install tensorflow GPU for a Windows 7 with pip3 for python3.5.x.我试图用 pip3 for python3.5.x 为 Windows 7 安装 tensorflow GPU。 Instead of doing pip3 install --upgrade tensorflow I just did pip install tensorflow and after it completed i was finally able to import tensorflow in python3.5.x.而不是做pip3 install --upgrade tensorflow我只是做了pip install tensorflow并且完成后我终于能够在 python3.5.x 中导入 tensorflow。

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=3 to make sure python3 was installed in the environment.而不是使用想要在 conda 环境中安装 python2.7 的文档命令( conda create -n tensorflow pip python=2.7 # or python=3.3, etc. ),并不断出错说找不到模块按照安装验证步骤,我使用conda create -n tensorflow pip python=3来确保 python3 安装在环境中。

Doing this, I only had to type python instead of python3 when validating the installation and the error went away.这样做,我只需要在验证安装时输入python而不是python3 ,错误就消失了。

For me, if I did对我来说,如果我这样做了

python3 -m pip install tensorflow

then I got the error the OP reports when using a 3rd party library calling tensorflow.然后我在使用调用 tensorflow 的 3rd 方库时收到了 OP 报告的错误。

However, when I substituted either tensorflow-cpu or tensorflow-gpu (depending upon which one is appropriate for you) then the code was suddenly able to find tensorflow.但是,当我替换tensorflow-cputensorflow-gpu (取决于哪个适合您)时,代码突然能够找到 tensorflow。

尝试 TensorFlow 文档中的Anaconda 安装步骤

Activate the virtualenv environment by issuing one of the following commands:通过发出以下命令之一激活 virtualenv 环境:

$ source ~/tensorflow/bin/activate # bash, sh, ksh, or zsh $ source ~/tensorflow/bin/activate # bash、sh、ksh 或 zsh
$ source ~/tensorflow/bin/activate.csh # csh or tcsh $ source ~/tensorflow/bin/activate.csh # csh 或 tcsh

Hope this help希望这有帮助

This Worked for me:这对我有用:

$ sudo easy_install pip
$ sudo easy_install --upgrade six
$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/mac/tensorflow-0.9.0-py2-none-any.whl
$ sudo pip install --upgrade $TF_BINARY_URL

I had exactly the same problem.我遇到了完全相同的问题。 It's because default python is in version 2 You need to link to the version 3.这是因为默认 python 是版本 2 你需要链接到版本 3。

>sudo rm -rf /usr/bin/python
>sudo ln -s /usr/bin/python3.5 /usr/bin/python

蟒蛇链接

I ran into the same issue.我遇到了同样的问题。 I simply updated my command to begin with python3 instead of python and it worked perfectly.我只是更新了我的命令以从python3而不是python开始,它运行得很好。

In my case, I install 32 Bit Python so I cannot install Tensorflow, After uninstall 32 Bit Python and install 64 Bit Python , I can install tensorflow successfully.就我而言,我安装了32 Bit Python因此无法安装 Tensorflow,卸载32 Bit Python并安装64 Bit Python ,我可以成功安装 tensorflow。

After reinstall Python 64 bit , you need to check your python install folder path is properly set in Windows Environment Path.重新安装Python 64 bit ,您需要检查您的 Python 安装文件夹路径是否在 Windows 环境路径中正确设置。

You can check Python version by typing python in cmd.您可以通过在 cmd 中键入python来检查 Python 版本。

On my remote machine, I had TensorFlow installed via pip and when I was importing it in ipython the import was successful.在我的远程机器上,我通过pip安装了TensorFlow ,当我在ipython中导入它时,导入成功。 Despite of that I still got the No module named tensorflow error when I was running my scripts.尽管如此,我在运行脚本时仍然遇到No module named tensorflow错误。 The issue here was that I was running my scripts with sudo , so the python and tensorflow paths were not visible to the root.这里的问题是我使用sudo运行我的脚本,所以pythontensorflow路径对 root 不可见。 When I ran my scripts without sudo , everything worked.当我在没有sudo情况下运行我的脚本时,一切正常。

My experience.我的经验。

A, check your python version, in my case python 3.6 A,检查你的python版本,在我的例子中是python 3.6

B, use the command: python3.6 myscript.py; B、使用命令:python3.6 myscript.py;

C, my tensorflow version is 1.10 C,我的tensorflow版本是1.10

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM