简体   繁体   English

Python 在 Anaconda 下找不到 Tensorflow 模块

[英]Python not finding Tensorflow module under Anaconda

I'm a newbie trying to execute the code in first_steps_with_tensor_flow.ipynb locally on Windows 10. I have installed Anaconda Navigator 1.8.2, created an environment where I've installed, among others, tensorflow package.我是一个新手,试图在 Windows 10 上本地执行first_steps_with_tensor_flow.ipynb 中的代码。我已经安装了 Anaconda Navigator 1.8.2,创建了一个我已经安装了tensorflow包的环境。 I then launch Visual Studio Code from Anaconda and run然后我从 Anaconda 启动 Visual Studio Code 并运行

import math
from IPython import display
from matplotlib import cm
from matplotlib import gridspec
from matplotlib import pyplot as plt
import numpy as np
import pandas as pd
from sklearn import metrics
import tensorflow as tf
from tensorflow.python.data import Dataset

The last line is marked with red in VS Code, saying E0401:Unable to import 'tensorflow.python.data' .最后一行在 VS Code 中用红色标记,表示E0401:Unable to import 'tensorflow.python.data' Running, I get ModuleNotFoundError: No module named 'tensorflow' .运行时,我得到ModuleNotFoundError: No module named 'tensorflow' However, if I change the last line to print (tf.__version__) I do get 1.2.1 as output, so obviously Tensorflow is installed.但是,如果我将最后一行更改为print (tf.__version__)我确实会得到1.2.1作为输出,因此显然已安装了 Tensorflow。 Oddly VS Code complains E1101:Module 'tensorflow' has no __version__ member , but that line works.奇怪的是 VS Code 抱怨E1101:Module 'tensorflow' has no __version__ member ,但该行有效。

What am I doing wrong here?我在这里做错了什么?

are you using Visual Studio Code (VSC) or just pylint in general?您使用的是 Visual Studio Code (VSC) 还是一般的 pylint? I found out the reason why this is happening.我找到了发生这种情况的原因。

For VSC, the python extension, use pylint for intellisense of python.对于 VSC,python 扩展,使用 pylint 进行 python 的智能感知。 Pylint seems to have a bug with the sub-module. Pylint 的子模块似乎有一个错误。 For me, the error only shows in VSC and not in prompt .对我来说,错误只显示在 VSC 而不是 prompt

I solved this problem by doing the following steps:我通过执行以下步骤解决了这个问题:

Click "Code" -> Click "Preferences" -> Click "Settings"单击“代码”-> 单击“首选项”-> 单击“设置”

Now in the settings, you have a search bar on top, search:现在在设置中,顶部有一个搜索栏,搜索:

python.linting.pylintEnable and set it to false python.linting.pylintEnable并将其设置为false

Now there is alternative for linting, I am using the pep8 as a example here since it come with the anaconda, search this现在有linting的替代方案,我在这里使用pep8作为示例,因为它与anaconda一起提供,搜索这个

python.linting.pep8Enabled and set it to true python.linting.pep8Enabled并将其设置为true

Now pylint is not the default linter anymore, we are now using the pep8.现在 pylint 不再是默认的 linter,我们现在使用的是 pep8。 Just to make sure, quit VSC and reopen it.只是为了确保,退出 VSC 并重新打开它。 there should not be any error anymore.应该不会再有任何错误了。

I am fairly sure this is a problem with pylint, instead of the TF you installed.我相当确定这是 pylint 的问题,而不是您安装的 TF。 By default, the Microsoft python extension in the VSC is using pylint as the linting tool.默认情况下,VSC 中的 Microsoft python 扩展使用 pylint 作为 linting 工具。 By changing it to pep8 or other we can avoid the error.通过将其更改为 pep8 或其他,我们可以避免该错误。

Tensorflow is on version 1.7.0 currently and you have version 1.2.1 installed. Tensorflow 当前的版本是 1.7.0,您安装了 1.2.1 版本。 Having reviewed the tensorflow repository , the tensorflow.python.data was first used in 1.4.0-rc0, before that it was in contrib.在查看了tensorflow 存储库后,tensorflow.python.data 最初在 1.4.0-rc0 中使用,在此之前它被用于 contrib。

I'd suggest to upgrade to the current version and retry.我建议升级到当前版本并重试。 Alternatively look up the correct path in version 1.2.1.或者在 1.2.1 版中查找正确的路径。

Pylint was not installed for VS to work with.没有安装 Pylint 供 VS 使用。 I found this by searching (in VS) for lint, inspired by Haomin above.我通过搜索(在 VS 中)lint 找到了这个,灵感来自上面的 Haomin。 A messagebox appeared and the first suggestion was to install pylint.出现了一个消息框,第一个建议是安装 pylint。 "pip install pylint". “pip安装pylint”。 I clicked this and it all worked!我点击了这个,一切都奏效了! This has taken ages to find.这需要很长时间才能找到。

Use tf not tensorflow .使用tf而不是tensorflow You have imported tensorflow as tf , not tensorflow .您已将 tensorflow 导入为tf ,而不是tensorflow

import math
from IPython import display
from matplotlib import cm
from matplotlib import gridspec
from matplotlib import pyplot as plt
import numpy as np
import pandas as pd
from sklearn import metrics
import tensorflow as tf
from tf.python.data import Dataset 

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

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